Update a record where _id = :id with Mongoose

According to this question and this other one, the Mod on _id is not allowed occurs when one tries to update an object based on its id without deleting it first.

I also found this github issue which tries to explain a solution. It explicitly states:

Be careful to not use an existing model instance for the update clause (this won't work and can cause weird behavior like infinite loops). Also, ensure that the update clause does not have an _id property, which causes Mongo to return a "Mod on _id not allowed" error.

The solution, it seems, is to do the following:

var upsertData = my_visit.toObject();

console.log(req.body.id); // OK

delete upsertData._id;

models.visits.update({ _id: req.body.id }, upsertData, { multi: false }, function(err) {
    if(err) { throw err; }
    //...
}

On a side note, you can probably rewrite your route to do both the create and update without the if-else clause. update() takes an extra option upsert, which, according to the docs:

upsert (boolean) whether to create the doc if it doesn't match (false)