Javascript Mongoose将两个查询合并到同一个集合中

Javascript Mongoose将两个查询合并到同一个集合中,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我正在尝试进行查询,以查找依赖于同一集合中其他文档的文档,如下所示 第一个查找用户,第二个查找通过使用接收到的用户数据来查找数据。但是我想用一个查询,比如SQL中的join 这是模式 var ConnectionSchema = new Schema({ socketId: { type: String, require: true }, location: { type: [Number], index: '2dsphere' }, user: { type:

我正在尝试进行查询,以查找依赖于同一集合中其他文档的文档,如下所示

第一个查找用户,第二个查找通过使用接收到的用户数据来查找数据。但是我想用一个查询,比如SQL中的join

这是模式

var ConnectionSchema = new Schema({
socketId: {
    type: String,
    require: true
},
location: {
    type: [Number],
    index: '2dsphere'
},
user: { type: Schema.ObjectId, ref: "User" },
date: {
    type: Date,
    require: true,
    default: new Date()
}
}))

//询问

return mongoose.model("Connection").findOne({ user: userId }).populate("user").then(usr => {
    return mongoose.model("Connection").find({
        location: {
            $near: {
                $maxDistance: config.searchDistance,
                $geometry: { type: Number, coordinates: usr.location }
            }
        },
        user: { $ne: userId },
    });
});
只有一个查询就可以做到这一点吗?
谢谢。

是的,你可以这样做

    return mongoose.model("Connection").findOne({ user: userId })
.populate("user" ,
        match : {$and : [{location: {
            $near: {
                $maxDistance: config.searchDistance,
                $geometry: { type: Number, coordinates: usr.location }
            }
          }},
           {user: { $ne: userId }}]})
        .then(usr => {
        // perform your action
    });

只是找到一份文件,不是吗?我正在努力找到他们中的许多人。首先找到具有userId的用户,然后使用此用户的位置进行查询。如果使用userId,则是,因为不同的用户不能具有相同的userId,因此将只有一个文档,或者如果具有相同的userId,则必须将
findOne()
更改为
find()
。但最终此查询将收集用户,而不是连接。