2

I want to remove an nested array element if its first value is 0 from array. The data is shown below.

[
  {
    "data": [[0,1],[2,3],[0,3],[4,5]]
  }
]

test case

And here is my solution, which doesn't work.

db.collection.update({},
{
  "$pull": {
    "data": {
      "$elemMatch": {
        "0": 0
      }
    }
  }
})

1 Answer 1

4

If you're using Mongo version 4.2+ you can use pipelined updates like so:

db.collection.update({},
[
  {
    $set: {
      data: {
        $filter: {
          input: "$data",
          as: "datum",
          cond: {
            $ne: [
              {
                $arrayElemAt: [
                  "$$datum",
                  0
                ]
              },
              0
            ]
          }
        }
      }
    }
  }
])

Mongo Playground

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.