1

Currently I have three Collection "User" , "UserLocation" & "UserAddress" which are related with each other.

Below are the Schema

UserModel

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

module.exports = mongoose.model('user', userTable );

UserLocationModel

var userLocationTable = new Schema({
  location : String,
  user_id :{
    type : ObjectId,
    ref : 'user',
  }
});

module.exports = mongoose.model('userLocation',userLocationTable);

UserAddressModel

var userAddressTable = new Schema({
    userLocationID : {
      type : ObjectID,
      ref : 'userLocation',
    },
   address : String
});

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

My Code

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);
    })

Current Output

[
{
    "_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": []
    }
}
]

Here the problem is I am not able to fetch the data from UserAddress Model. I have tried all the possible ways but unable to fetch the data from UserAddress Collection. Can someone help me in this?

  • you have userLocationID saved in UserAddressModel but in lookup you're using "foreignField": "userLocation", not "foreignField": "userLocationID", – sushant mehta Sep 27 '19 at 09:13
  • @sushantmehta I have edited the question and updated userLocation to userLocationID still i am not able to fetch – Shashikant Maurya Sep 27 '19 at 09:16
  • can you try after changing userAddresss to userAddresses in lookup.from – sushant mehta Sep 27 '19 at 09:18
  • Updated the question and tried but still nothing happen – Shashikant Maurya Sep 27 '19 at 09:22
  • make it useraddresses as mongo saves collection in lowercase and plural – sushant mehta Sep 27 '19 at 09:39
  • 2
    MongoDB does not *"save in lowercase", `mongoose` does. That's an important distinction to understand. It's also a basic principle of the "how to join" which has been explained many times before. The **collection name** needs to be exactly what MongoDB expects it to be **on the server** in order to actually do anything. BTW, you appear to have based your code on a *very* old post. `$lookup` has far more effective ways to implement *chained joins* without needing `$unwind. See the second marked duplicate. – Neil Lunn Sep 27 '19 at 10:16

0 Answers0