简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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