【发布时间】: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