How to make nested queries in MongoDb that works like nested Sql select queries

前端 未结 6 1058
不知归路
不知归路 2021-02-05 11:02

I want to make an efficient query in MongoDb to find all users who have their userids listed in a usergroup. Ideally I want to make this as a single request to Mongodb. What I w

6条回答
  •  说谎
    说谎 (楼主)
    2021-02-05 11:48

    define function

    function bbb(){
        var org_ids = new Array();
        var orgs = 
            db.orgTreeNode.find({ancestors:"ca5cd344-ba47-4601-a07b-ea2c684bfb4e"},{"_id":1});
        orgs.forEach(function(org){
            org_ids.push(org._id);
        })
    
        return db.user.find({"org":{$in:org_ids}}).skip(300).limit(10);
    }
    

    execute function

    bbb()
    

提交回复
热议问题