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

时间:2016-08-30 15:05:15

标签: mongoose database nosql

我目前有一个MongoDB文档的以下模式,它应该保存用户数据:

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

草图属性需要是一个数组对象,其中每个对象都关联草图的名称和保存草图数据的数组。出于某种原因,架构最终会被创建如下:

{
    "__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();
  });
  });

2 个答案:

答案 0 :(得分:4)

架构

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

答案 1 :(得分:1)

我最终为草图创建了一个子文档,并使其成为用户架构的子项:

var sketchSchema =  mongoose.Schema({ name: String, sketchData : Array
  });
var SavedSketch = mongoose.model('Sketch', sketchSchema);
// });


var userSchema =  new mongoose.Schema({
  username: {type: String, unique : true},
  password: {type: String},
  firstname: String,
  lastname: String,

  sketches:[sketchSchema]


var User = mongoose.model('User', userSchema);

module.exports = User;