-1

我有一个像这样的模式:

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

4

1 回答 1

0

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

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

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

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

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

于 2013-03-13T13:17:59.347 回答