I have next document
user = {
_id: ObjectID
name: string // it is unique
modified: Time,
field1:
field2:
…
fieldN
}
And I want to add new_user to collection with next logic.
- If user with name equal new_user.name does not exists, then add new_user
- If user with such name exists, but user.modified<new_user.modified, then update user.field1,…,user.fieldN, and user.modified
- If user with such name exists, but user.modified>new_user.modified then do nothing.
It is possible to do this using findOne and then insertOne or updateOne commands. But can be such logic done with single command?
And additional question. I have a bunch of users and I want to do this logic for these users. Can I perform it with single command, like updateMany with some params?