Reputation: 41
How do you do nested select in MongoDB analogous to
SELECT id FROM table1 WHERE id IN (SELECT id FROM table2)
Upvotes: 3
Views: 1121
Reputation: 43884
MongoDB does not yet possess the ability to do subqueries which would allow this functionality.
I am sure it is something within the JIRA however, I could not immediately find it.
The only way currently is to actually pick the table, iterate the cursor pulling out the information and using that as part of a $in
query as shown by @Bruno, like so:
ids=[];
for(i in db.c2.find({},{_id:1}){ // I have assumed id=_id
ids[ids.length]=i; // The default return of the index pos is actually _id
}
return db.c1.find({$in:ids});
As a mere example I wrote off of the top of my head.
Upvotes: 3
Reputation: 94
This page contains comparator between SQL and MongoDB:
http://docs.mongodb.org/manual/reference/sql-comparison/
For example, you can to use a aggregation pipeline and where is the same as:
db.your_collection.aggregate({$match: {"id": {$in: ['id_0', 'id_1']} }})
A simple query as the same as:
db.your_collection.aggregate({field: "value"})
Mongodb official page has a lot of informations
Upvotes: 0