【发布时间】:2020-10-18 22:21:15
【问题描述】:
我有以下 MongoDB 架构:
let ChannelSchema = new Schema({
name: {type: String},
active: { type: Boolean, default: true },
}, { timestamps: true })
let UserSchema = new Schema({
username: {type: String, required: [true, "username_required"], unique: true},
channel: [ ChannelSchema ]
}, { timestamps: true });
我正在尝试创建一个端点来切换我的 Channel 架构的活动状态。但是,当我创建以下测试代码时,响应是 undefined
router.post('/deactivate', async (req, res, next) => {
let channelId = req.body.channelId;
let channel = await Channel.findOneById(channelId});
res.json(channel);
});
在这种情况下,如何通过 id 选择频道并将活动状态更改为 false?我宁愿不必先选择用户文档。这可能吗?
【问题讨论】:
-
您可以将 $set 用于哈希字段,将位置更新运算符用于数组字段。
-
我得调查一下,你
标签: node.js mongodb express mongoose mongoose-schema