In my chat application, every user has a dictionary of unread messages which looks like this :
Unread Dictionary
{
_id: // reference to their user_id,
events: [] // array of ObjectIds
}
I want to make a mongoose findByIdAndUpdate query where ) search by ID and $push a new ObjectId into the events array.
If the document doesn't exist, I would like for it to upsert the document assigning it the _id used for the update query and initializing the array with the element being $push ed in the update query.
EDIT:
Here is an example of the query I would like to make:
Model.findByIdAndUpdate(user_id, {$push: {events: event_id}}, {upsert: true}, function (err, updatedDoc) {});
If the document doesn't exist and it decides to upsert, will it use the user_id I used to search to assign the _id on the upserted document? OR will mongo assign it's own _id? I'm looking for something that does the former.