MongoDB:使用引用查询数组

时间:2015-04-27 11:24:11

标签: mongodb

我是MongoDB的新手,我遇到了问题。让我们假设我有以下文件:

<form class="form-horizontal" id="validationForm" onsubmit="return $('#validationForm').valid()" method="post" runat="server">

我想要一个查询,显示用户John Smith已经流式传输的电影。可以看出,引用嵌入在数组中(在流中)。有人知道怎么做吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您可以使用游标方法forEach()来迭代游标并访问文档,如下例所示:

var firstName = "John",
    lastName = "Smith";

db.users.find({"forname" : firstName, "surname" : lastName}).forEach( function(doc){
    doc.streaming.forEach(function (stream){
        var movie = db.media_content.findOne({"_id": stream.media_content_id});
        print(firstName + " " + lastName + " watched " + movie.content_name + " for " + stream.stream_min + " minutes");
    });
});

输出

John Smith watched FILM B for 45 minutes 
John Smith watched FILM B for 45 minutes