Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb select with condition is selected result must in sub select query

Tags:

mongodb

How do you do nested select in MongoDB analogous to

SELECT id FROM table1 WHERE id IN (SELECT id FROM table2)
like image 782
user166255 Avatar asked Dec 03 '25 03:12

user166255


1 Answers

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.

like image 65
Sammaye Avatar answered Dec 06 '25 01:12

Sammaye



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!