Javascript Mongoose更新对象值数组(如果存在),否则在单个查询中推送对象
我有下面的猫鼬模式 { "name": "String", "code": "String", "publish": { "status": 1, "channels": [ { "_id": false, "channelId": "Number", "remoteId": "String" } ] } } 是否有任何解决方案可以使用Mongoose在单个查询中插入和更新字段 const setFields = { publish: { channels: { 1 } } } Categories.updateOne( { name: 'Pantlooms', "publish.channels.channelId": channelId }, { $set: setFields, $addToSet: { channels: { remoteId: "12" } } }, { upsert: true } ) Categories.update({ name: 'Pantlooms' }, { $set: { "publish": { $cond: { if: { $eq: ["channels.$.channelId", 1] }, then: { channels: [{ remoteId: "12"}] }, // it's the upsert case else: { channels: [{ channelId: 1, remoteId: "12" }] } // it's the update case } } } }, { upsert: true }) let conditions = { "name": "Pantlooms", "publish.channels.channelId": { "$nin": [1] } }; var update = { $addToSet: { publish: { channels: { channelId: 1, remoteId: "12" } } } } Categories.findOneAndUpdate(conditions, update, function (err, doc) { console.log(err); });Javascript Mongoose更新对象值数组(如果存在),否则在单个查询中推送对象,javascript,mongodb,mongoose,insert-update,Javascript,Mongodb,Mongoose,Insert Update,我有下面的猫鼬模式 { "name": "String", "code": "String", "publish": { "status": 1, "channels": [ { "_id": false, "channelId": "Number", "remoteId": "String" } ]