1

I have a nested array structured like this

 {
 "_id": "5dff75968102f11e20ae888e",
 "docID": "Employees",
 "data": [
     [
     "234768237",
     "Value1",
     ],
     [
     "234768238",
     "Value2"
     ],
     [
     "234768239",
     "Value3"
     ]
 ]
 }

Inside the nested data array, first element is employee ID, Ex: 234768237. I would want to pass an array of employee ID's and delete the matching ones, for ex : [234768237, 234768238]

What i have tried so far

    let employeeIDArray =  [234768237, 234768238];
    collection.updateOne(
        { docID: "Employees" },
        {
            $pull: {
                "data.$[]": { "0": { $each: employeeIDArray } }
            }
        }
    );

2 Answers 2

2

You're close, try:

let employeeIDArray =  [234768237, 234768238];
collection.updateOne(
    { docID: "Employees" },
    {
        $pull: {
            "data": { $in: employeeIDArray } 
        }
    }
);

The $each modifier is only available for $push and $addToSet actions, read more about it here.

EDIT:

For Nested Array use $pullAll instead of $pull like so:

collection.updateOne(
    { docID: "Employees" },
    {
      $pullAll: {
         "data.$[]":  employeeIDArray
    }
});
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for you suggestion, but it doesn't remove the elements. Please note that data[] contains arrays in the question. I'm trying to delete the element from the nested array.
oops mb, what mongo version are you on?
I'm using the mongo db atlas.
mb its just what i was testing on, changed to data.
It still doesn't work, note that employeeIDArray array contains only 0th element, i guess your solution is still not matching based on the 0th element inside the array.
|
0

If you are using MongoDB's latest version then you can use below query to update using aggregation pipeline:

collection.updateOne(
    { docId: "Employees" }, 
    [ { 
        $set: { 
            data: { 
                $filter: { 
                    input: "$data", 
                    cond: { $in: [ { $arrayElemAt: [ "$$this", 0 ] }, [ "234768237", "234768238" ] ] } }
                }
            } 
    } ], 
    { upsert: true }
)

4 Comments

Hi, i'm getting error 'BSON field \'update.updates.u\' is the wrong type \'array\', expected type \'object\'', with your sugggestion.
I think it is a MongoDB version related problem that you are facing. In the latest 4.2 version, you can update the document using the aggregation pipeline as given above. What version of MongoDB are you using?
I'm using MongoDB atlas
You can upgrade the version of MongoDB in Atlas if you are using the cluster of Tier 10 or higher. -- docs.atlas.mongodb.com/scale-cluster/#scale-cluster-version

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.