邮差只发布到猫鼬嵌套级别1

时间:2015-03-23 17:20:46

标签: javascript mongodb express mongoose

我试图将帖子测试为具有嵌套字段的mongoose模式,但我只能级别1和级别2中的第一个字段。例如:

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

Mongoose Schema:

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);
});
}

邮差查询:

enter image description here

邮差结果:

enter image description here

我想:

{
"__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" ],
]
}

1 个答案:

答案 0 :(得分: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" }
    ]
}

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