title: NodeJS/Mongo: Looping a query through various collections
body: <p>I'm looking to loop a query through various collection with MongoDB using the <a href="http://mongodb.github.io/node-mongodb-native/2.2/api/index.html" rel="noreferrer">NodeJS Driver</a>.
  For this test, I've used the <a href="http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#findOne" rel="noreferrer">sample code from the 'findOne' docs</a> to insert a bunch of documents in various Collections: </p>
  
  <pre><code>  collection.insertMany([{a:1, b:1}, {a:2, b:2}, {a:3, b:3}], {w:1}, function(err, result) {
      test.equal(null, err);
  </code></pre>
  
  <p>Creating at the same time various collections (each collection has at least one instance of the documents previously inserted): </p>
  
  <ul>
  <li>test</li>
  <li>test1</li>
  <li>test2</li>
  <li>test3</li>
  <li>test4</li>
  <li>test6</li>
  <li>test10</li>
  </ul>
  
  <p>And what I want is to gather the list of collection that I have in the DB (<code>'test'</code> in my case):</p>
  
  <pre><code>var MongoClient = require("mongodb").MongoClient,
    test = require("assert");
  MongoClient.connect("mongodb://localhost:27017/test", function(err, db) {
    db.listCollections().toArray(function(err, items) {
      test.ok(items.length &gt;= 1);
      console.log(items);
      db.close();
    });
  });
  </code></pre>
  
  <p>And there pops the list of collection previously mentioned. Up until now everything is all-right! I can even loop through the array to get the name of the collections only: </p>
  
  <pre><code>var MongoClient = require("mongodb").MongoClient,
    test = require("assert");
  MongoClient.connect("mongodb://localhost:27017/test", function(err, db) {
    db.listCollections().toArray(function(err, items) {
      test.ok(items.length &gt;= 1);
      items.forEach(c =&gt; {
        console.log(c.name);
      });
      db.close();
    });
  });
  </code></pre>
  
  <p>Again no problem there! But when I then try a query within the loop: </p>
  
  <pre><code>var MongoClient = require("mongodb").MongoClient,
    test = require("assert");
  MongoClient.connect("mongodb://localhost:27017/test", function(err, db) {
    db.listCollections().toArray(function(err, items) {
      test.ok(items.length &gt;= 1);
      items.forEach(c =&gt; {
        var collection = db.collection(c.name);
        collection.findOne({ a: 2 }, { fields: { b: 1 } }, function(err, doc) {
          console.log(doc);
        });
      });
    });
    db.close();
  });
  </code></pre>
  
  <p>I get: </p>
  
  <pre><code>null
  null
  null
  null
  null
  null
  null
  </code></pre>
  
  <p>Even though looping through to get the collection seems to work perfectly fine: </p>
  
  <pre><code>var MongoClient = require("mongodb").MongoClient,
    test = require("assert");
  MongoClient.connect("mongodb://localhost:27017/test", function(err, db) {
    db.listCollections().toArray(function(err, items) {
      test.ok(items.length &gt;= 1);
      items.forEach(c =&gt; {
        var collection = db.collection(c.name);
        console.log(collection);
        });
      });
    db.close();
  });
  </code></pre>
  
  <p>Example output: </p>
  
  <pre><code>Collection {
    s: 
     { pkFactory: 
        { [Function: ObjectID]
          index: 10866728,
          createPk: [Function: createPk],
          createFromTime: [Function: createFromTime],
          createFromHexString: [Function: createFromHexString],
          isValid: [Function: isValid],
          ObjectID: [Circular],
          ObjectId: [Circular] },
       db: 
        Db {
          domain: null,
          _events: {},
          _eventsCount: 0,
          _maxListeners: undefined,
          s: [Object],
          serverConfig: [Getter],
          bufferMaxEntries: [Getter],
          databaseName: [Getter] },
       topology: 
        Server {
          domain: null,
          _events: [Object],
          _eventsCount: 8,
          _maxListeners: undefined,
          clientInfo: [Object],
          s: [Object] },
       dbName: 'test',
       options: 
        { promiseLibrary: [Function: Promise],
          readConcern: undefined,
          readPreference: [Object] },
       namespace: 'test.test2',
       readPreference: 
        ReadPreference {
          _type: 'ReadPreference',
          mode: 'primary',
          tags: undefined,
          options: undefined },
       slaveOk: true,
       serializeFunctions: undefined,
       raw: undefined,
       promoteLongs: undefined,
       promoteValues: undefined,
       promoteBuffers: undefined,
       internalHint: null,
       collectionHint: null,
       name: 'test2',
       promiseLibrary: [Function: Promise],
       readConcern: undefined } }
  </code></pre>
  
  <p>I'm guessing that the <code>Collection</code> structure is the problem for my loop but I'm not sure what's happening exactly...
  This is an example of the expected output for each Collection: </p>
  
  <pre><code>{ _id: 5a13de85a55e615235f71528, b: 2 }
  </code></pre>
  
  <p>Any help would be much appreciated! Thanks in advance! </p>