2012-04-03 12 views
1

私は既にmongoose文書を読みましたが、私はまだ文書の更新方法を理解できません。mongoose updateの使用方法と埋め込みドキュメントの更新方法?

私はノード、エクスプレス、およびmongooseをmongooseで使用します。

これはどのようにiノードが文書を埋め込む更新しない...

{ "title" : "this is a title", 
    "_id" : ObjectId("4f7a92554ad893f322000004"), 
    "nodes" : [ 
       { "title" : "this is node 1", 
        "_id" : ObjectId("4f7a92554ad893f322000009") }, 
       { "title" : "this is node 2", 
        "_id" : ObjectId("4f7a92554ad893f322000008") }, 
       { "title" : "this is node 3", 
        "_id" : ObjectId("4f7a92554ad893f322000007") }, 
       { "title" : "this is node 4", 
        "_id" : ObjectId("4f7a92554ad893f322000006") }, 
       { "title" : "this is node 5", 
        "_id" : ObjectId("4f7a92554ad893f322000005") } 
      ] 
} 

私のMongoDBでコレクションです???

app.put('/api/paper/:pid/:nid', function(req, res) { 
    var PaperModel = mongoose.model('papers', Paper); 
    PaperModel.update({_id: req.params.pid, 'nodes._id': req.params.nid}, {title: 'this   is a new node title'}, function(error, result) { 
     console.dir(result); 
    }); 
}); 

これは機能しません。

どのように私は、埋め込みアレイの「ノード」を更新しない

nodes._id:4f7a92554ad893f322000009タイトル:これは新しいタイトルである:これはタイトルにノード1のですか?

答えて

7
PaperModel.update(
    {_id: req.params.pid, 'nodes._id': req.params.nid} 
    , { $set: {'nodes.$.title': 'this is a new node title'}} 
    , function(error, result) { 
     console.dir(result); 
    } 
); 

これが唯一の時間(MongoDBの制限)に一つのサブ文書を更新することができることに留意されたいです。

関連する問題