【问题标题】:Postman only posts to mongoose nested level 1邮递员只发帖到 mongoose 嵌套级别 1
【发布时间】:2015-03-23 17:20:45
【问题描述】:

我正在尝试测试具有嵌套字段的 mongoose 架构的帖子,但我只能在级别 1 和级别 2 中的第一个字段。例如:

我有一个可以包含多个 IP 地址和多个子网的网络模型。当我将查询放入 Postman 时,它允许我创建多个 IP 地址和多个子网(很棒),但我无法定义类型字段?

Mongoose 架构:

var mongoose = require('mongoose'), Schema = mongoose.Schema, ObjectId = mongoose.Schema.ObjectId;

var networkSchema = module.exports = mongoose.model('Network', {
network_id:ObjectId,
location: String,
hostname: String,
device: String,
model: String,
ipAddress: [ipaddressSchema],
subnets: [subnetSchema],
iosVersion: String,
softwareImage: String,
serialNumber: String,
});

var ipaddressSchema = Schema ({
ipAddress: String,
type: String,
});

var subnetSchema = Schema ({
range: String,
type: String,
});

控制器:

var Network = require('../models/network');

module.exports.create = function (req, res) {
var network = new Network(req.body);
network.save(function (err, result) {
         res.json(result);
});
}

module.exports.list = function (req, res) {
Network.find({}, function (err, results) {
  res.json(results);
});
}

邮递员查询:

邮递员结果:

我想要:

{
"__v": 0,
"location": "London Office",
"hostname": "lon-asa-01",
"device": "Switch-MLS",
"model": "Cisco 3750",
"softwareImage": "1.2",
"serialNumber": "123456",
"_id": "5510495c1d40ef965d7d1cec",
"subnets":[ 
["range" : "10.0.100.0/24", "type" : "Client" ],
["range" : "10.0.101.0/24", "type" : "Server" ],
],
"ipAddress": [
 ["ipAddress" : "10.0.100.1", "type" : "Inside" ],
 ["ipAddress" : "10.0.101.254", "type" : "Outside" ],
]
}

【问题讨论】:

  • 以原始格式发布表单,您可以将其格式化为 JSON,这样您就可以嵌套属性。
  • AndreiC,“我想要”部分就是一个例子。你能确认嵌套属性的 json 语法吗?

标签: javascript mongodb express mongoose


【解决方案1】:

好的,给你:

首先,您的架构应该如下所示:

var networkSchema = module.exports = mongoose.model('Network', {
    network_id: ObjectId,
    location: String,
    hostname: String,
    device: String,
    model: String,
    ipAddress: [{type: ObjectId, ref: 'IpadressModelName'}],
    subnets: [{type: ObjectId, ref: 'SubnetModelName'}],
    iosVersion: String,
    softwareImage: String,
    serialNumber: String,
}); 

在您的控制器中,您必须首先插入您的网络所依赖的实体,以便您将有一个 _id 提供给网络模型作为参考:

module.exports.create = function (req, res) {
    var network = new Network(req.body);
    var ipAddress = [],
        ipIds = [];
    req.body.ipAddress.forEach(function(ip){
        ipAddress.push(new IpadressModelName(ip));
    });
    var subnets = [],
        subnetsIds = [];
    req.body.subnets.forEach(function(sn){
        subnets.push(new SubnetModelName(sn));
    });
    IpadressModelName.create(ipAddress, function () {
        // args[0] should be the error
        if (args[0]) {
            throw args[0]
        }else{
            for(var i=1; i<args.length; i++ )
                ipIds.push(args[i]._id);
        }
        SubnetModelName.create(subnets, function () {
          // args[0] should be the error
            if (args[0]) {
                throw args[0]
            }else{
                for(var i=1; i<args.length; i++ )
                    subnetsIds.push(args[i]._id);
            }
            network.ipAddress = ipIds;
            network.subnets = subnetsIds;
            network.save(function (err, result) {
                 res.json(result);
            });
        });
    });
}

最后以原始 JSON 形式发布数据:

{
    "location": "London Office",
    "hostname": "lon-asa-01",
    "device": "Switch-MLS",
    "model": "Cisco 3750",
    "softwareImage": "1.2",
    "serialNumber": "123456",
    "subnets":[ 
        {"range" : "10.0.100.0/24", "type" : "Client" },
        {"range" : "10.0.101.0/24", "type" : "Server" }
    ],
    "ipAddress": [
        {"ipAddress" : "10.0.100.1", "type" : "Inside" },
        {"ipAddress" : "10.0.101.254", "type" : "Outside" }
    ]
}

此示例中的代码仅用于演示您可以使用的方法,它不符合最佳实践。

【讨论】:

  • AndreiC,感谢您的详尽解释!我现在有很多事情要做,所以会及时回复(希望很快)。我可以确认,如果我在 mongo shell 中运行原始 json,它会根据需要添加记录,尽管是作为嵌入式文档而不是引用的集合。
  • 接下来我会处理控制器。
  • 这成功了!现在如何让返回的 JSON 显示嵌套对象而不仅仅是 ObjectIds?
猜你喜欢
  • 2014-12-29
  • 2020-11-04
  • 2019-05-13
  • 2021-07-09
  • 1970-01-01
  • 2021-04-29
  • 1970-01-01
  • 2020-11-23
  • 2019-10-16
相关资源
最近更新 更多