its me
its me

Reputation: 542

findOneAndUpdate is not working inside array

I'm trying to update the document in the MongoDB collection but it's not working for me. Here is the function all fields come to the backend.

{
    "_id" : ObjectId("59a6b381c13a090c70fc21b6"),
    "processNumber" : "FEE 082517",
    "System" : "abc",    
    "TaxAmount" : 0,
    "TaxPercent" : 0,
    "Currency" : "USD",
    "ProcessData" : [ 
        
        {
            "_id" : ObjectId("59ee2873b1621419a03fba6c"),
            "KDSID" : "1db1d4b8-61bc-45eb-bf6d-15af1e391df5"
            
        }, 
        {
            "_id" : ObjectId("59ee2873b1621419a03fba6d"),
            "KDSID" : "aa9ccaf3-a638-4013-afdc-ccf0a39361e8"
            
        }, 
        {
            "_id" : ObjectId("59ee2873b1621419a03fba6e"),
            "KDSID" : "4c5e32a7-e2fb-4fe9-998f-e22602e46dba"
        }
        {
            "Name" : "2017 Calc.xlsx",
            "FileID" : "59ee2873b1621419a03fb9b7",
            "_id" : ObjectId("59ee2873b1621419a03fba75")
        }
    ]     
       
  }

Query:

db.process.findOneAndUpdate(
   { 'ProcessData._id': ObjectId("59ee2873b1621419a03fba75"),'ProcessData.FileID': { '$exists': true, '$ne': null }},
   { $set: { 'ProcessData.$.IsFailed': "Yes" } }
  
)

When I run the above query IsFailed is not updating. can you please advise? I have tried in node and MongoDB and it's not working.

If ProcessData._id matches with the given id and ProcessData.FileID exist we have to set IsFailed Yes

Upvotes: 1

Views: 219

Answers (3)

RoundedHouse
RoundedHouse

Reputation: 81

You're querying through an array of embedded documents, and hence by using elemMatch to find the specific id and perform needed operation on that document should be implemented for example as below:

db.process.updateOne(
{
    ProcessData: {
        $elemMatch: {
            _id: 
            ObjectId("59ee2873b1621419a03fba75"),
        }
    }
},
{
    "ProcessData.FileID": {
        $exists: true,
        $ne: null
    }
},
{
    $set: {
        "ProcessData.$.IsFailed": "Yes"
    }
})

Upvotes: 0

Dĵ ΝιΓΞΗΛψΚ
Dĵ ΝιΓΞΗΛψΚ

Reputation: 5669

here's my version with $elemMatch

db.process.updateOne(
    {
        ProcessData: {
            $elemMatch: {
                _id: ObjectId("59ee2873b1621419a03fba75"),
                FileID: {
                    $exists: true,
                    $ne: null
                }
            }
        }
    },
    {
        $set: { "ProcessData.$.IsFaild": "Yes" }
    })

https://mongoplayground.net/p/3LBlw9kA6Gl

Upvotes: 1

varman
varman

Reputation: 8894

You need to use arrayFilter when you modify the array.

db.collectionName.updateOne(
    { 
        'ProcessData._id': ObjectId("59ee2873b1621419a03fba75"),
        'ProcessData.FileID': { '$exists': true, '$ne': null }
    },
    {
        $set:{"ProcessData.$[p].isFailed": "Yes"}},
    {
      arrayFilters:[{"p._id":ObjectId("59ee2873b1621419a03fba75")}]
    }
)

Upvotes: 1

Related Questions