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

查看:37
本文介绍了Mongoose - 在 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
});

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

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