简体   繁体   English

如何使用Mongoose和NodeJS在MongoDB中保存嵌套数组

[英]How to save nested array in MongoDB using Mongoose and NodeJS

Can anyone explain me how to save the nested array items into mongodb with mongoose and nodejs?. 任何人都可以解释我如何使用mongoose和nodejs将嵌套数组项保存到mongodb中。

Here is a schema I am using. 这是我正在使用的架构。

var demoSchema = ({
    "r_id": Number,
    "r_label": String,
    "entity": [{
        "d_label": String,
        "d_type": String
      }
    ]
})

And here is Nodejs function I am using to save the data into db 这里是我用来将数据保存到db中的Nodejs函数

 app.route("/mypages/rooms")
  .post(function(req, res) {
   var db = mongoOp.demo();
   var response = {};
   req.checkBody("r_id", "Enter a valid r_id address.").notEmpty();
   req.checkBody("r_label", "Enter a valid label address.").notEmpty();

   var errors = req.validationErrors();
   if (errors) {
     console.log(errors);
     console.log(req.body);
     res.status(500);
     res.end('500 Server Error');
     //res.render('addrooms',{flag:1});
     return;
     } else {
         db.r_id = req.body.r_id;
         db.r_label = req.body.r_label;
         db.entity = req.body.entity;
         db.save(function(err) {
          if (err) {
           findfromdb(req, res, 2); //own function for implementation purpose
          } else {
           findfromdb(req, res, 1);
          }
        });
        //var middleVar = req.body.resources;
      //  console.log(middleVar[0].d_rgb);
     }
 });

set entity with array [] 使用array []设置实体

db.entity = [{}];

app.route("/mypages/rooms")
  .post(function(req, res) {
   var db = mongoOp.demo();
   var response = {};
   req.checkBody("r_id", "Enter a valid r_id address.").notEmpty();
   req.checkBody("r_label", "Enter a valid label address.").notEmpty();

   var errors = req.validationErrors();
   if (errors) {
     console.log(errors);
     console.log(req.body);
     res.status(500);
     res.end('500 Server Error');
     //res.render('addrooms',{flag:1});
     return;
     } else {
         db.r_id = req.body.r_id;
         db.r_label = req.body.r_label;
         db.entity = [{
                      "d_label": req.body.label_type,
                      "d_type": req.body.d_type
                     }];
         db.save(function(err) {
          if (err) {
           findfromdb(req, res, 2); //own function for implementation purpose
          } else {
           findfromdb(req, res, 1);
          }
        });
        //var middleVar = req.body.resources;
      //  console.log(middleVar[0].d_rgb);
     }
 });

The below operation adds the element label_type and d_type to entity array if they does not exist in the array, if they exists, then they won't be added 如果数组中不存在元素label_typed_type ,则将其添加到实体数组中,如果它们存在,则不会添加它们

https://docs.mongodb.com/manual/reference/operator/update/addToSet/ https://docs.mongodb.com/manual/reference/operator/update/addToSet/

Model.update(
    query, //  { _id: 1 }
    {
        $addToSet: {
            "enity": {
                "d_label": req.body.label_type,
                "d_type": req.body.d_type
            }
        }
    }
)

have a look at this answer 看看这个答案

Pushing item to Mongodb collection array 将项目推送到Mongodb集合数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 mongoose 和 nodejs 在 mongodb 中保存 model 的数组 - Save an array of a model in mongodb using mongoose and nodejs 如何使用 Mongoose 模式将嵌套数组保存到 Mongodb? - How Save Nested Array into Mongodb Using Mongoose schema? 尝试在 Nodejs 上使用 mongoose 在 MongoDB 中保存嵌套文档 - Trying to save nested document in MongoDB using mongoose on Nodejs 如何使用猫鼬在nodejs中保存数组数据? - How to save array data in nodejs using mongoose? 如何在nodejs中使用mongoose将对象数组和嵌套对象数组插入mongodb - how to insert array of objects and array of nested objects into mongodb using mongoose in nodejs 如何从Mongodb到Mongoose的嵌套文档中获取Nodejs中的数组? - How to get an Array in Nodejs from a nested document in Mongodb through Mongoose? 如何使用body-parser使用nodejs和mongoose将嵌套对象数组插入/添加到mongodb数据库中 - How to insert/add array of nested objects into mongodb database with nodejs and mongoose using body-parser 如何在nodejs中使用mongoose在mongodb中选择深层嵌套数据 - How to select deep nested data in mongodb using mongoose in nodejs 如何使用猫鼬以Mongodb嵌套模式的形式保存数据 - How to save data in the form of Mongodb Nested schema using mongoose 如何使用 mongoose 和 NodeJs 在 Mongodb 的数组字段中插入数据 - How to insert data in array field in Mongodb using mongoose and NodeJs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM