mongodb-js / mongoose-autopopulate

Always populate() certain fields in your mongoose schemas
Apache License 2.0
221 stars 36 forks source link

Auto Populate works for one key in document but not in another #57

Closed mrwadepro closed 4 years ago

mrwadepro commented 4 years ago

I have the following Schema

const ClassSchema = new Schema({
  students: {
    numOfStudents: { type: Number, default: 0 },
    studentList: [
      {
        type: Schema.Types.ObjectId,
        ref: "User",
        autopopulate: { select: "name profile profile_type account_type" }
      }
    ]
  },
  mentors: {
    numOfMentors: { type: Number, default: 0 },
    mentorList: [
      [
        {
          type: Schema.Types.ObjectId,
          ref: "User",
          autopopulate: { select: "name profile profile_type account_type" }
        }
      ]
    ]
  },
});
module.exports = Class = mongoose.model(
  "Class",
  ClassSchema.plugin(require("mongoose-autopopulate"))
);

And within the Document I am testing against I have the following:

 "students" : {
        "numOfStudents" : NumberInt(1), 
        "studentList" : [
            ObjectId("5c9b828e347bb645e0865257")
        ]
    }, 
    "mentors" : {
        "numOfMentors" : NumberInt(1), 
        "mentorList" : [
            ObjectId("5c9ba636347bb645e0865283")
        ]
    }

and when I use the following query:

 const query = {
          "students.studentList": { $in: req.user._id }
        };
        Class.find(query)

I get the following response:

[
    {
        "students": {
            "numOfStudents": 1,
            "studentList": [
                {
                    "_id": "5c9b828e347bb645e0865257",
                    "name": "Student",
                    "account_type": "student",
                    "profile": {
                        "profile_picture": {
                            "url": "https://picture.jpeg",
                            "originalname": "download (2).jpeg",
                            "mimetype": "application/jpeg",
                            "blobName": "profile-picture-7eb5e4b6-a1ad-4bbf-880b-02540d709a86.jpeg",
                            "container": "stemuli",
                            "blob": "profile-picture-7eb5e4b6-a1ad-4bbf-880b-02540d709a86.jpeg",
                            "size": "6136",
                            "etag": "\"0x8D75918FB547DD0\"",
                            "createdOn": "2019-11-08T04:03:54.746Z"
                        },
                        "_id": "5c9b828e347bb645e086525a"
                    },
                    "profile_type": "StudentProfile"
                }
            ]
        },
        "mentors": {
            "numOfMentors": 1,
            "mentorList": [
                [
                    "5c9ba636347bb645e0865283"
                ]
            ]
        }
}
]

I assumed maybe I messed something up here and the user in the mentors.mentorList with the _id : 5c9ba636347bb645e0865283 didn't have the correct references. So, since I know that the student was populating, I moved copied the student into the mentors.mentorList.

So, now it would look like this:

"students" : {
        "numOfStudents" : NumberInt(1), 
        "studentList" : [
            ObjectId("5c9b828e347bb645e0865257")
        ]
    }, 
    "mentors" : {
        "numOfMentors" : NumberInt(1), 
        "mentorList" : [
            ObjectId("5c9b828e347bb645e0865257")
        ]
    }

Weirdly enough, I get the same type of response back with the mentors.mentorList not populated:

"students": {
            "numOfStudents": 1,
            "studentList": [
                {
                    "_id": "5c9b828e347bb645e0865257",
                    "name": "Student",
                    "account_type": "student",
                    "profile": {
                        "profile_picture": {
                            "url": "https://stemuli.blob.core.windows.net/stemuli/profile-picture-7eb5e4b6-a1ad-4bbf-880b-02540d709a86.jpeg",
                            "originalname": "download (2).jpeg",
                            "mimetype": "application/jpeg",
                            "blobName": "profile-picture-7eb5e4b6-a1ad-4bbf-880b-02540d709a86.jpeg",
                            "container": "stemuli",
                            "blob": "profile-picture-7eb5e4b6-a1ad-4bbf-880b-02540d709a86.jpeg",
                            "size": "6136",
                            "etag": "\"0x8D75918FB547DD0\"",
                            "createdOn": "2019-11-08T04:08:44.406Z"
                        },
                        "_id": "5c9b828e347bb645e086525a"
                    },
                    "profile_type": "StudentProfile"
                }
            ]
        },
        "mentors": {
            "numOfMentors": 1,
            "mentorList": [
                [
                    "5c9b828e347bb645e0865257"
                ]
            ]
        }

I am really not sure what could be going wrong. And it seems like it is user error, but I can't figure out why it's not working! Maybe someone can shed some light on this.

Thank you so much.

mrwadepro commented 4 years ago

Schema design issue, didn't notice the nested array for mentorList. Face Palm