'How to create a query which looks for an objects property's map inside an array

This is entry Schema

const entrySchema = new Schema({
entry: {
    type: String,
    required: true,
},
topicId: {
    type: mongoose.Schema.Types.ObjectId,
    required: true,
},
author: {
    type: String,
    required: true,
},
createdAt: {
    type: Date,
    required: true,
    default: Date.now(),
},
updatedAt: {
    type: Date
},
likes: {
    count:{type: Number, default: 0},
    authors:{type: [String], default: []}
}

})

and this is topicSchema

const topicSchema = new Schema({
topic: {
    type: String,
    require: true,
    unique: true
},
date: {
    type: Date,
    require: true,
    default: Date.now()
},
entries: {
    type: [mongoose.Schema.Types.ObjectId],
    require: true,
    default: []
}

})

entries is an array object which contains entry objects inside. This is the code to get the topics whose _ids equals topicId for every entry object in entries object.

await Topic.find({_id: {$in: entries.topicId}})

This is the query but it's wrong topicId is not a key of entries, I just want to look topicIds of every entry object which are in entries array. How can I create a query which looks topicIds of every entries ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source