在 Mongoose 中保存模型无法保存嵌套组件
Saving Model in Mongoose fails to save nested component
我有以下模型架构:
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,
},
});
我想保存一个新文档。我执行以下操作:
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) {})
执行此操作会成功创建新文档,但该文档不包含 tracker
或 survey
。但是,它包含 first
、last
和 email
.
如何成功保存新模型的嵌套组件?谢谢。
要在名为type
的嵌入对象中定义字段,您需要使用对象符号来定义其类型,否则 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' : {'type': String}, // Here...
},
'survey' : {
'etag' : String,
'id' : String,
'photoLink' : String,
'role' : String,
'type' : {'type': String}, // ...and here
},
});
我有以下模型架构:
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,
},
});
我想保存一个新文档。我执行以下操作:
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) {})
执行此操作会成功创建新文档,但该文档不包含 tracker
或 survey
。但是,它包含 first
、last
和 email
.
如何成功保存新模型的嵌套组件?谢谢。
要在名为type
的嵌入对象中定义字段,您需要使用对象符号来定义其类型,否则 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' : {'type': String}, // Here...
},
'survey' : {
'etag' : String,
'id' : String,
'photoLink' : String,
'role' : String,
'type' : {'type': String}, // ...and here
},
});