问题标签 [mongoose-populate]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1340 浏览

node.js - Mongoose 填充选项和查询时间

我正在一个平台上工作,我在所有查询中使用 mongoose .populate 次数,我打开 mongoose 调试模式,发现查询执行时间几乎没有差异(现在 100 个文档,将有 100000 个文档将来)使用填充和不使用填充。

我知道基本上填充也在内部进行 finOne 查询,我的问题是,使用.populate会增加我的查询时间,或者如果记录数量达到数百万,它是否会影响我的性能。还有我可以选择的任何替代方案来提高性能吗

0 投票
1 回答
217 浏览

mongodb - Mongoose not populating previously saved document with reference to newly saved document

all.

I am writing a MEAN stack application, using Mongoose (4.0.6) with Node/Express to interface with MongoDB, and I am running into difficulty populating saved documents when I later save new documents that the existing document should have a reference to. Specifically, in the app I have a user create an instance of a company before creating their admin account for that company, so when the user registers him/herself as an admin, I'd like the company document to populate its users array with the new user.

Here are my schemas for company and user:

User.js...

Company.js...

Here is the code in my controller:

The company (e.g. TEST53) saves to the database correctly with an empty users array:

Then when I create the user, it saves correctly:

And I can see that the correct ObjectId prints to the console for user.company:

But the company's users array doesn't populate with the user's id, and the console.log inside the .exec function prints 'TEST53's users now includes '.

I have tried several ways of wiring this up, with just 'users' instead of { path: 'users' }, writing a function that pushes the user into the array, using .run instead of .exec, but so far without success.

Is there something obvious I'm missing? Thanks in advance for any suggestions!

0 投票
0 回答
2397 浏览

node.js - 在 post find mongoose hook 中修改结果

我正在寻找一种方法来修改猫鼬的查询结果。

下面是带有后挂钩的自包含模型

查找查询工作正常,帖子正在触发,但结果未按帖子挂钩过滤。

在返回结果之前如何编辑结果。

0 投票
2 回答
87 浏览

javascript - Mongoose - 使用时不填充记录,否则为记录数组

我正在学习 MeanJS,但我对 Mongoose 有疑问。我有两个模型:

我正在保存这样的文章:

它正在正确保存。该对象如下所示:

甚至当我计算像 {{ category.articles.length }} 这样的记录时,类别中的文章数量也很合适,我什至可以在视图中打印 ObjectIds。但是当我像这样添加 .populate('articles') 时:

长度返回 0,ObjectIds 消失,我无法访问文章属性,就像类别中没有文章一样。任何想法为什么会发生这种情况?

附加编辑:

0 投票
2 回答
168 浏览

javascript - 填充嵌套引用的更好方法 mongoosejs

我正在尝试填充嵌套在其他引用中的引用。我让它工作了,但它似乎有点老套,想知道是否有其他方法可以做到这一点:

是否有一些方法可以填充所有引用,或者返回第二个填充作为板调用的一部分,而不像现在这样手动设置它?

0 投票
1 回答
12098 浏览

node.js - lean() inside populate in mongoose

I am populating a user collection in mongoose and want to set a flag if the user is signed up using social accounts. So I am selecting that fields in populate. But due to security concerns I don't want to pass it in response. So I converted it into plain object using lean() and set the values as null. But once a values are set null for a particular user then it set that values null for that user_id. How set them null or how to hide them from response.

Thanks in advance

0 投票
0 回答
469 浏览

node.js - Mongoose 填充错误模式尚未注册模型。使用 mongoose.model(name, schema)

嗨,他们是我的 Mongoose 模型:

这是另一个:

如果我尝试这个查询,它会给我一个错误。

MissingSchemaError:未捕获的错误:尚未为模型“模型”注册架构。使用 mongoose.model(name, schema)

0 投票
2 回答
491 浏览

node.js - 未达到 aync 回调,响应挂起

我遇到了一个问题,我不确定要采取什么步骤来解决它。现在所有数据都被正确检索但是 responseCallback 永远不会触发,因此我没有使用响应数组到达 res.json 调用。这里的任何指导将不胜感激!谢谢。

为澄清起见,问题在于 aysnc.each 回调。

这是我希望将更新的业务信息返回给客户的地方,但这永远不会触发。

0 投票
1 回答
505 浏览

node.js - Mongoose:使用搜索查询在架构中设置字段

所以..我想知道在猫鼬中我是否可以做这样的事情:

因此,当我使用查询搜索俱乐部时,我希望填充 playMatches 字段。现在我使用一种“单例类型方式”来填充playedMatches字段,如下所示:

但是因为我希望事情变得异步,这实际上不起作用,而且我不想在使用任何其他使用playedMatches字段的函数之前调用一个方法来设置playedMatches字段,因为这很丑。也..

MatchSchema 如下所示:

提前谢谢!

0 投票
1 回答
205 浏览

arrays - MongoDb 子文档数组填充(通过 Mongoose ORM):调用填充时是否保持数组顺序

假设我在 Mongoose 中有 2 个 Schema,如下所示:

如果按照以下行进行查询:

数组中元素的顺序会保持不变吗?这里的目标是允许用户维护他们电影的播放列表顺序。如果在“填充”方法触发时,电影对象 ID 的数组顺序没有得到维护,那么这将不符合我的目的。因此,我想我会问在这方面知识更丰富的人。

如果这可行,那么我还有另一个任务是允许用户更改播放列表中电影的顺序,这应该是直接的,允许在数组中交换电影对象 id 索引。

提前感谢您的帮助。