所以我有一个猫鼬模式:

schema = mongoose.Schema({
    identifier: Number,
    shopItems: [{
         identifier: Number,
         price: Number
    }]
});

现在我知道如何使用{ $push { shopItems { identifier: id, price: price } }将新项目推送到这个collections shopitems数组。
但现在我想用一个特定的标识符值更新shopitems数组中的一个项,这可能吗?
我在想这会管用的:{ $push { "shopItems.identifier": myVal, price: newPrice } }但是没有,所以我真的迷路了。

最佳答案

您可以实现以下目标:

db.collection.update({'shopItems.identifier' : myVal }, { $set : { 'shopItems.$.price' : newPrice}});

10-06 02:30