2

I have x local registers which synchronise with a central one. I want to either modify the old doc or insert a new one into the central register. I rename the original id in the central register to avoid id conflicts.

I also need the new doc as a return value, so that I can store the _idof the central register in my local register.

Thats why I use:

PatientModel.findOneAndUpdate({
    refId: new ObjectId(patient.refId), 
    institute: new ObjectId(patient.institute), 
    deleted: false
}, patient, { 'upsert':true, 'new': true }, (err, patientFound) ->

But I get this error:

{ [MongoError: exception: Cannot update '__v' and '__v' at the same time]
  name: 'MongoError',
  message: 'exception: Cannot update \'__v\' and \'__v\' at the same time',
  errmsg: 'exception: Cannot update \'__v\' and \'__v\' at the same time',
  code: 16836,
  ok: 0 }

If I transform the bson objects into javascript objects first and then delete the __v property, the error does not appear. I only have docs with __v == 0. I don't think it is a good solution because I may need the __v property later.

Questions

1) What is causing this error 2) How to handle this

I found this but I don't understand why this should help: Mongoose error: Cannot update __v and __v at the same time

Community
  • 1
  • 1
Andi Giga
  • 3,744
  • 9
  • 38
  • 68
  • 1
    You seem to be just throwing the whole `patient` object at the update. You really should not be doing that. MongoDB updates are largely intended to use operators like [`$set`](https://docs.mongodb.org/v3.0/reference/operator/update/set/) directly on only the fields you actually wish to change. – Blakes Seven Nov 10 '15 at 09:20
  • 2
    As for the error, it seems you have versioning turned on. And as a side effect of what I just said is that there is both a `$set` and an `$inc` trying to be applied to `__v` at the same time. So avoid that by only applying the `$set` to just the fields you want to modify. – Blakes Seven Nov 10 '15 at 09:23
  • Ok, how will I do that if I don't know which fields will be modified? It could be the whole doc if not existent, multiple fields or just one field. – Andi Giga Nov 10 '15 at 10:39
  • This is why 90% of your own personal questions remain unanswered. They are way to broad with way to many possibilities. All of these interactions are dependent on handling of various "client/server" levels. And there is nothing specfic in your questions to how you are handling this. Best guess is you are not handling it at all not understanding this is what you need to do nor the basics of how to implement it. Hence, you are asking the wrong questions if you do not understand these things first. – Blakes Seven Nov 10 '15 at 11:04
  • 2
    I don't handle it because as I wrote I don't really know what it means or why it is caused. If the question is to broad, please tell me what you need to know and I will try to answer it. – Andi Giga Nov 10 '15 at 15:35

0 Answers0