Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
Delete an array element from a 2d array mongoDB

I want to delete an array from a 2d array from my mongoDB collection. My Schema looks like:

{
    "_id" : ObjectId("6218e63e4f9e4efbb05de2bf"),
    "name" : "Toyota",
    "rate" : 50,
    "fine" : 5,
    "datesBooked" : [ 
        [ 
            "2022-02-08", 
            "2022-02-14"
        ], 
        [ 
            "2022-02-16", 
            "2022-02-18"
        ], 
        [ 
            "2022-02-20", 
            "2022-02-22"
        ], 
        [ 
            "2022-02-24", 
            "2022-02-25"
        ], 
        [ 
            "2022-03-01", 
            "2022-03-02"
        ], 
        [ 
            "2022-03-01", 
            "2022-03-02"
        ]
    ],
    "__v" : 0
}

I would like to find an object with a certain _id and delete a particular element from the datesBooked 2d array. I tried using $pull function but I couldn't get it right. Can someone please tell me how should I be doing this! I'm using express and javascript to do this. Also, I am noob. Already grateful thanks!

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This is how to remove particular element from the 2nd array inside datesBooked array via $pull:

 var myVariable="2022-02-14"
 db.collection.update({ "_id": ObjectId("6218e63e4f9e4efbb05de2bf") },
  {
   "$pull": {
     "datesBooked.$[]": myVariable
   }
 })

Explained: You need to use the all positional operator $[] ( available since mongodb v3.6) to provide the 2nd array to the $pull operation so it remove the array element with the exact value , in the example this is value "2022-02-14"

playground

If you need to remove the element with [var1,var2] from first array you need to do with $pull like this:

 db.collection.update({
  "_id": ObjectId("6218e63e4f9e4efbb05de2bf")
  },
 {
 "$pull": {
  "datesBooked": [
   "2022-02-08",
   "2022-02-14"
  ]
 }
})

explained: Pull the array element from the first array

playground

over 4 years ago · Santiago Trujillo Report

0

in Mongodb v4.2+ you can use pipelined updates to achieve this, like so:

db.collection.updateOne(
{ _id: id },
[
  {
    $set: {
      datesBooked: {
        $concatArrays: [
          {
            $slice: [
              "$datesBooked",
              index
            ]
          },
          {
            $slice: [
              "$datesBooked",
              index + 1,
              {
                $size: "$datesBooked"
              }
            ]
          }
        ]
      }
    }
  }
])

Mongo Playground

For older versions this cannot be done in 1 call, you will need to split this into 2 calls separate calls.

-------------------------- EDIT ------------------------------- Here's how to do it with variables:

const var1 = "2022-02-20";
const var2 = "2022-02-22";

db.collection.updateOne(
{ _id: id },
[
  {
    $set: {
      datesBooked: {
        $filter: {
          input: "$datesBooked",
          cond: {
            $and: [
              {
                $ne: [
                  {
                    $arrayElemAt: [
                      "$$this",
                      0
                    ]
                  },
                  var1
                ]
              },
              {
                $ne: [
                  {
                    $arrayElemAt: [
                      "$$this",
                      1
                    ]
                  },
                  var2
                ]
              }
            ]
          }
        }
      }
    }
  }
])

Mongo Playground

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!