在Mongoose中保存模型无法保存嵌套组件 [英] Saving Model in Mongoose fails to save nested component

查看:120
本文介绍了在Mongoose中保存模型无法保存嵌套组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型架构:

var memberSchema = mongoose.Schema({

    'project'       : { 
        'type'      : Schema.Types.ObjectId, 
        'ref'       : 'Project' 
    },
    'first'         : String,
    'last'          : String,
    'email'         : String,
    'tracker'       : {
        'etag'      : String,
        'id'        : String,
        'photoLink' : String,
        'role'      : String,
        'type'      : String,
                      },
    'survey'        : {
        'etag'      : String,
        'id'        : String,
        'photoLink' : String,
        'role'      : String,
        'type'      : String,
                      },

});

我要保存一个新文档.我执行以下操作:

I want to save a new document. I perform the following:

var Member     = require('../app/models/member');

var new_member = new Member({
  project      : project._id,
  first        : req.body.all_perms[i].first,
  last         : req.body.all_perms[i].last,
  email        : req.body.all_perms[i].email,
  tracker      : {
                    etag      : req.body.all_perms[i].etag_tracker,
                    id        : req.body.all_perms[i].ftid_tracker,
                    photoLink : req.body.all_perms[i].photoLink_tracker,
                    role      : req.body.all_perms[i].role_tracker,
                    type      : req.body.all_perms[i].type_tracker,
                 },
  survey       : {
                    etag      : req.body.all_perms[i].etag_survey,
                    id        : req.body.all_perms[i].ftid_survey,
                    photoLink : req.body.all_perms[i].photoLink_survey,
                    role      : req.body.all_perms[i].role_survey,
                    type      : req.body.all_perms[i].type_survey,
                 },
})
new_member.save(function(err, saved_result) {})

执行此操作将导致成功创建新文档,但该文档不包含trackersurvey.但是,它确实包含firstlastemail.

Performing this operation results in a successfully created new document BUT the document does not contain either tracker or survey. It does, however, contain first, last, and email.

如何成功保存新模型的嵌套组件?谢谢.

How do I successfully save the nested component of the new model? Thanks.

推荐答案

要在名为type的嵌入式对象中定义字段,您需要使用对象符号来定义其类型,或者Mongoose认为其正在定义对象的类型.父对象.

To define a field in an embedded object named type, you need to use the object notation to define its type or Mongoose thinks it's defining the type of the parent object.

因此将您的架构更改为:

So change your schema to:

var memberSchema = mongoose.Schema({

    'project'       : { 
        'type'      : Schema.Types.ObjectId, 
        'ref'       : 'Project' 
    },
    'first'         : String,
    'last'          : String,
    'email'         : String,
    'tracker'       : {
        'etag'      : String,
        'id'        : String,
        'photoLink' : String,
        'role'      : String,
        'type'      : {'type': String},   // Here...
    },
    'survey'        : {
        'etag'      : String,
        'id'        : String,
        'photoLink' : String,
        'role'      : String,
        'type'      : {'type': String},   // ...and here
    },
});

这篇关于在Mongoose中保存模型无法保存嵌套组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆