简体   繁体   中英

How to $pull from nested array with no fieldnames in mongodb

my arrays in mongoDB are: slots: Array
0: Array, 1: Array, 2: Array, etc.

in each array, I have an array of slots: "August 15th 2021 00:00" 1: "August 15th 2021 00:30" 2: "August 15th 2021 01:00" 3: "August 15th 2021 01:30" 4: "August 15th 2021 02:00" 5: "August 15th 2021 02:30" 6: "August 15th 2021 03:00" 7: "August 15th 2021 03:30" 8: "August 15th 2021 04:00" 9: "August 15th 2021 04:30" 10: "August 15th 2021 05:00" 11: "August 15th 2021 05:30"

I try to pull from these but it doesn't work. my code:

  exports.postAvailibility = (req,res)=>{
    const teacherId = req.params.teacherId;
    var slots = req.body.chosenSlots;
    var day = req.body.day
    Teacher.updateOne({_id: teacherId}, { $addToSet: {availibility: slots} }, (err, result)=>{
      if(err){
        return res.json(err)
      } else{
        Teacher.updateOne({_id: teacherId}, {$pull: {"slots.0": slots}}, (err, result)=>{
          if(err){
            return res.json(err)
          } else{
            return res.json("Successfully booked slot")
          }
        } )
      }
    })
  }

It adds to the availability field but doesn't pull from slots

This worked for me:

const teacherId = req.params.teacherId;
    var slots = req.body.chosenSlots;
    var day = req.body.day
    Teacher.updateOne({_id: teacherId}, { $addToSet: {availability: slots} }, (err, result)=>{
      if(err){
        return res.json(err)
      } else{
        Teacher.updateOne({_id: teacherId}, {$pull: {"slots.$[]": slots}}, (err, result)=>{
          if(err){
            return res.json(err)
          } else{
            return res.json("Successfully booked slot")
          }
        } )
      }
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM