简体   繁体   English

如何从 mongodb 中没有字段名的嵌套数组中 $pull

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

my arrays in mongoDB are: slots: Array我的 arrays 在 mongoDB 是: slots: Array
0: Array, 1: Array, 2: Array, etc. 0:数组,1:数组,2:数组等

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"在每个数组中,我都有一个插槽数组:“2021 年 8 月 15 日 00:00”1:“2021 年 8 月 15 日 00:30”2:“2021 年 8 月 15 日 01:00”3:“2021 年 8 月 15 日 01:30”4 : "2021年8月15日02:00" 5: "2021年8月15日02:30" 6: "2021年8月15日03:00" 7: "2021年8月15日03:30" 8: "2021年8月15日04:00" 9 : "2021 年 8 月 15 日 04:30" 10: "2021 年 8 月 15 日 05:00" 11: "2021 年 8 月 15 日 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")
          }
        } )
      }
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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