模型如下:
let gardenmodal = new mongoose.Schema({
display:{type:Boolean, default:true},
itemList:[{type:Number,count:Number}]
});
let GardenModel = db.model('GardenModel', gardenmodal);
文档如下:
{ display: true,
itemList:
[ { type: 1, count: 200 },
{ type: 2, count: 81 },
{ type: 3, count: 78 } ],
_id: 5b2e52144c73cb22fc77b92b,
__v: 0 }
我想更新类型= 1的元素以使其num减少2,并更新类型= 2的元素以使其num减少3,同时更新一个类型为10的新元素count = 100,需要推入itemList,这是我的尝试:
let res = await GardenModel.findOneAndUpdate({},{
$inc:{"itemList.$[elem1].count":-3, "itemList.$[elem2].count" : -2},
$push:{"itemList":{ type: 10, count: 200 }}
},{
arrayFilters:[{"elem1.type":2},{"elem2.type":3}],
new:true
});
但是它失败了:
(node:6596) UnhandledPromiseRejectionWarning: MongoError: Updating the path 'itemList.$[elem1].count' would create a conflict at 'itemList'
at H:\node\wcgroup\node_modules\mongodb-core\lib\connection\pool.js:593:63
at authenticateStragglers (H:\node\wcgroup\node_modules\mongodb-core\lib\connection\pool.js:516:16)
at Connection.messageHandler (H:\node\wcgroup\node_modules\mongodb-core\lib\connection\pool.js:552:5)
at emitMessageHandler (H:\node\wcgroup\node_modules\mongodb-core\lib\connection\connection.js:309:10)
at Socket.<anonymous> (H:\node\wcgroup\node_modules\mongodb-core\lib\connection\connection.js:452:17)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:279:12)
at readableAddChunk (_stream_readable.js:264:11)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onread (net.js:636:20)
我该怎么办?