title: MongoDB: Find an object by its ID without knowing the collection
body: <p>I have an mongodb databse with 100+ collections. I'm trying to find an object, with a known ObjectID, that belongs to some (unknown) collection of this database.</p>
  
  <p>I tried to do:</p>
  
  <pre><code>db.getCollectionNames().forEach(function(collname) {
      var object = db[collname].find({'_id' : ObjectId("54d0232ef83ea4000d2c0610")});
      if(object._id !== undefined){
          printjson("Found in " &gt;&gt; collname);
      }
  });
  </code></pre>
  
  <p>... similar to what's suggested here: <a href="https://stackoverflow.com/questions/25543452/loop-through-all-mongo-collections-and-execute-query">Loop through all Mongo collections and execute query</a></p>
  
  <p>However, I am getting no results from the script.</p>
  
  <p>Edit:</p>
  
  <p>When I do this I get the expected <code>Found!</code>:</p>
  
  <pre><code>var object = db['rightcollection'].findOne({'_id' : ObjectId("54d0232ef83ea4000d2c0610")});
  if(object !== null){
      printjson("Found!");
  }
  </code></pre>
  
  <p>But the following returns <code>0</code> (instead of returning nothing as in the original example):</p>
  
  <pre><code>db.getCollectionNames().forEach(function(collname) {
      var object = db[collname].findOne({'_id' : ObjectId("54d0232ef83ea4000d2c0610")});
      if(object !== null){
          printjson("Found in " &gt;&gt; collname);
      }
  });
  </code></pre>