Mongodb 使用Mongoose中的查找从多个集合获取数据时出现问题

Mongodb 使用Mongoose中的查找从多个集合获取数据时出现问题,mongodb,mongoose,mongodb-query,aggregation-framework,lookup,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,Lookup,目前我有三个相互关联的集合“User”、“UserLocation”和“UserAddress” 下面是模式 用户模型 var userTable = new Schema({ name: String, email : String }); module.exports = mongoose.model('user', userTable ); var userLocationTable = new Schema({ location : String, user_id :

目前我有三个相互关联的集合“User”、“UserLocation”和“UserAddress”

下面是模式

用户模型

var userTable = new Schema({
  name: String,
  email : String
});

module.exports = mongoose.model('user', userTable );
var userLocationTable = new Schema({
  location : String,
  user_id :{
    type : ObjectId,
    ref : 'user',
  }
});

module.exports = mongoose.model('userLocation',userLocationTable);
var userAddressTable = new Schema({
    userLocationID : {
      type : ObjectID,
      ref : 'userLocation',
    },
   address : String
});

module.exports = mongoose.model('userAddress',userAddressTable);
UserLocationModel

var userTable = new Schema({
  name: String,
  email : String
});

module.exports = mongoose.model('user', userTable );
var userLocationTable = new Schema({
  location : String,
  user_id :{
    type : ObjectId,
    ref : 'user',
  }
});

module.exports = mongoose.model('userLocation',userLocationTable);
var userAddressTable = new Schema({
    userLocationID : {
      type : ObjectID,
      ref : 'userLocation',
    },
   address : String
});

module.exports = mongoose.model('userAddress',userAddressTable);
UserAddressModel

var userTable = new Schema({
  name: String,
  email : String
});

module.exports = mongoose.model('user', userTable );
var userLocationTable = new Schema({
  location : String,
  user_id :{
    type : ObjectId,
    ref : 'user',
  }
});

module.exports = mongoose.model('userLocation',userLocationTable);
var userAddressTable = new Schema({
    userLocationID : {
      type : ObjectID,
      ref : 'userLocation',
    },
   address : String
});

module.exports = mongoose.model('userAddress',userAddressTable);
我的代码

UserModel.aggregate([
        {
            "$lookup": {
                "from": "userlocations",
                "localField": "_id",
                "foreignField": "user_id",
                "as": "location"
            }
        },
        {
            "$unwind" : "$location"
        },
        {
            "$lookup": {
                "from": "useraddresses",  
                "localField": "location._id",
                "foreignField": "userLocationID",
                "as": "location.address"
            }
        },
    ]).exec((err, data) => {
        if (err) throw err;
        res.send(data);
    })
电流输出

[
{
    "_id": "5d8b1e6ca4d1ab5e54f1b8ee",
    "name": "Shivam",
    "email": "ee@gmail.com",
    "__v": 0,
    "location": {
        "_id": "5d8b1e6ca4d1ab5e54f1b8ef",
        "location": "Mumbai",
        "user_id": "5d8b1e6ca4d1ab5e54f1b8ee",
        "__v": 0,
        "address": []
    }
},
{
    "_id": "5d8b3a740b6fee55ac39a61d",
    "name": "ameet",
    "email": "ddt@gmail.com",
    "__v": 0,
    "location": {
        "_id": "5d8b3a740b6fee55ac39a61e",
        "location": "sion",
        "user_id": "5d8b3a740b6fee55ac39a61d",
        "__v": 0,
        "address": []
    }
}
]

这里的问题是我无法从UserAddress模型中获取数据。我已经尝试了所有可能的方法,但无法从UserAddress集合中获取数据。有人能帮我吗?

您在UserAddressModel中保存了userLocationID,但在查找中使用的是“foreignField”:“userLocation”,而不是“foreignField”:“userLocationID”,@sushantmehta我已经编辑了问题并将userLocation更新为userLocationID,但我仍然无法获取。在查找中将userAddresss更改为userAddresses后,您是否可以尝试。From更新了问题并尝试了,但仍然没有任何结果MongoDB没有“以小写形式保存”,mongoose有。这是一个需要理解的重要区别。这也是“如何加入”的一个基本原则,之前已经解释过很多次了。集合名称必须与MongoDB希望它在服务器上的名称完全一致,以便实际执行任何操作。顺便说一句,您的代码似乎基于一篇非常古老的文章
$lookup
有更有效的方法来实现链式连接,而不需要“$unwind”。请参阅第二个标记为重复的。您在UserAddressModel中保存了userLocationID,但在查找中使用的是“foreignField”:“userLocation”,而不是“foreignField”:“userLocationID”,@sushantmehta我已经编辑了问题并将userLocation更新为userLocationID,但我仍然无法获取。在查找中将userAddresss更改为userAddresses后,您是否可以尝试。From更新了问题并尝试了,但仍然没有任何结果MongoDB没有“以小写形式保存”,mongoose有。这是一个需要理解的重要区别。这也是“如何加入”的一个基本原则,之前已经解释过很多次了。集合名称必须与MongoDB希望它在服务器上的名称完全一致,以便实际执行任何操作。顺便说一句,您的代码似乎基于一篇非常古老的文章
$lookup
有更有效的方法来实现链式连接,而不需要“$unwind”。请参阅第二个标记为重复的项目。