javascript 我应该如何正确使用 populate with mongoose?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17598712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How should I properly use populate with mongoose?
提问by Eduárd Moldován
I am learning some node and have been trying to use mongoose. Currently, my goal is to learn how to use populate.
我正在学习一些节点并且一直在尝试使用猫鼬。目前,我的目标是学习如何使用populate。
I have a projects
definition and milestone
require:
我有一个projects
定义并milestone
要求:
projectSchema = new mongoose.Schema({
id: String,
title: String,
description: String,
owner: String,
site: String,
creation_date: Date,
milestone_ids: Array,
milestones: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Milestone"
}]
})
Project = mongoose.model("Project", projectSchema)
milestones = require(__dirname + "/milestones.js")();
Then I do this at some point in the projects.js
:
然后我在某个时间点这样做projects.js
:
Project.find(query, {}, {sort: {_id: -1}},
function (error, results) {
callback(results);
}
).populate("milestones");
How do I populate the milestones?
如何填充里程碑?
Here is the project
data from mongo:
这是project
来自 mongo的数据:
{
"title": "sitename",
"description": "online thing",
"creation_date": {
"$date": "2013-07-11T19:45:42.139Z"
},
"_id": {
"$oid": "51df0b66dbdd7c4f14000001"
},
"milestones": [],
"milestone_ids": [],
"__v": 0
}
And this one is the milestone
that is basically connected to the project:
而这个是milestone
与项目基本相关的:
{
"title": "Proof of concept",
"description": "Make it work.",
"due_date": {
"$date": "2013-07-11T19:46:38.535Z"
},
"project_id": "51df0b66dbdd7c4f14000001",
"_id": {
"$oid": "51df0b9edbdd7c4f14000002"
},
"__v": 0
}
Also, this is the milestone schema:
此外,这是里程碑模式:
milestoneschema = new mongoose.Schema({
id: String,
title: String,
description: String,
owner: String,
site: String,
due_date: Date,
project_id: {
type: String,
ref: "Project"
}
})
Milestone = mongoose.model("Milestone", milestoneschema);
回答by Peter Lyons
You need to get the order right of defining query options then executing, and chainable APIs such as mongoose Query can't know what additional methods you might call AFTER the query fires. So when you pass the callback to .find
, mongoose sends the query right away.
您需要获得定义查询选项的正确顺序,然后执行,并且诸如 mongoose Query 之类的可链接 API 无法知道在查询触发后您可能会调用哪些其他方法。因此,当您将回调传递给 时.find
,猫鼬会立即发送查询。
Pass a callback to find
将回调传递给 find
- query defined by arguments to
find
- since callback is there, query immediately executes and issues command to DB
- then
.populate
happens, but it has no effect as the query has already been sent to mongo
- 由参数定义的查询
find
- 由于回调在那里,查询立即执行并向数据库发出命令
- 然后
.populate
发生,但它没有效果,因为查询已经发送到 mongo
Here's what you need to do:
您需要执行以下操作:
Project.find(query, {}, {
sort: {
_id: -1
}
}).populate("milestones").exec(function (error, results) {
callback(results);
});
Or a little more readable:
或者更具可读性:
Project
.find(query)
.sort('-_id')
.populate('milestones')
.exec(function(error, results) {
callback(results);
});
Omit callback and use .exec
省略回调并使用 .exec
- query passed to
.find
creates query object with parameters - additional chained calls to
.sort
,.populate
etc further configure the query .exec
tells mongoose you are done configuring the query and mongoose issues the DB command
- 查询传递给
.find
创建带有参数的查询对象 - 额外的链式调用
.sort
,.populate
等进一步配置查询 .exec
告诉 mongoose 您已完成查询配置,mongoose 发出 DB 命令