sql - How to do nested select in MongoDB -
how one, in mongodb, implement following sql statement?
select count(*),t1.a,t1.b t1, t2 (t1.id = t2.t1_id) , (t2.t1_id in (select id t1 order id desc limit 10)) group 2,3 order 1 desc
i figured out how except nested select.
i'm running outer select in loop using "$in"
each value of nested select. java code follows:
basicdblist t1list = new basicdblist(); dbobject inclause = new basicdbobject("$in", t1list); dbobject t2query = new basicdbobject("t1s", inclause); dbobject nextt2; (int query = 0; query < 10; query++) { system.out.printf("running query %d - ", query); dbcursor top_ten_t1s = t1coll.find().sort(new basicdbobject("v", -1)).limit(10); while (top_ten_t1s.hasnext()) { nextt2 = top_ten_t1s.next(); t1list.clear(); t1list.add(new long(nextt2.get("_id").tostring())); int thecount = t2coll.find(t2query).count(); } }
you can't in 1 query in mongodb. you'll need read result of inner query memory first (which sounds you're doing):
var ids = t1.find().sort( { _id: -1 } ).limit(10);
and apply result outer query.
Comments
Post a Comment