如何在MongoDB模式中创建嵌套对象

loremIpsum1771

我目前有一个用于保存用户数据的MongoDB文档的以下架构:

var userSchema =  new mongoose.Schema({
  username: {type: String, unique : true},
  password: {type: String},
  firstname: String,
  lastname: String,
  sketches: 
              [ 
                {name: String, 
                 sketch: Array}
              ]

sketches属性必须是一个数组对象,其中每个对象都将一个草图的名称与一个保存草图数据的数组相关联。由于某种原因,最终会按以下方式创建模式:

{
    "__v" : 1,
    "_id" : ObjectId("57c4d7693aa85cea2acf4d4d"),
    "firstname" : "test",
    "lastname" : "name",
    "password" : "password123",
    "sketches" : [ 
        {
            "sketch" : []
        }
    ],
    "username" : "testname"
}

我不确定在MongoDB中创建嵌套对象的正确格式,但我假设它与JSON相同。模式应如何构造以产生对象数组。

编辑:

从PUT请求插入文档的Web服务:

app.route("/addSketch/:username").put(function(req, res, next) {
  var user_name = req.params.username;
  User.findOne({username:user_name},function(err,foundObject){
    if(err){
      console.log("error");
      res.status(500).send();
    }
    else{
      if(!foundObject){
        res.status(404).send();
      }
      else{
        
        if(req.body.strokes && req.body.sketchName){
          var sketchObj = [];
          sketchObj[req.body.sketchName] = req.body.strokes;
          foundObject.sketches.push(req.body.sketchData);
        }
        foundObject.save(function(err,updatedObject){
          if(err){
            console.log(err);
            res.status(500).send();
          }
          else{
            res.send(updatedObject);
          }
        });
      }
    }

  });


  console.log('saving on server');
   var form = formidable.IncomingForm();

   console.log(form);
   console.log('the type of the request received is', (typeof req));

  form.parse(req, function(err, fields, files) {
      res.writeHead(200, {"content-type": "text/plain"});
      res.write('received upload:\n\n');
    var name = fields.name;
    var newSketch = new SavedSketch();
      newSketch.name = name;
      newSketch.sketchData =  fields.value;
      newSketch.save(function(err,savedObject){
        if(err){
               console.log(err);
               res.status(500).json({status:'failure'})
            }
            else{
              console.log("ID: " + fields.value.id + " strokeData:" + fields.value.strokes);
               res.json({status: 'success'});
            } 
      });

         res.end();
  });
  });
瓦西克·穆罕默德(Wasiq Muhammad)

架构图

var userSchema =  new mongoose.Schema({
  username: {type: String, unique : true},
  password: {type: String},
  firstname:{type: String},
  lastname: {type: String},
  sketches: [
    {
      name: String,
      sketch : {type : Array}
    }
  ]
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在MongoDB模式,嵌套对象和数组MEAN堆栈中创建嵌套对象

如何在 mongodb 的嵌套模式中更新?

如何在Mongodb中拉出嵌套对象?

如何在mongodb中查询嵌套对象

如何在javascript中创建嵌套对象

如何在PHP中创建嵌套对象

如何在Mongoose模式中要求嵌套的json对象

如何在mongodb的文档中的对象中创建对象?

如何在嵌套的对象数组中取消设置值?MongoDB

MongoDB:如何在嵌套数组中插入对象?

如何在mongodb中查询嵌套的json对象?

如何在mongodb中查询对象的嵌套数组?

MongoDB - 如何在嵌套的对象数组中查找空值

如何在mongodb中查询3级嵌套对象

如何在 mongodb 的嵌套结构中检索特定对象?

如何在MongoDB中的$ group内创建嵌套数组?

如何在PHP5中创建嵌套的json对象

如何在FSharp中创建嵌套的JSON对象

如何在 vanilla JavaScript 中动态创建嵌套对象

猫鼬模式,如何在一个模式中嵌套对象?

如何在 mongodb 中为新日期创建新对象?

如何在Mongoose中无限嵌套模式?

如何在打字稿中创建动态嵌套的对象和对象属性

如何在AngularJs中推送JSON对象并创建一个嵌套对象

使用JAXB从Java对象创建xml时,如何在JAXB中创建嵌套的根元素

如何在棉花糖中创建模式以嵌套查询数据?

如何动态更新mongodb中的嵌套对象

如何在MongoDB中使用查找对象的嵌套对象

如何在复杂的mongoDb模式中发布到嵌套字段