2016-01-24 112 views
10

使用的MongoDB和猫鼬,我不能得到$推到一个元素追加到我的数组。在我运行这个之后,我检查了mongo shell并且新的append不在那里。不知道我做错了:猫鼬更新不坚持

UserModel.findOne({ firstName: "bob" }, 'pets', function(err, user) { 
    UserModel.update(user, { 
    $push: { 
     pets: { 
      name: "pear", type: "pig" 
     } 
    } 
    }, function(err, numAffected, raw) { 
     if (err) console.log(err); 
     console.log('updated ' + JSON.stringify(numAffected)); 
    }); 
}); 

Mongoose: users.ensureIndex({ username: 1 }) { unique: true, background: true } 
Mongoose: users.findOne({ firstName: 'bob' }) { fields: { pets: 1 } } 
Mongoose: users.update({ pets: [ { type: 'dog', name: 'apple' }, { type: 'cat', name: 'felix' } ], _id: ObjectId("56a53d45a428cbde3c4299f8") }) { '$push': { pets: { name: 'pear', type: 'pig' } } } {} 
updated {"ok":1,"nModified":0,"n":0} 

> bob = db.users.find()[0] 
{ 
    "_id" : ObjectId("56a53d45a428cbde3c4299f8"), 
    "firstName" : "bob", 
    "lastName" : "smith", 
    "username" : "bob123", 
    "pets" : [ 
     { 
      "name" : "apple", 
      "type" : "dog" 
     }, 
     { 
      "name" : "felix", 
      "type" : "cat" 
     } 
    ], 
    "__v" : 0 
} 
> 

UPDATE: 改变类型字段,仍然没有成功后。这次我只是想推{ $push: { pets: "sssss" } }

UserModel.findOne({ firstName: "bob" }, 'pets', function(err, user) { 
    UserModel.update(user, 
    { $push: { pets: "sssss" }}, 
    function(err, numAffected, raw) { 
     if (err) console.log(err); 
     console.log('updated ' + JSON.stringify(numAffected)); 
    }); 
}); 

下面是实际的MongoDB的日志:

2016-01-29T03:14:37.030+0000 I COMMAND [conn17] command curves.$cmd command: update { update: "users", updates: [ { q: { pets: [ { petType: "pig", name: "pear" } ], _id: ObjectId('56aad0a3ef5848c231ec80ed') }, u: { $push: { pets: "sssss" } }, upsert: false, multi: false } ], ordered: true, writeConcern: { w: 1 } } ntoskip:0 keyUpdates:0 writeConflicts:0 numYields:0 reslen:55 locks:{ Global: { acquireCount: { r: 1, w: 1 } }, Database: { acquireCount: { w: 1 } }, Collection: { acquireCount: { w: 1 } } } protocol:op_query 0ms 

回答

2

的真正原因是,pets阵列中使用的type不正确。实际上,typemongoose一个关键字,它被用来标记Schema Type

如果改变typetype1如下模式

​​

保存的pets应额外_id

"pets" : [ 
    { 
     "name" : "o3", 
     "type1" : "t3", 
     "_id" : ObjectId("56a5df9adea3071b0de83407") 
    }, 

以下在这种情况下,我们可以做以下

UserModel.findOne({ firstName: "bob" }, 'pets', function(err, user) { 
    UserModel.update(user, { 
    $push: { 
     pets: { 
      name: "pear", type1: "pig" 
     } 
    } 
    }, function(err, numAffected, raw) { 

并且结果是

"pets" : [ 
    { 
     "name" : "o3", 
     "type1" : "t3", 
     "_id" : ObjectId("56a5df9adea3071b0de83407") 
    }, 
    { 
     "type1" : "pig", 
     "name" : "pear", 
     "_id" : ObjectId("56a5dff3a648301d0db118df") 
    } 
], 
+0

你知道为什么使用'$ push'在这里不起作用吗?我读过'将'type'作为关键字可能会导致问题,但我不认为这是发生在这里的事情。 –

+0

@jackblank,在我的测试模式中,类型为字符串,所以整个目的是推入数组作为对象字符串'[对象的对象]'。 – zangw

3

这里。这是类似的代码,它正在工作。 findbyidandupdate需要有一个“新:真正的”可选PARAM添加到它。否则,你会得到旧的文档返回给你。

var express = require("express"); 
var app = express(); 
var mongoose = require("mongoose"); 

mongoose.connect("mongodb://localhost/population"); 

var db = mongoose.connection; 

db.on("error", console.error.bind(console, "connection error:")); 
db.once("open", function(){ 
    var mongoose = require("mongoose"); 

    var userSchema = new mongoose.Schema({ 
     firstname : String, 
     lastname : String, 
     pets : [] 
    }) 
    var userModel = mongoose.model("User", userSchema); 

    var bob = new userModel({ 
     "firstname" :"bob", 
     "lastname" : "Smith", 
     "pets" : [ 
      {name : "apple", type : "dog"}, 
      {name : "felix", type : "cat"} 
     ] 
    }) 

    bob.save(bob, function(err, user){ 
     console.log(user) 
    }); 

    userModel.findOneAndUpdate(
     {firstname : "bob"}, 
     {$push : {"pets" : {name : "pear", type : "pig"}}}, 
     //THE MOST IMPORTANT PART ADD new : true 
     {safe : true, upsert : true, new : true}, 
     function(err, model){ 
      console.log(model); 
     } 
) 

})//once 


app.listen(3000, function(){ 
    console.log("listening on port: " , 3000) 
})