【问题标题】:Can't Store Nested JSON Array to MongoDB Using Mongoose无法使用 Mongoose 将嵌套的 JSON 数组存储到 MongoDB
【发布时间】:2018-12-23 15:29:07
【问题描述】:

我在 Postman 上有这样的 JSON:

{
    "hostname": [
        {
            "item": [
                {
                    "system": "10l313",
                    "severity": "2"
                },
                {
                    "system": "2131414",
                    "severity": "3"
                }
            ]
        },
        {
            "item": [
                {
                    "system": "4234235",
                    "severity": "4"
                }
            ]
        }
    ]
}

我想从上面的 json 在 mongodb 中创建新的集合。这只是实际json数组的一个小图,上面的json数组可以包含一个巨大的数组。我很困惑如何使用 mongoose 保存尽可能多的 json 数组,我必须循环尽可能多的数组长度还是有其他更简单的方法?

猫鼬模式:

var ItemSchema =  new Schema({
    _id: mongoose.Schema.Types.ObjectId,

    system: {
        type: String
    },

    severity: {
        type: Number
    }
})

var VulnSchema = new Schema({

    hostname: [{
        item: [{ItemSchema}]
    }]

});

控制器:

exports.create_vulnerabilities = function (req, res) {
    var vuln = new Vuln ({
        _idFIle: mongoose.Types.ObjectId(),
        hostname: req.body.hostname
    });         
    vuln.save()
      .then(result => {
          res.status(201).json({
              result
          });
      })
      .catch(err => {
          console.log(err);
          res.status(500).json({
              error: err
          });
      });

};

我尝试运行我的代码,但结果是这样的。问题是系统和严重性属性没有存储在mongodb中。

{
    "_id" : ObjectId("5b4c39a301651a0fc047bec7"),
    "hostname" : [ 
        {
            "_id" : ObjectId("5b4c39a301651a0fc047beca"),
            "item" : [ 
                {
                    "_id" : ObjectId("5b4c39a301651a0fc047becc")
                }, 
                {
                    "_id" : ObjectId("5b4c39a301651a0fc047becb")
                }
            ]
        }, 
        {
            "_id" : ObjectId("5b4c39a301651a0fc047bec8"),
            "item" : [ 
                {
                    "_id" : ObjectId("5b4c39a301651a0fc047bec9")
                }
            ]
        }
    ],
    "__v" : 0
}

请帮助我。谢谢你

【问题讨论】:

  • req.body.hostname 是我在顶部描述的 json

标签: arrays json node.js mongoose nested


【解决方案1】:

改变

var VulnSchema = new Schema({
    hostname: [{
       item: [{ItemSchema}]
    }]
});

var VulnSchema = new Schema({
     hostname: [{
        item: [ItemSchema]
    }]
});

示例尝试运行:

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/test');

const Schema = mongoose.Schema,ObjectId = Schema.ObjectId;

var ItemSchema =  new Schema({
    _id: mongoose.Schema.Types.ObjectId,

    system: {
        type: String
    },

    severity: {
        type: Number
    }
})

var VulnSchema = new Schema({
    hostname: [{
        item: [ItemSchema]
    }]
});

const Vuln = mongoose.model('Vuln', VulnSchema);

var hostname = [
    {
        "item": [
            {
                "system": "10l313",
                "severity": "2"
            },
            {
                "system": "2131414",
                "severity": "3"
            }
        ]
    },
    {
        "item": [
            {
                "system": "4234235",
                "severity": "4"
            }
        ]
    }
]

var vuln = new Vuln ({
    _idFIle: mongoose.Types.ObjectId(),
    hostname: hostname
});

vuln.save()
    .then(result => {
       console.log(JSON.stringify(result))
})
    .catch(err => {
       console.log(err);}
);

【讨论】:

  • 我是否应该进行迭代以获取系统和严重性,然后将其保存在 mongodb 中?
  • 我已经用一个例子更新了答案。请尝试一下。
猜你喜欢
  • 2018-02-18
  • 1970-01-01
  • 1970-01-01
  • 2019-03-28
  • 2022-09-29
  • 2021-08-13
  • 1970-01-01
  • 2013-03-29
  • 2016-02-09
相关资源
最近更新 更多