Can't get the result of Mongoose virtual populate
Asked Answered
A

2

27

I have a circle model in my project:

var circleSchema = new Schema({
    //circleId: {type: String, unique: true, required: true},
    patientID: {
        type: Schema.Types.ObjectId,
        ref: "patient"
    },
    circleName: String,
    caregivers: [{
        type: Schema.Types.ObjectId
    }],
    accessLevel: Schema.Types.Mixed
});

circleSchema.virtual('caregiver_details', {
    ref: 'caregiver',
    localField: 'caregivers',
    foreignField: 'userId'
});

caregiver schema:

var caregiverSchema = new Schema({
    userId: {
        type: Schema.ObjectId,
        unique: true
    }, //objectId of user document
    detailId: {
        type: Schema.ObjectId,
        ref: "contactDetails"
    },
    facialId: {
        type: Schema.ObjectId,
        ref: "facialLibrary"
    }, //single image will be enough when using AWS rekognition
    circleId: [{
        type: Schema.Types.ObjectId,
        ref: "circle"
    }], //multiple circles can be present array of object id
});

Sample Object:

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

I have tried virtual populate for Mongoose but I am unable to get it to work. This seems to be the exact same problem: https://github.com/Automattic/mongoose/issues/4585

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 ids in the result. It is not getting populated.

Antiscorbutic answered 10/5, 2017 at 2:16 Comment(11)
Hello. Can you please show code of caregiver schema?Seabolt
One note: you must add ref property here: userId: {type: Schema.ObjectId, unique: true},Seabolt
@Seabolt didn't really get you. can you please elaborate.Antiscorbutic
I mean you forgot about ref property: userId: {type: Schema.ObjectId, unique: true, ref: 'user'}Seabolt
@Seabolt that did not work either. not sure what is happening. :(Antiscorbutic
Let us continue this discussion in chat.Antiscorbutic
Did you try example from docs: mongoosejs.com/docs/populate.htmlSeabolt
I have made a big mistake. Referencing refers to the _id. Whereas the records I have in circle schema have userId object ids. Is there a way to reference that?Antiscorbutic
The virtual populate seems to be the solution. Although, I can still not get it to work.Antiscorbutic
This seems to be the exact issue. but does not work for me: github.com/Automattic/mongoose/issues/4585Antiscorbutic
I also have the same. this link: #63484001Crowley
A
83

Figured out what the problem was. By default, the virtual fields are not included in the output. After adding this in circle schema:

circleSchema.virtual('caregiver_details',{
    ref: 'caregiver',
    localField: 'caregivers',
    foreignField: 'userId'
});

circleSchema.set('toObject', { virtuals: true });
circleSchema.set('toJSON', { virtuals: true });

It now works perfectly.

Antiscorbutic answered 10/5, 2017 at 20:14 Comment(2)
YEEEEES! Thank you! It took me days to finally figure this out. Virtual fields are not included as clearly stated in the docs. Somehow I only remembered this about toJSON -- but I didn't see it for toObject even though it's on the same line.Infantile
I've had this crazy problem as well! just spent a few hours on this shit! Thanks for saving it Buddy.Uitlander
P
2

If you use expressJs res.json method it's ehough to add only:

yourSchema.set('toJSON', { virtuals: true });

Or you can directly use toJSON/toObject:

doc.toObject({ virtuals: true })) // or doc.toJSON({ virtuals: true }))
Pythia answered 31/12, 2019 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.