Home > Back-end >  Mongoose array in document does not exist when queried
Mongoose array in document does not exist when queried

Time:02-11

I need to query for all documents with an array but the returned documents dont have the array.

query

(async () => {
        const data = await Lesson.find({signed: {$exists: true}});
        console.log(data[0].signed); # undefined
})();

model

const lessonSchema = new mon.Schema(
    {
        day: Number,
        startTime: Number,
        endTime: Number,
        description: {type: String, trim: true},
        signed: [mon.Schema.Types.ObjectId]
    }, 
    {
        collection: 'lessons'
    }
);
module.exports = mon.model("Lesson", lessonSchema);

I checked the database and the documents do have the array. The query retrieve everything except for the array (all of the documents and their values except for the array). NOTE: in the database I have only two test documents. both have an array and both don't have the array in the query.

Thanks

EDIT: I found out that if I remove the signed property from the schema it works. Why?

CodePudding user response:

I had this problem because the type of signed was objectId and the ids I used were strings. I cleared the array and added real ObjectId and it worked.

  • Related