Это в основном тот же вопрос, что и этот вопрос ... StackOverflow Вопрос ... но мне нужно знать поведение массивов.
У меня проблемы с $ set stillОбновление других переменных. Это мой запрос, и он работает для обновления этих переменных, но он также устанавливает некоторые массивы (media: [] и sports: []), которые есть в athleteProfile.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});
Что я делаю не так?