이 명령 고려 $ 세트 플래그 findOneAndUpdate 내 프로젝트를 개발하는 동안몽구스
WorkPlan.findOneAndUpdate({ _id: req.params.id }, { '$set': updateObj }, function(err) {
...
})
, 나는 첫 번째 명령의 결과임을 알아 놀랐다 두 번째 명령의 결과와 동일 : updateObj
이 데이터베이스의 기존 레코드에 병합됩니다. 첫 번째 경우에는 대체 될 예정입니다. 이것은 mongoose/mongodb의 버그입니까? 아니면 내가 잘못하고 있습니까? 개체를 병합하는 대신 업데이트하는 방법은 무엇입니까? 몽구스 4.0.7을 사용하고 있습니다.
감사합니다.
==========
업데이트 :
이 실제 업무 계획 스키마 정의입니다 :
workPlanSchema = mongoose.Schema({
planId: { type: String, required: true },
projectName: { type: String, required: true },
projectNumber: { type: String, required: false },
projectManagerName: { type: String, required: true },
clientPhoneNumber: { type: String, required: false },
clientEmail: { type: String, required: true },
projectEndShowDate: { type: Date, required: true },
segmentationsToDisplay: { type: [String], required: false },
areas: [
{
fatherArea: { type: mongoose.Schema.ObjectId, ref: 'Area' },
childAreas: [{ childId : { type: mongoose.Schema.ObjectId, ref: 'Area' }, status: { type: String, default: 'none' } }]
}
],
logoPositions: [
{
lat: { type: Number, required: true },
lng: { type: Number, required: true }
}
],
logoPath: { type: String, required: false },
}, { collection: 'workPlans' });
WorkPlan = mongoose.model('WorkPlan', workPlanSchema);
그리고이 updateObj
의 예입니다
var updateObj = {
projectManagerName: projectManagerName,
clientEmail: clientEmail,
clientPhoneNumber: clientPhoneNumber,
segmentationsToDisplay: segmentationsToDisplay ? segmentationsToDisplay.split(',') : []
}
따라서 $ set 플래그를 사용하지 않으면 필드가 필요합니다. 예를 들어 projectNumber
은 새 레코드에 존재하지 않아도 아직 볼 수 있습니다.
감사를 참조하십시오, 나는 그 인식하지 않았다. –