1

this is my model

const DoctorSchema=mongoose.Schema({
    ds:[{day:String,timeSlots:[{time:String,status:Boolean}]}],
    hospital:{type:mongoose.Schema.ObjectId,ref:'Hospital',required:true},
    city:{type:[String],index:true},
    addedAt:{type:Date,default:Date.now}
})

the ds field is the dateslot fields of the doctor. after a patient books a time slot then the status of that time slot of that particular day should be made to false so that no other patient books the same time slot.

the ds field is an array of objects containing day field and timeslots array where time slots are the slots available for a particular day

ds:[{day,that days timeslot array}]

  • https://stackoverflow.com/questions/26156687/mongoose-find-update-subdocument answers the question, showing solutions with mongoose and mongo directly. You can use the positional operator ($) or modify the document with mongoose and resave it – AdamExchange Aug 19 '20 at 19:55
  • const doctor=await Doctor.findOneAndUpdate({"_id":docId,"ds.timeSlots._id":"5f37f980d178d528f822775d"},{ $set:{'ds.timeSlots.$.status':false}}) i tried this but getting error MongoError: Cannot create field 'timeSlots' in element {ds: [ { _id: ObjectId('5f37f980d178d528f8227750'), day: "0", timeSlots: [ { _id: ObjectId('5f37f980d178d528f8227751'), time: "4:30 PM", status: true }, { _id: ObjectId('5f37f980d178d528f8227752'), time: "5:45 PM", status: false } ] } – Kumar Neeraj Aug 19 '20 at 20:36
  • just the official docs "Nested Arrays The positional $ operator cannot be used for queries which traverse more than one array, such as queries that traverse arrays nested within other arrays, because the replacement for the $ placeholder is a single value" – Kumar Neeraj Aug 19 '20 at 20:56

0 Answers0