MongoDB: querying multiple collections with two queries?

I have two posts collections and authors . The mail document contains a DBref link for the author _id next to the mail data . My collection looks like this:

posts

  "_id" : ObjectId("4fa12443d3269e98070013b4"),
  "author" : {
    "$ref" : "authors",
    "$id" : ObjectId("4fa1242bd3269e9807000023")
  },
  "post" : " mi eleifend egestas. Sed pharetra, felis eget varius ultrices, mauris ipsum porta elit, a feugiat tellus lorem eu metus. In lorem.",
  "post_title" : "Volutpat. Nulla facilisis. Suspendisse commodo tincidunt nibh. Phasellus nulla. Integer",
  "date" : 1293803276,
  "rating" : 8,
  "comments" : [{
      "name" : "Doris Turner",
      "email" : "Hedda_Herman@.com",
      "upVotes" : 81,
      "downVotes" : 93,
      "comment_date" : 1111395830,
      "comment" : "consectetuer ipsum nunc id enim. Curabitur massa. Vestibulum accumsan neque et nunc. Quisque ornare tortor at"
    }, {
      "name" : "Brenda Tyler",
      "upVotes" : 1,
      "downVotes" : 73,
      "comment_date" : 940325674,
      "comment" : "cursus purus. Nullam scelerisque neque sed sem egestas blandit. Nam Nulla aliquet. Proin velit. Sed malesuada augue ut lacus. Nulla tincidunt, neque vitae semper egestas, urna justo faucibus lectus, a sollicitudin orci sem eget massa."}],
  "tags" : ["tag1", "tag3"]
}  

And my collection of authors looks like this:

authors

{
  "_id" : ObjectId("4fa1242bd3269e9807000016"),
  "name" : "Kristina Chung ",
  "email" : "curran_ramos@google.co.id\r\n.dk",
  "age" : 60,
  "city" : "Copenhagen"
}

I’m trying to create a “relational” query to find: Messages with a rating of more than 6 and less than 9, and the age of the author of the message is more than 19 and less than 25.

I am trying to collect this but cannot get the correct data.

The first request is in my message collection and looks like this:

$query = array('rating' => array('$gte' => 6, '$lt' => 9), 'comments.upVotes' => array('$gt' => 2, '$lt' => 20));

I choose the author’s field. $ id, which is a link to a collection of authors.

Then I put all $ id in the array as such:

while ($cursor->hasNext()): $document = $cursor->getNext();
    $authorID[] = $document['_id'];
endwhile;

$query2 = array('age' => array('$gte' => 19, '$lt' =>100), '_id' => array('$in' => $authorID));
$cursor = q('author')->find($query2);

: $ count = $cursor- > count(); , , 0.

?

, , .

, - .


-

+3
2

"" " MongoDB . ( ) ( ) , MongoDB . (, , ), .

, , '$ in'. JavaScript mongo:

> var authorList = [];
> var authorCursor = db.authors.find({age:{$gt:19,$lt:25}},{"_id":1});
> while(authorCursor.hasNext()){authorList.push(authorCursor.next()["_id"])};
> db.posts.find({"author.$id":{$in:authorList},rating:{$gt:6,$lt:9}});

. , _id . author _id s. , : author _id , , .

+5

, , , , , toArray():

u = db.authors.find({"isActive":false}).toArray()
idlist = []
u.forEach(function(myDoc) { idlist.push(myDoc.ID ); } )
db.posts.find({"AuthorID": {$in : idlist} } )


u = db.authors.find({"isActive":false})
idlist2 = []
while (u.hasNext()) {idlist2.push(u.next()["ID"])};
idlist.forEach(function(myDoc) { 
   if ( idlist2.indexOf(myDoc) == -1 ) 
       {print (myDoc)} 
})

20 !

+1

All Articles