我有一个简单的模式:猫鼬,findOneAndUpdate,更新到数组,但复制
var extraSchema = new Schema({
key : String,
value : String,
id : String
});
var ItemSchema = new Schema({
department : String,
category : String,
id : String,
name : String,
description : String,
price : Number,
seller : String,
quantity : Number,
extra : [extraSchema],
visible : Boolean
});
我尝试更新一个额外的对象为文档项目,这里有一个例子:
{
"_id": "581cf2597b27281f04e8619e",
"name": "300",
"description": "The dvd of the movie 300",
"price": 19,
"seller": "BestDVD",
"id": "1",
"quantity": 10,
"department": "581c783f41d2893b80f3b0ba",
"category": "581c7f8441d2893b80f3b0c0",
"visible": true,
"__v": 0,
"extra": [
{
"id": "1",
"value": ".100",
"key": "weight",
"_id": "581cf2667b27281f04e8619f"
}
]
}
所以我想改变ID 1的额外元素的.250
值我做了以下内容:
Item.findOneAndUpdate({'extra.id' : req.body.id},
{'$set' : {
'extra.$.key' : req.body.key,
'extra.$.value' : req.body.value
}}, function(err) {
if (err) throw err;
}
);
当我这样做,它不会更新ID为1的额外的元素,但它复制的元素,并更改重量:
{
"_id": "581cf2597b27281f04e8619e",
"name": "300",
"description": "The dvd of the 300 movie",
"price": 19,
"seller": "BestDVD",
"id": "1",
"quantity": 10,
"department": "581c783f41d2893b80f3b0ba",
"category": "581c7f8441d2893b80f3b0c0",
"visible": true,
"__v": 0,
"extra": [
{
"id": "1",
"value": "\".100\"",
"key": "weight",
"_id": "581cf2667b27281f04e8619f"
},
{
"id": "1",
"value": ".250",
"key": "weight",
"_id": "581cf2857b27281f04e861a0"
}
]
}
我不明白发生了什么,是否有人有好主意吗?
在此先感谢您的帮助
干杯, 英里