[英]How to insert/add array of nested objects into mongodb database with nodejs and mongoose using body-parser
I'm trying to make a post request to insert data to mongo database.我正在尝试发出将数据插入到 mongo 数据库的帖子请求。 I'm using mongoose
for this.为此,我正在使用mongoose
。 I have created the mongoose
Schema
like below -我创建了如下所示的mongoose
Schema
-
const countrySchema = mongoose.Schema({
country_id: {
type : Number,
required: true,
unique : true
},
country_name : {
type : String,
required : true,
unique : true
},
country_description : {
type : String,
required : true,
unique : true
},
country_currency : {
type : String,
required : true,
unique : true
},
country_capital : {
type : String,
required : true,
unique : true
},
country_numberof_districs : {
type : Number,
required : true
},
country_createdat : {
type : Date,
required : true
},
country_districs : {
districs : [
{
distric_id : {
type : Number,
// required : true,
},
distric_name : {
type : String,
// required : true,
},
distric_description : {
type : String,
// required : true,
},
distric_famous_for : {
type : String,
// required : true
}
}
]
}
});
and below is my post method -下面是我的帖子方法 -
router.post('/addcountry', (request, response, next) => {
const district = [];
district.push({
distric_id: request.body.distric_id,
distric_name : request.body.distric_name,
distric_description : request.body.distric_description,
distric_famous_for : request.body.distric_famous_for
});
let newCountry = new country({
country_id : request.body.country_id,
country_name : request.body.country_name,
country_description : request.body.country_description,
country_currency : request.body.country_currency,
country_capital : request.body.country_capital,
country_numberof_districs : request.body.country_numberof_districs,
country_createdat : new Date(),
country_districs : {
districs : [district]
}
});
country.addCountry(newCountry, (err, country) =>{
console.log('came here');
if(err){
response.json({
success : false,
message : 'failed to add country' + err
});
}else {
response.json({
success : true,
message : 'country added successfully'
});
}
});
});
I tested with Postman
.我用Postman
测试过。 it works fine without districs
part.它在没有districs
部分的情况下工作正常。 I tried to post below object -我尝试在对象下方发布 -
{
"country_id" : 909773,
"country_name" : "USA",
"country_description" : "UNITED STATRE",
"country_currency" : "USD",
"country_capital" : "NEWYORK",
"country_numberof_districs" : 100,
"country_districs": {
"districs":
[
{
"distric_id": 777,
"distric_name" : "melbourne",
"distric_description" : "no des",
"distric_famous_for" : "beaches"
},
{
"distric_id": 900,
"distric_name" : "sydney",
"distric_description" : "no des",
"distric_famous_for" : "beaches"
}
]
}
}
The result was like this -结果是这样的——
{
"_id" : ObjectId("58e37b5c3c6b07153600a7f6"),
"country_id" : 909773,
"country_name" : "USA",
"country_description" : "UNITED STATRE",
"country_currency" : "USD",
"country_capital" : "NEWYORK",
"country_numberof_districs" : 100,
"country_createdat" : ISODate("2017-04-04T10:54:20.726Z"),
"country_districs" : {
"districs" : [
{
"_id" : ObjectId("58e37b5c3c6b07153600a7f7")
}
]
},
"__v" : 0
}
Note : First I tried with one district, it also didn't work.注意:首先我尝试了一个分区,它也不起作用。 how can I add this distric object(there can be many. like distric array).and how can I iterate through all the distric objects.我怎样才能添加这个区域对象(可以有很多。比如区域数组)以及如何遍历所有区域对象。 Hope your help with this.希望您对此有所帮助。
Can you try this while making the country -你能在做国家的时候试试这个吗?
let newCountry = new country({
country_districs : {
districs : req.body.country_districs.districs
}
});
along with the rest of the fields...连同其他领域...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.