简体   繁体   English

如何使用Node.js在MongoDb中查询“加入”集合?

[英]How to query a 'join' collection in MongoDb using Node.js?

I'm using Node.js and mongoose to query a mongodb database. 我正在使用Node.js和mongoose查询mongodb数据库。 It is very simple scenario but I cannot figure out how to do it properly. 这是一个非常简单的场景,但是我无法弄清楚如何正确地做它。

I have a collection to query and then given the result, need to fetch object from another collection to populate the result. 我有一个要查询的集合,然后给出结果,需要从另一个集合中获取对象以填充结果。 code sample as below: 代码示例如下:

q.exec( function (e, docs){
    if (e || !docs) {
        res.status(400);
        res.send("Error while processing request.\n"+ JSON.stringify(e, null, 4));
    }else{
         //do the JOIN here.
         var result = _(docs).map(
            function(doc){
                markModel.findOne(
                   {question_id: doc._id, user_id: user_id},
                   function (err, markDoc){
                     if (markDoc)
                        doc.mark = markDoc;
                   });
              //HOW and what to return here??
            });
         res.send(result);
    }
});

In the code above, docs is the result of the first query; 在上面的代码中, docs是第一个查询的结果; and then I need to find the mark from another collection for each of the doc in docs , and attach it to the doc object. 然后我需要从docs每个doc另一个集合中找到mark ,并将其附加到doc对象。 Because the second query is also asyn called, I don't know what should be returned in the map() function, without using defer or promise, etc. 因为第二个查询也被asyn调用,所以我不知道在不使用defer或promise等的情况下应该在map()函数中返回什么。

MongoDB is a non-relational database and doesn't support joins. MongoDB是非关系数据库,不支持联接。
For this needs please check data models documentation . 为此,请检查数据模型文档
Also look at practice of using MapReduce : http://cookbook.mongodb.org/patterns/pivot/ 还要看看使用MapReduce实践: http : //cookbook.mongodb.org/patterns/pivot/

Node.js behaviors is asynchronous in which callbacks will be executed after getting result set . Node.js行为是异步的,在获取结果集后将执行回调。 In your code you are doing multiple call to mongodb , which can be avoided by using $in construct . 在您的代码中,您正在对mongodb进行多次调用,可以通过使用$ in构造避免这种情况。 Doing multiple mongo call in loop is bad design where you can't ensure when to return or beak loop , because you don't know when call back will be called . 在循环中进行多个mongo调用是很糟糕的设计,因为您不知道什么时候会调用回调,所以您无法确定什么时候返回或进行喙循环。 To avoid this just collect all doc _id and do one mongo call using $in construct . 为了避免这种情况,只需收集所有doc _id并使用$ in结构进行一个mongo调用。 This will avoid the confusion of when to send response .Something like : 这样可以避免发送响应时间的混乱。

q.exec(function (e, docs) {
        if (e || !docs) {
            res.status(400);
            res.send("Error while processing request.\n" + JSON.stringify(e, 
                                                                 null, 4));
        } else {
            //do the JOIN here.     
            var docId = _(docs).map(
                function (doc) {
                    return parseInt(doc._id);
                });
            markModel.findOne({
                    question_id: {
                        $in: docId
                    },
                    user_id: user_id
                },
                function (err, markDoc) {
                    if (markDoc)
                        doc.mark = markDoc;
                    res.send(result); // send result
                });


        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM