Update adding "Undefined" value

Hello, I am writing this message in case you can help me.
I want to update all the documents (movies) that have the empty cast array, adding a new element inside the array with Undefined value.
Thanks in advance!

Hi @Josjoaqun1991
if i understand your request, you can use this query for example:

> db.test.updateMany({"array":{$size:0}},{$push:{"array":{value:"undefined"}}})
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }

Above there is the result of this operation:

> db.test.find().pretty()
{
        "_id" : ObjectId("63b9d3114274ac5214a44331"),
        "array" : [
                {
                        "value" : "undefined"
                }
        ]
}
{
        "_id" : ObjectId("63b9d32f4274ac5214a44332"),
        "array" : [
                {
                        "value" : "undefined"
                }
        ]
}

Best Regards

1 Like

Thank you very much @Fabio_Ramohitaj !

Another question…
Before, I want to check how many films have empty the array “cast”

I have used this code , Do you think is correct?

db.dataset.count({ cast: { $exists: true, $not: {$size: 0} } })

Hi @Jose_jimenez1,
Looking it quickly, it looks likes correct for me!

Best Regards