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

Mongoose 如何在MongoDB模式中创建嵌套对象,mongoose,database,nosql,Mongoose,Database,Nosql,我目前有一个MongoDB文档的以下模式,该文档应该保存用户数据: var userSchema = new mongoose.Schema({ username: {type: String, unique : true}, password: {type: String}, firstname: String, lastname: String, sketches: [ {name: String,

我目前有一个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();
  });
  });

模式

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

模式

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

我最终为草图创建了一个子文档,并将其作为用户模式的子文档:

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;

我最终为草图创建了一个子文档,并将其作为用户模式的子文档:

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;

你是如何在你的猫鼬代码中插入文档的,你也能展示这部分吗?@chridam我刚刚更新了帖子。我也在考虑将Sketchs数组作为用户文档的子文档,但我不确定这样做的最佳方式。你如何将文档插入Mongoose代码中,你能同时显示该部分吗?@chridam我刚刚更新了帖子。我也在考虑让Sketchs数组成为用户文档的子文档,但我不确定这样做的最佳方式。看起来效果不错,我最终为Sketchs创建了一个子文档。我将用我所做的发布另一个答案。这似乎是可行的,我最终为草图创建了一个子文档。我将用我所做的发布另一个答案。声明:
password:{type:String}
vs
firstname:String
?。@Sunny
{type:String}
是相等的
:String
。在第一个模式中,我们可以添加更多选项,如
password:{type:String,unique:true,required:true}
。声明:
password:{type:String}
vs
firstname:String
?。@Sunny
{type:String}
等于
:String
。第一种模式,我们可以添加更多选项,如
密码:{type:String,unique:true,requuried:true}