Mongo - findAndModify has to be executed twice to take effect I have a collection campaign like this:
{
        &quot;_id&quot; : ObjectId(&quot;5ec12c501bd23866ef315a85&quot;),
        &quot;schemaVersion&quot; : &quot;1&quot;,
        &quot;internalVersion&quot; : NumberLong(56),
        &quot;code&quot; : &quot;CK&quot;,
        &quot;siteCampaigns&quot; : [
                {
                        &quot;_id&quot; : ObjectId(&quot;5f3689274d42720edb5be328&quot;),
                        &quot;siteCode&quot; : &quot;IT&quot;,
                        &quot;startDate&quot; : ISODate(&quot;2020-02-10T06:00:00Z&quot;),
                        &quot;endDate&quot; : ISODate(&quot;2020-02-20T06:00:00Z&quot;),
                        &quot;status&quot; : &quot;SUBSCRIBED&quot;
                },
                {
                        &quot;_id&quot; : ObjectId(&quot;5f3689274d42720edb5be327&quot;),
                        &quot;siteCode&quot; : &quot;ES&quot;,
                        &quot;startDate&quot; : ISODate(&quot;2020-02-10T06:00:00Z&quot;),
                        &quot;endDate&quot; : ISODate(&quot;2020-02-20T06:00:00Z&quot;),
                        &quot;status&quot; : &quot;UNSUBSCRIBED&quot;
                },
                ...
            ]
}

Now I want to find and modify the element in the siteCampaigns.
I use this query:
db.campaign.findAndModify({
    query: {
        code: &quot;CK&quot;, 'siteCampaigns.siteCode': &quot;IT&quot;
    }, 
    update: {
        &quot;$set&quot;: {
            'siteCampaigns.$.endDate': ISODate(&quot;2020-08-30T00:00:00Z&quot;)
        }
    }, 
    upsert:false
})

I execute it for the first time, I find the document, no error, but the field is not modified; when I repeat it, it finds the same document, now updated.
I always have to execute it twice to take effect. Why? I use mongo shell.
Find the doc..

The 'findAndModify' command modifies and returns a single document. By default, the returned document does not include the modifications made on the update. To return the document with the modifications made on the update, use the new option.

About new:

Optional. When true, returns the modified document rather than the original. The findAndModify method ignores the new option for remove operations. The default is false. |

https://docs.mongodb.com/manual/reference/command/findAndModify/
So I should have done:
db.campaign.findAndModify({
    query: {
        code: &quot;CK&quot;, 'siteCampaigns.siteCode': &quot;IT&quot;
    }, 
    update: {
        &quot;$set&quot;: {
            'siteCampaigns.$.endDate': ISODate(&quot;2020-08-30T00:00:00Z&quot;)
        }
    }, 
    upsert:false,
    new: true
})