猫鼬-在ObjectId数组上使用Populate [英] Mongoose - using Populate on an array of ObjectId

查看:69
本文介绍了猫鼬-在ObjectId数组上使用Populate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来有点像的模式:

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

var conversationSchema = new Schema({
    created: { type: Date, default: Date.now },
    updated: { type: Date, default: Date.now },
    recipients: { type: [Schema.ObjectId], ref: 'User' },
    messages: [ conversationMessageSchema ]
});

因此,我的收件人集合是引用我的用户架构/集合的对象ID的集合.

So my recipients collection, is a collection of object id's referencing my user schema / collection.

我需要在查询时填充这些,所以我正在尝试:

I need to populate these on query, so i'm trying this:

Conversation.findOne({ _id: myConversationId})
.populate('user')
.run(function(err, conversation){
    //do stuff
});

但显然'用户'并未填充...

But obviously 'user' isn't populating...

有没有办法做到这一点?

Is there a way I can do this?

推荐答案

使用架构路径的名称代替集合名称:

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

Conversation.findOne({ _id: myConversationId})
.populate('recipients') // <==
.exec(function(err, conversation){
    //do stuff
});

这篇关于猫鼬-在ObjectId数组上使用Populate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆