如何使用node.js在嵌套模式中插入?

时间:2013-03-04 09:19:59

标签: jquery performance node.js mongodb mongoose

我有一个类似的架构:

var SchComments = new Schema({
    title     : String
  , body      : String
  , date      : Date
});


var BlogPost = new Schema({
    author    : ObjectId
  , title     : String
  , body      : String
  , date      : Date
  , comments  : [SchComments ]
  , meta      : {
        votes : Number
      , favs  : Number
    }
});

如何将值插入嵌套模式。如何调用SchComments来传递Node.JS

中的值

1 个答案:

答案 0 :(得分:0)

要向帖子的comments数组添加元素,您可以在数组上调用push,然后保存更改:

var post = new BlogPostModel({});
post.comments.push({title: 'a', body: 'b', date: new Date()});
post.save(callback);

或在update中使用$push运算符:

BlogPostModel.update({}, {$push: {comments: {
    title: 'a2', body: 'b2', date: new Date()
}}}, callback);

还有其他方法,但这些是最典型的。

相关问题