2013-03-04 48 views
-1

我有这样一个模式:如何使用node.js在嵌套模式中插入?

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 
    } 
}); 

我如何可以插入值,以嵌套模式。我怎么能叫SchCommentsNode.JS

+0

如何将值传递给客户端的mongodb读取。 PLZ建议一些答案,以便我可以在工作中取得进展 – pkp 2013-03-04 10:20:09

+0

我需要使用此嵌入式架构将数据插入到mongoDb中。 plz在node.js编码标准 – pkp 2013-03-05 09:04:16

+0

上建议了一些解决方案阅读关于此主题的[documentation](http://mongoosejs.com/docs/subdocs.html),并返回任何您仍有的具体问题。 – JohnnyHK 2013-03-05 12:46:46

回答

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); 

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

相关问题