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

前端 未结 6 1054
不知归路
不知归路 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:52

    If it can trigger to get you ans--

    db.users.find({
        _id: {
            $in: db.logs.find({
                loggedbyuser: {
                    $ne: ObjectId("569f9d093447ee781ca80b52")
                },
                logtype: "marketfetched",
                "logcreated": {
                    $gt: new ISODate("2016-02-06T00:00:00.871Z")
                }
            }, {
                loggedbyuser: 1,
                _id: 0
            }).sort({
                'logcreated': -1
            }).map(function(like) {
                return like.loggedbyuser;
            })
        }
    }).map(function(like) {
        return like.fullname;
    });
    

提交回复
热议问题