title: mongodb join-like query with two collections and a where clause
body: <p>Suppose we have following collections in a database:</p>
  
  <pre><code>db.documents.insert([{'name': 'A'}, {'name': 'B'}, {'name': 'C'}])
  db.fragments.insert([{'value:': 'A1', doc_name: 'A'}, {'value:': 'A2', doc_name: 'A'},
                       {'value:': 'B1', doc_name: 'B'}, {'value:': 'B2', doc_name: 'B'},
                       {'value:': 'C1', doc_name: 'C'}, {'value:': 'C2', doc_name: 'C'}])
  </code></pre>
  
  <p>where <code>documents</code> collection stores the names of the documents (and other stuff omitted in this example), <code>fragments</code> collections refers by <code>doc_name</code> to a document related to the fragment.</p>
  
  <p>Now, if I only want to consider a subset of documents</p>
  
  <pre><code>&gt; db.documents.find().limit(2)
  { "_id" : ObjectId("52d1a3bf49da25160ad6f076"), "name" : "A" }
  { "_id" : ObjectId("52d1a3bf49da25160ad6f077"), "name" : "B" }
  </code></pre>
  
  <p>then how can I see the fragments of associated to these selected documents, so I would get</p>
  
  <pre><code>{ "_id" : ObjectId("52d1a3bf49da25160ad6f079"), "value:" : "A1", "doc_name" : "A" }
  { "_id" : ObjectId("52d1a3bf49da25160ad6f07a"), "value:" : "A2", "doc_name" : "A" }
  { "_id" : ObjectId("52d1a3bf49da25160ad6f07b"), "value:" : "B1", "doc_name" : "B" }
  { "_id" : ObjectId("52d1a3bf49da25160ad6f07c"), "value:" : "B2", "doc_name" : "B" }
  </code></pre>
  
  <hr>
  
  <p>As a solution, I was thinking that I should store the document names in an array, something like <code>var docnames = ???</code> such that</p>
  
  <pre><code>&gt; docnames
  [ "A", "B" ]
  </code></pre>
  
  <p>and then trying to use this array in a where clause, something like</p>
  
  <pre><code>&gt; db.fragments.find({$where: function(x) { return (this.doc_name in docnames)}})
  error: {
          "$err" : "ReferenceError: docnames is not defined near 'c_name in docnames)}' ",
          "code" : 16722
  }
  </code></pre>
  
  <p>But as I am very new to mongodb, so I am having trouble figuring it out. I believe this could be done as a one-liner as well.</p>