如何使用mongoose制作模式给定输出?

时间:2016-01-22 04:15:57

标签: node.js mongodb mongoose

我已经给出了我的输出,我该如何制作schema.any为此举例说明我已经添加了我的模式

我的输出代码:

 $scope.countries = [{
            "name": "India",
                "states": [{
                "name": "Maharashtra",
                    "cities": [{
                    "name": "Pune"
                }, {
                    "name": "Mumbai"
                }, {
                    "name": "Nagpur"
                }, {
                    "name": "Akola"
                }]
            }, {
                "name": "Madhya Pradesh",
                    "cities": [{
                    "name": "Indore"
                }, {
                    "name": "Bhopal"
                }, {
                    "name": "Jabalpur"
                }]
            }, {
                "name": "Rajasthan",
                    "cities": [{
                    "name": "Jaipur"
                }, {
                    "name": "Ajmer"
                }, {
                    "name": "Jodhpur"
                }]
            }]
        }, {
            "name": "USA",
                "states": [{
                "name": "Alabama",
                    "cities": [{
                    "name": "Montgomery"
                }, {
                    "name": "Birmingham"
                }]
            }, {
                "name": "California",
                    "cities": [{
                    "name": "Sacramento"
                }, {
                    "name": "Fremont"
                }]
            }, {
                "name": "Illinois",
                    "cities": [{
                    "name": "Springfield"
                }, {
                    "name": "Chicago"
                }]
            }]
        }, {
            "name": "Australia",
                "states": [{
                "name": "NewSouthWales",
                    "cities": [{
                    "name": "Sydney"
                }]
            }, {
                "name": "Victoria",
                    "cities": [{
                    "name": "Melbourne"
                }]
            }]
        }];

这是我的路线:

app.route('/address')
        .get(address.list)
        .post(address.create);

控制器代码:

/**
 * Create a address
 */
exports.create = function(req, res) {
    var address = new Address(req.body);
    address.user = req.user;

    address.save(function(err) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            res.json(address);
        }
    });
};

架构代码:

'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var citySchema = {
    name: {type:String, required: false}
};

citySchema  = 'new Schema('+ citySchema +',{_id:false})';
var stateSchema = {
    name: {type:String, required: false},
    cities: [citySchema], default:[]
};
stateSchema  = 'new Schema('+stateSchema +',{_id:false})';

var countrySchema = {
    name: {type:String, required: false},
    states: [stateSchema], default:[]

};
// dbObj is mongoose connection object
var collectionObj = dbObj.model('countries', countrySchema);

我已经更新了我的路由和控制器架构。我可以更改为inser,对象请给出一些建议吗?

1 个答案:

答案 0 :(得分:0)

请尝试使用以下架构: -

var citySchema = {
    name: {type:String, required: false}
};

citySchema  = 'new Schema('+ citySchema +',{_id:false})';
var stateSchema = {
    name: {type:String, required: false},
    cities: [citySchema], default:[]
};
stateSchema  = 'new Schema('+stateSchema +',{_id:false})';

var countrySchema = {
    name: {type:String, required: false},
    states: [stateSchema], default:[]

};
// dbObj is mongoose connection object
var collectionObj = dbObj.model('countries', countrySchema);

现在,如果您想将任何单个国家/地区数据插入数据库,请执行以下步骤 -

// Let us assume that we need to insert following data first

    var data = {
        "name": "India",
        "states": [{
                "name": "Maharashtra",
                "cities": [{
                        "name": "Pune"
                    }, {
                        "name": "Mumbai"
                    }, {
                        "name": "Nagpur"
                    }, {
                        "name": "Akola"
                    }]
            }, {
                "name": "Madhya Pradesh",
                "cities": [{
                        "name": "Indore"
                    }, {
                        "name": "Bhopal"
                    }, {
                        "name": "Jabalpur"
                    }]
            }, {
                "name": "Rajasthan",
                "cities": [{
                        "name": "Jaipur"
                    }, {
                        "name": "Ajmer"
                    }, {
                        "name": "Jodhpur"
                    }]
            }]
    };


// add country 

var insertObj = {
    "name": "India",
    "states": []
}
countryModel.create(insertObj, function (err, job) {
    cb(err, job);
});

// add states 

    var states = [{
            "name": "Maharashtra",
            "cities": []
        }, {
            "name": "Madhya Pradesh",
            "cities": []
        }, {
            "name": "Rajasthan",
            "cities": []
        }];
    var stateCond = {"name": "India"};
    var stateUpdate = {$push: {states: states}};
    var stateOpts = {};
    countryModel.findOneAndUpdate(stateCond, stateUpdate, stateOpts, function (err, resp) {
        cb(err, resp);
    });

// Now if you want to add "Maharastra" Cities 

    var cities = [{
            "name": "Pune"
        }, {
            "name": "Mumbai"
        }, {
            "name": "Nagpur"
        }, {
            "name": "Akola"
        }]

    var cityCond = {"name": "India", "states.name": "Maharashtra"};
    var cityUpdate = {$push: {cities: cities}};
    var cityOpts = {};
    countryModel.findOneAndUpdate(cityCond, cityUpdate, cityOpts, function (err, resp) {
        cb(err, resp);
    });

// Now same insertion query need to be run for "Madhya Pradesh" and "Rajasthan"

由于