Mongoose - using Populate on an array of ObjectId

后端 未结 2 679
旧时难觅i
旧时难觅i 2020-12-05 02:25

I\'ve got a schema that looks a bit like:

var conversationSchema = new Schema({
    created: { type: Date, default: Date.now },
    updated: { type: Date, de         


        
相关标签:
2条回答
  • 2020-12-05 02:54

    For anyone else coming across this question.. the OP's code has an error in the schema definition.. it should be:

    var conversationSchema = new Schema({
        created: { type: Date, default: Date.now },
        updated: { type: Date, default: Date.now },
        recipients: [{ type: Schema.ObjectId, ref: 'User' }],
        messages: [ conversationMessageSchema ]
    });
    mongoose.model('Conversation', conversationSchema);
    
    0 讨论(0)
  • 2020-12-05 03:02

    Use the name of the schema path instead of the collection name:

    Conversation.findOne({ _id: myConversationId})
    .populate('recipients') // <==
    .exec(function(err, conversation){
        //do stuff
    });
    
    0 讨论(0)
提交回复
热议问题