如何使用mongoose更新,并更新嵌入文档?

cra*_*ngs 1 mongoose mongodb node.js express

我已经阅读过mongoose文档了,我仍然无法弄清楚如何更新文档.

我使用mongoose的node,express和mongodb.

这是我的mongodb中的一个集合......

{ "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" ) }
            ]
}
Run Code Online (Sandbox Code Playgroud)

我如何更新节点嵌入文件???

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);
    });
});
Run Code Online (Sandbox Code Playgroud)

它不起作用.

我如何更新嵌入数组"节点"

nodes._id:4f7a92554ad893f322000009 title:这是节点1到标题:这是一个新标题?

aar*_*ann 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);
    }
);
Run Code Online (Sandbox Code Playgroud)

请注意,这一次只能更新一个子文档(mongodb限制).