javascript - postman 仅向 Mongoose 嵌套级别 1 发帖

标签 javascript mongodb express mongoose

我正在尝试测试具有嵌套字段的 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,
});

Controller :

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

postman 查询:

enter image description here

postman 结果:

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

最佳答案

好的,开始吧:

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

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

在您的 Controller 中,您必须首先插入网络所依赖的实体,以便您将有一个 _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" }
    ]
}

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

关于javascript - postman 仅向 Mongoose 嵌套级别 1 发帖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29216433/

相关文章:

javascript - Angular ui 路由器 - 多次使用相同的 templateUrl 和 Controller

node.js - 如何使用 Node.js/Express + Passport + Websockets 进行身份验证?

javascript - MongoError : Majority read concern requested, 但存储引擎不支持

javascript - Node.js 发送后无法设置 header ,res.json 从未调用两次

javascript - Express 中的动态 URL 和静态路由

javascript - Angular-UI Bootstrap Datepicker 新日期

javascript - 如何让FancyBox在页面加载时自动启动并在15秒后自动隐藏?

javascript - For 循环在迭代输出之前生成 'undefined'

node.js - MongoDB mLab mongoose Node.js 驱动程序 - 一段空闲时间后连接超时?

javascript - 将数据推送到mongodb文档中的数组