Tuesday, 9 May 2017

Mongoosejs populating model with schema object ids in array

I have a circle model in my project:

var circleSchema = new Schema({
    patientID: {type: Schema.Types.ObjectId, ref: "patient"},
    circleName: String,
    caregivers: [{type: Schema.Types.ObjectId, ref: "caregiver"}],
    accessLevel: Schema.Types.Mixed //entire circle will have single permission
});

Sample Object:

{ 
    "_id" : ObjectId("58cf4832a96e0e3d9cec6918"), 
    "patientID" : ObjectId("58fea8ce91f54540c4afa3b4"), 
    "circleName" : "circle1", 
    "caregivers" : [
        ObjectId("58fea81791f54540c4afa3b3"), 
        ObjectId("58fea7ca91f54540c4afa3b2")
    ], 
    "accessLevel" : {
        "location\"" : true, 
        "notes" : false, 
        "vitals" : true
    }
}

How do i populate the caregivers property. Here is the code I have tried. It does not populate

circle.find({"patientID": req.user._id}).populate('caregivers').exec(function(err, items){
        if(err){console.log(err); return next(err) }
        res.json(200,items);
    });

I am only getting the object id's in the result. It is not getting populated.



via Himanshu Jain

No comments:

Post a Comment