2016-01-24 18 views
10

mongodbとmongooseを使用して、$ pushを使って配列に要素を追加することはできません。私がこれを実行した後、私はmongoシェルをチェックインし、新しい追加はそこにありません。ではない私が間違っているのかわから:マングース更新が永続しない

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というキーワードの1つで、Schema Typeのマークに使用されています。

スキーマ以下のようtype1typeを変更した場合は

​​

保存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'を使用するのがなぜ役に立たないのですか?私は、「型」をキーとして問題を引き起こすかもしれないと読んだが、私はそれがここで起こっているとは思わない。 –

+0

@jackblank、私のテストスキーマでは、型はStringなので、オブジェクト全体がオブジェクト文字列 '[object Object]'として配列にプッシュされます。 – zangw

3

ここにあります。これは同様のコードであり、動作しています。 findbyidandupdateには"new:true"が必要です。オプションの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) 
}) 
関連する問題