1

In other words, how can I chain MongoDB .find()s so that the second .find() searches the cursor returned by the first .find()?

MySQL has subquery support.

Community
  • 1
  • 1
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
  • No you cannot chain a `.find()` operation from an existing cursor. But what is your actual use case? Like most things "noSQL" there is usually a different answer. – Neil Lunn May 14 '14 at 07:08
  • @NeilLunn: my use case is unfortunately overcoming a [bug in the MongoDB shell that prevents using certain characters in a regexp character class](https://jira.mongodb.org/browse/SERVER-13934). – Dan Dascalescu May 14 '14 at 10:44
  • 1
    Then perhaps you could actually add all of that detail to your question, then people can truly understand why the question you have linked to does not actually meet your requirements. There is only one sentence essentially in your question and it does not describe what you are actually trying to do. – Neil Lunn May 14 '14 at 10:48

1 Answers1

1

Subqueries and joins are a very 'relational' thing, so you might want to reconsider your design. In any case, you can't join collections directly, but you can use $in, e.g.:

> foo = [];
[ ]
> db.Comment.find().forEach(function(rover) {foo.push(rover.UserId)})
> foo
[
        ObjectId("535fd8e6eb596a27ec924d15"),
        ObjectId("536a6479eb596a2a283f43e8"),
        ObjectId("536a5fa4eb596a2a283f43de")
]
> db.User.find({"_id" : {$in : foo}});
{ "_id" : ObjectId("535fd8e6eb596a27ec924d15"), "FirstName" : "John", ...
mnemosyn
  • 45,391
  • 6
  • 76
  • 82