mongodb:如何获取包含嵌套字段数据的文档

时间:2013-09-03 05:52:31

标签: node.js mongodb

假设我的数据是

db.posts.save({postid:1,postdata:"hi am ",comments:["nice","whats bro"]});

所以在这种情况下如何迭代评论 装置

cursor=selct * from posts;
cursor 1=selct * from comments where postid=:cursor.postid
for (i in cursor)
for(j in cursor1)

1 个答案:

答案 0 :(得分:1)

迭代评论的一种方法如下:

db.posts.find().forEach(
  function(doc) {
    // iterate over the comments in your preferred javascript way
    var i;
    for (i=0; i<doc.comments.length; ++i) {
      // Do whatever you want with doc.comments[i] here
    }
  }
)