2016-11-04 67 views
0

我有一个简单的模式:猫鼬,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" 
    } 
    ] 
} 

我不明白发生了什么,是否有人有好主意吗?

在此先感谢您的帮助

干杯, 英里

回答

0

当与更新操作,例如使用db.collection.update()和db.collection.findAndModify(),

位置$运算符充当匹配查询文档的第一个元素的占位符。

0

我发现了这个问题,以及对我的耻辱。正是有了邮递员的一个问题,其实我觉得我是用PUT但它与我的特点POST添加额外的,我现在明白为什么它从来没有工作......

需要降温了我的大脑,愚蠢的问题