Search code examples
node.jsmongodbmongoosemongoose-schemamongoose-populate

Mongoose Population not working returning [Object] only not the fields


Hey guys i am trying to fetch some data using mongoose from MongoDB.

it is not even populating the first level .

i will try my best to explain the situation below.

I have my documents structure like below:

Schema[s]

- UsersSchema

_id : { type: mongoose.Schema.Types.ObjectId },    
userData : [{ type: mongoose.Schema.Types.ObjectId, ref: 'DataType' }],


- DataTypeSchema

_id : { type: mongoose.Schema.Types.ObjectId },    
data : [{ type: mongoose.Schema.Types.ObjectId, ref:'Data' }],


- DataSchema

_id : { type: mongoose.Schema.Types.ObjectId },    
desc: String

Filling the data :

  • in User > userData : ["dataTypeID" , "dataTypeID" ]
  • in DataType > data : ["dataID" , "dataID" ]
  • in Data > desc: "pending"

The problem:

i am trying to get the desc field of Data in my User document.

What i tried :

1 try

await User.find().populate('userData').lean();

response : it should populate the DataType model . instead i am looking at [ [Object], [Object] ],

[{
    _id: 5b37aa4638a07505e809191a,
    userData: [ [Object], [Object] ],
    __v: 0
}]

2 try

await User.find().populate('userData.data').lean();

i tried to do some thing which i don't know what it will do :D

response : and now it returns the id's of the DataType model

[{
   _id: 5b37aa4638a07505e809191a,
    userData: [ 5b35577759407235a4293020, 5b355fb5f51a0c1de0a3ab3d ],
    __v: 0
}]

3 try

then after reading the mongoose documentation :

await User.find().populate({
  path: 'userData',
  model: 'User',
  populate: {
    path: 'data',
    model: 'DataType',
  }
}).lean();

response : got and empty array userData : []

[{
 _id: 5b37aa4638a07505e809191a,
 userData: [ ],
 __v: 0
}]

note : if i remove the model: 'User' from the first populate i get the same response as .populate('userData') = [ [Object], [Object] ],

after allot of searching in answers i fond this : mongoose-deep-populate to deep populate which is also working as the normal mongoose populate no effect at all.

it just doesn't make sense to me that why i can't even populate the DataType in the User :(

i hope any one will help me out in this weird kinda situation .

thanks


Solution

  • You were almost going in the right direction with the 3rd try, but you gave the wrong model to populate from.

    This is indeed a problem of nested population or deep population, as you found out, but you need to pass the correct model while populating,

    Since userData is a _id of DataType collection, the model should be DataType and not User, similarly data is a _id of Data collection, the model should be Data not DataType. Make these changes and it should work as expected.

    model in .populate() is to tell mongodb/mongoose which collection to populate it from.

    Try this :

    await User.find().populate({
      path: 'userData',
      model: 'DataType',
      populate: {
        path: 'data',
        model: 'Data',
      }
    }).lean();
    

    Also, Since you have defined Ref in the schema as well, you don't actually need to pass model in the populate, it will automatically know which collection to populate from.

    Below query should work as well:

    await User.find().populate({
      path: 'userData',
      populate: {
        path: 'data'
      }
    }).lean();
    

    Hope this helps!