SOUser
SOUser

Reputation: 610

MongoDB nested collection query of same collection

How do you do a nested query when you need the results of the same collection in the next query?

var mongo = require('../config/mongo');

var mongoDB = mongo.db;
...

exports.myFunction = function(req, res) {

...
...
// e.g. myArray = ['a','b','c'];

mongoDB.collection('MyCollection', function(err, collection) {
    collection.find({ $or: [{ 'source': {$in: myArray} },{ 'target': {$in: myArray} }]}, { "someVar": 0}).toArray(function(err, firstResults) {

            var allResults = [];
            for (var i = 0; i < firstResults.length; i++) {
                allResults[firstResults[i].source]=1;
                allResults[firstResults[i].target]=1;
            };

            var secondResults = Object.keys(allResults);

            mongoDB.collection('MyCollection', function(err, collection) {
                collection.find({ $or: [{ 'source': {$in: secondResults} },{ 'target': {$in: secondResults} }]}, { "someVar": 0}).toArray(function(err, items) {
                    res.send(items);
                });
            });


    });
});

But it doesn't like that I am calling the same collection 'MyCollection' twice. I'm trying to get any document whose source or target involves secondResults.

Upvotes: 0

Views: 2293

Answers (1)

JRP
JRP

Reputation: 989

You should get rid of the second call to open 'MyCollection'. You already have a 'MyCollection' object after opening it once. Namely, you have this:

mongoDB.collection('MyCollection', function(err, collection) {
  collection.find({ $or: [{ 'source': {$in: secondResults} },{ 'target': {$in: secondResults} }]}, { "someVar": 0}).toArray(function(err, items) {
    res.send(items);
   });
});

but you already have an instance of 'MyCollection' from here:

mongoDB.collection('MyCollection', function(err, collection) {
  collection.find({ $or: [{ 'source': {$in: myArray} },{ 'target': {$in: myArray} }]}, { "someVar": 0}).toArray(function(err, firstResults) {

So since the second call to 'collection' is in the scope of the first call, you can (and should) reuse that instance:

collection.find({ $or: [{ 'source': {$in: secondResults} },{ 'target': {$in: secondResults} }]}, { "someVar": 0}).toArray(function(err, items) {
  res.send(items);
});

Alternatively, if you're using node-mongodb-native, you can have a look at some of the examples to see other ways of accomplishing what you're after.

UPDATE 1

This code works for me:

var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db){
  if(err) throw err;

  var collection = db.collection('c');
  var secondQuery = {};

  collection.find({a:'b'}).toArray(function(err, firstRes){
    for(var i = 0; i < firstRes.length; i++){
      secondQuery[firstRes[i].a] = firstRes[i].b;
    }

    collection.find(secondQuery).toArray(function(err, secondRes){
      for(var i = 0; i < secondRes.length; i++){
        console.log(secondRes[i]);
      }

      db.close();
    });
  });
});

UPDATE 2

Note that the above code is using node-mongodb-native version 1.3.19. Since version 1.2, it is recommended to use MongoClient when interacting with mongodb from Node.js driver.

Upvotes: 5

Related Questions