Moving directors, actors and writer under new field cast. HELP

is this right

db.movieDetails.bulkWrite(
[
{updateMany:
{
{“director”: cast.director}, {$set : cast}, {upsert: true}
}
},
{updateMany:
{
{“actors”: cast.actors}, {$set : cast}, {upsert: true}
}
},
{updateMany:
{
{“writers”: cast.writers}, {$set : cast}, {upsert: true}
}
},
]
)

and can someone please explain why he used the query “imdb.id”: details.imdb.id

i’m using this code because db.movieDetails.updateOne({“imdb.id”: detail.imdb.id}, {$set: detail}, {upsert: true}) didn’t work

Hi @jama_18656,

Here, the detail object contains the documents that needs to be inserted in the collection movieDetails and we are looping through these documents and checking the following query for each of the documents:

This is the filter to check if the document in the detail object is already present in the collection.

This $set operator will replace the existing document with the new one, if the document in the detail object is already present in the collection.

If the detail data is not present in the collection, this option will insert a new document.

Hence, to eliminate data duplicity, we set upsert to true. So if the document is not present, it will insert the new document and if the document exists, it will replace the document.

I hope it helps!!

Please let me know, if you have any issue.

Thanks,
Sonali