Replace an entire set of documents without losing the old data

Hey! I’ve a usecase where i need to replace a set of documents, let’s take an example this

i’ve movies and users, I get all the movies by the user from an external API and they can’t be uniquely identified, so I need to replace every single one of them everytime.

I thought of embedding it in the users document, but it’s about 100 movies per user, which would not be that performant i think.

so i created a collection named movies, I placed every one of them in there with reference to the user, but now how could I update the user’s entire movies without deleting because that would lead to a short time where the user doesn’t have any movies as I didn’t inserted the new movies yet.

If this was a query, it would look like this:

// Get all the movies by a user and replace them with new ones.
db.movies.replaceMany({ userId: 1 }, { $replaceWith: newMovies })