我应该如何正确使用与猫鼬一起填充? [英] How should I properly use populate with mongoose?

查看:130
本文介绍了我应该如何正确使用与猫鼬一起填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习一些节点,并一直在尝试使用猫鼬。目前,我的目标是学习如何使用填充

I am learning some node and have been trying to use mongoose. Currently, my goal is to learn how to use populate.

我有项目定义和里程碑要求:

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")();

然后我在 projects.js

Project.find(query, {}, {sort: {_id: -1}},
    function (error, results) {
        callback(results);
    }
).populate("milestones");

如何填充里程碑?

这是来自mongo的项目数据:

Here is the project data from 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:

{
    "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);


推荐答案

您需要获得定义查询选项的订单权限然后执行,并且可链接的API(例如mongoose Query)无法知道在查询触发后您可能调用的其他方法。因此,当您将回调传递给 .find 时,mongoose会立即发送查询。

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.


  • 的参数定义的查询

  • 因为回调就在那里,查询立即执行并向DB发出命令

  • 然后 .populate 发生了,但它没有效果,因为查询已经发送到mongo

  • 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

这是你需要做的:

Project.find(query, {}, {
    sort: {
        _id: -1
    }
}).populate("milestones").exec(function (error, results) {
    callback(results);
});

或者更具可读性:

Project
    .find(query)
    .sort('-_id')
    .populate('milestones')
    .exec(function(error, results) {                  
        callback(results);
    });



省略回调并使用 .exec




  • 查询传递给 .find 使用参数创建查询对象

  • .sort .populate 等的其他链式调用进一步配置查询

  • .exec 告诉mongoose你完成了配置查询并且mongoose发出了DB命令

  • Omit callback and use .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
    • 这篇关于我应该如何正确使用与猫鼬一起填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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