Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mongoose更新对象值数组(如果存在),否则在单个查询中推送对象_Javascript_Mongodb_Mongoose_Insert Update - Fatal编程技术网

Javascript Mongoose更新对象值数组(如果存在),否则在单个查询中推送对象

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" } ]

我有下面的猫鼬模式

{ "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); });