Home > Software engineering >  Delete an array element from a 2d array mongoDB
Delete an array element from a 2d array mongoDB

Time:02-28

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!

CodePudding user response:

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

CodePudding user response:

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

  • Related