猫鼬填充不与ObjectId数组一起使用

编程入门 行业动态 更新时间:2024-10-26 09:34:53
本文介绍了猫鼬填充不与ObjectId数组一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的模式:

/** Schemas */ var profile = Schema({ EmailAddress: String, FirstName: String, LastName: String, BusinessName: String }); var convSchema = Schema({ name: String, users: [{ type: Schema.Types.ObjectId, ref: 'Profiles' }], conversationType: { type: String, enum: ['single', 'group'], default: 'single' }, created: { type: Date, default: Date.now }, lastUpdated: { type: Date, default: Date.now } }); /** Models */ db.Profiles = mongoose.model('Profiles', profile); db.Conversations = mongoose.model('ChatConversations', convSchema); module.exports = db;

然后,我尝试使用以下代码填充用户( mongoosejs/docs/populate. html ):

Then I try to populate Users using following code (mongoosejs/docs/populate.html):

db.Conversations.find(query).populate('users').exec(function (err, records) { console.log(records); });

这将返回records但users数组作为空白数组[].

This is returning records but users array as a blank array [].

我也尝试了另一种方法( mongoosejs/docs/api.html#model_Model.populate ):

I also tried the other way around (mongoosejs/docs/api.html#model_Model.populate):

db.Conversations.find(query, function (err, records) { db.Conversations.populate(records, {path: "users", select: "BusinessName"}, function (err, records) { console.log(records); }); });

结果相同.当我检查对概要文件收集记录的引用时.

Results are same. When I checked references into profile collection records are there.

有什么想法吗?

推荐答案

我通过重命名模型(第三个观点)使它起作用:

I got it working by renaming model (the 3rd arguement):

mongoose.model( "Profiles", profile, "Profiles" );

问题是猫鼬在搜索profiles集合,但在数据库中以Profiles的形式出现.因此,我将其重命名为Profiles以匹配确切的名称.

The issue was Mongoose was searching for profiles collection but its there as Profiles in database. So I renamed it to Profiles to match the exact name.

Phewww!多亏我.

Phewww! Thanks to me.

更多推荐

猫鼬填充不与ObjectId数组一起使用

本文发布于:2023-11-17 03:50:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608726.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   不与   ObjectId

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!