【发布时间】:2018-10-06 05:25:41
【问题描述】:
这与这个问题基本相同...StackOverflow Question ...但我需要知道数组周围的行为。
我遇到 $set 仍在更新其他变量的问题。这是我的查询,它正在更新这些变量,但它也设置了一些数组(媒体:[] 和运动:[])我在运动员档案中.0 回空。
为了清楚起见,这里是用户对象的架构。
UserSchema.js
const UserSchema = new Schema({
password: { type: String, required: true },
username: { type: String, unique: true, required: true },
role: { type: String, required: true },
athleteProfile: [AthleteSchema],
evaluatorProfile: [EvaluatorSchema],
adminProfile: [AdminSchema],
search: [SearchSchema]
});
AthleteSchema.js
const AthleteSchema = new Schema({
athEmail: String,
firstName: {
type: String,
validate: {
validator: (firstName) => firstName.length > 2,
message: 'Name must be longer than 2 characters.'
}
},
lastName: {
type: String,
validate: {
validator: (lastName) => lastName.length > 2,
message: 'Name must be longer than 2 characters.'
}
},
profilePic: String,
accountType: String,
events: [{
ref: 'Events',
type: Schema.Types.ObjectId
}],
media: [MediaSchema],
city: String,
state: String,
zip: Number,
country: { type: String, uppercase: true },
height: String,
weight: Number,
birthday: Date,
sports: {
type: [SportSchema],
validate: [arrayLimit, '{PATH} exceeds the limit of 3']
},
references: {
type: [ReferenceSchema],
validate: [arrayLimit, '{PATH} exceeds the limit of 3']
},
evaluations: [EvaluationSchema],
gradYear: Number,
school: String
});
然后这是请求。
User.findByIdAndUpdate({ _id: id }, {
$set:
{ username,
password,
'athleteProfile.0': {
firstName,
lastName,
athEmail: email,
gradYear,
school,
birthday,
city,
state,
zip,
weight,
height
}}
}, { returnNewDocument: true});
我做错了什么?
【问题讨论】:
标签: javascript node.js express mongoose