简体   繁体   English

使用MongoDB的NodeJS-尝试使用两个不同的集合数据

[英]NodeJS with MongoDB - Trying to use two different collections data

I have the following code: 我有以下代码:

var res1 = db.collection('col1').find(query1).toArray();
var res2 = db.collection('col2').find(query2).toArray();

                            Promise.all([res1, res2])
                                .then(([resultRes1, resultRes2]) => {
                                    console.log('resultRes1', res1);
                                    console.log('resultRes2', res2);
                                    debugger;
                             }).catch(console.error);

Now, each one of them is working great separately my problem is that i want to run a function when both of them are done. 现在,他们每个人都很好地工作,我的问题是当他们两个都完成时我想运行一个函数。

if i try to nest them i keep getting a "mongodb topology was destroyed" alert (even though all i do is read from those collection - i am not changing anything 如果我尝试嵌套它们,我会不断收到“ mongodb拓扑已被破坏”警报(即使我所做的一切都是从这些集合中读取的-我不会更改任何内容

any ideas? 有任何想法吗? many thanks. 非常感谢。

Post the code where you are trying to run something where both are done. 将代码张贴在您试图运行两者都完成的地方。 Are you trying something like this? 您是否正在尝试这样的事情?

var res1 = db.collection('col1').find(query1);
var res2 = db.collection('col2').find(query2);
Promise.all([res1, res2])
    .then(([resultRes1, resultRes2]) => {
        console.log('resultRes1', resultRes1);
        console.log('resultRes2', resultRes2);
    })
    .catch(console.error);
var collection = db.collection(collection1);
var Res = collection.aggregate([{
    $lookup: {
        from: "collection2",
        localField: "ID",
        foreignField: "ID",
        as: "Sample"
    }
}], function(err, result) {
    if (result != null) {
        for (var i = 0; i < result.length; i++) {
            delete result[i].Sample;
        }
        res.send(result);
    }
})
};

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

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