【问题标题】:Unable to return _id of a subdocument using mongoosejs无法使用 mongoosejs 返回子文档的 _id
【发布时间】:2017-08-10 05:37:27
【问题描述】:
[
{
    "_id": "58b89de6a480ce48c8f3742d",
    "name": "Core Site",
    "version": "1.0.0",
    "author": "Jane Doe",
    "vendor": "Online Banking",
    "steps": [
        {
            "name": "Step 1: Fun Stuff",
            "dependencies": "1,2,3,4",
            "resource": "TS",
            "weight": 0
        },
        {
            "name": "Step 2: Weird Stuff",
            "dependencies": "3,4",
            "resource": "PS",
            "weight": 1
        }
    ]
},
{
    "_id": "58b8a22097fbe41746827ac7",
    "name": "Online Statements",
    "version": "1.0.0",
    "author": "John Doe",
    "vendor": "Online Banking",
    "steps": [
        {
            "name": "Step 1: Fun Stuff",
            "dependencies": "1,2,3,4",
            "resource": "TS",
            "weight": 0
        }
    ]
}]

我已将此信息保存到 MongoDB,但当我尝试获取此信息时,子文档“步骤”不包括我可以使用 Robomongo 看到的 _id。

我的架构如下:

    // grab the things we need
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var stepSchema = new Schema({
  name: { type: String, required: true},
  dependencies: String,
  resource: String,
  weight: Number
})

// create a schema
var sopSchema = new Schema({
  name: { type: String, required: true, unique: true },
  version: String,
  author: String,
  vendor: String,
  steps: [stepSchema]
});



// the schema is useless so far
// we need to create a model using it
var Sop = mongoose.model('Sop', sopSchema);

// make this available to our users in our Node applications
module.exports = Sop;

我的最终目标是检查该步骤是否已包含并在前端进行更改时对其进行更新。所以我想得到这个参考,以确保我有正确的子文档。

我的nodejs服务器中的get语句如下。

router.route('/sops')
    .get(function(req, res) {
        Sop.find(function(err, sops) {
            if (err)
                res.send(err);

                var subdoc = sops[0].steps[0];
                console.log(subdoc);
            res.json(sops);
    });
})

【问题讨论】:

  • angular2 以何种方式影响这篇文章。 ??删除它,因为它不在这里使用
  • 谢谢。我正在使用 angular2 使用其余端点并且没有捕获 _id 但发现返回本身是错误的。对不起
  • 您面临的问题是什么?
  • 我正在尝试创建一个可以向 SOP 添加多个步骤的反应式表单。我面临的主要问题是,如果稍作修改,我如何更新一个步骤,而不清除所有步骤并将它们更新为新步骤。我的第一种方法是获取 _id,这样我就可以传递它并执行 For 循环来确定发生了什么变化。但是,如果我能找到表格中发生的变化并发送它,那可能也可以。

标签: node.js rest mongoose mongoose-schema


【解决方案1】:

MongoDB 不会为您添加 _id 到子文档。您需要手动添加。

所以:

"steps": [
        {
            "_id": new ObjectId(),
            "name": "Step 1: Fun Stuff",
            "dependencies": "1,2,3,4",
            "resource": "TS",
            "weight": 0
        }
 ]

【讨论】:

  • 当我使用 Robomongo 时,我可以在数据库中看到 _id。不知道为什么那没有通过。我可以尝试添加对象只是为了查看。
【解决方案2】:

Link to Article that Fixed the issue

  var stepSchema = new Schema({
  name: { type: String, required: true},
  dependencies: String,
  resource: String,
  weight: Number
},{ _id: true });

在末尾添加 _id: true 属性以强制生成 _id。

【讨论】:

    猜你喜欢
    • 2014-11-07
    • 2020-06-30
    • 2020-03-14
    • 2021-02-25
    • 2017-08-12
    • 2018-02-27
    • 2014-09-20
    • 2012-10-13
    • 2021-03-08
    相关资源
    最近更新 更多