node.js - 如何使用 body-parser 使用 nodejs 和 mongoose 将嵌套对象数组插入/添加到 mongodb 数据库中

标签 node.js mongodb mongoose mean-stack

我正在尝试发出将数据插入 mongo 数据库的发布请求。为此,我正在使用 mongoose。我创建了如下所示的 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
          }
        }
      ]
    }
});

下面是我的发帖方法-

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

我用 Postman 测试过。它在没有 districs 部分的情况下工作正常。我试图在下面发布对象 -

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

结果是这样的-

{
    "_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
}

注意:首先我尝试了一个区,它也没有用。我怎样才能添加这个分区对象(可以有很多。比如分区数组)。我怎样才能遍历所有分区对象。希望您对此有所帮助。

最佳答案

你能在制作国家的时候试试这个吗-

let newCountry = new country({ 
    country_districs : { 
        districs : req.body.country_districs.districs
     } 
}); 

连同其他字段...

关于node.js - 如何使用 body-parser 使用 nodejs 和 mongoose 将嵌套对象数组插入/添加到 mongodb 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43205285/

相关文章:

node.js - 如何将各种 Mongoose 结果连接到一个 JSON 数组以显示在 DataTable 上?

node.js - 有没有办法只返回 Mongoose 模式中定义的字段?

javascript - 如何仅通过一次调用将一组对象保存到 Mongoose 数据库?

javascript - Socket.io 为 Socket 识别用户

node.js - 在 cf 中使用 PID 调试应用程序

java - 如何在Java代码中使用JavaMongoRDD的管道提供多阶段?

c# - 使用 MongoDB C# 驱动程序更新所有文档的字段数据类型

design-patterns - Redis、node.js 和 Javascript 回调范围/流程

javascript - 使用 node 和 multer 将图像上传到 heroku 不起作用

mongodb - MongoDB 中的序列