__v field causing mongo errors
See original GitHub issueI’m getting this error when saving a document:
{
name: "MongoError",
err: "Field name duplication not allowed with modifiers",
code: 10150,
n: 0,
connectionId: 1,
ok: 1
}
the mongo diaglog shows that the query in question did this:
update flags: 0 q: {
__v: 11,
_id: ObjectId('527a7c1ca2d740dd0f000004')
}
o: {
$set: {
__v: 11,
aaaa.bbbbb: [{
_id: ObjectId('52a115780594725664000001'),
type: "xxxxx",
name: "xxxxxxxxxxxxxxxx"
}],
cccc.ddddd: [{
_id: ObjectId('527136c893312c254a0000b7'),
type: "xxxxx",
name: "xxxxxxx"
}, {
_id: ObjectId('52a115780594725664000001'),
type: "xxxxx",
name: "xxxxxxxxxxxxxxx"
}],
tttttt: new Date(1393008423258)
},
$inc: {
__v: 1
}
}
Not that it’s both {$set: {__v: 11}}
and {$inc: {__v:1}}
, which mongo does not like.
Issue Analytics
- State:
- Created 10 years ago
- Comments:8
Top Results From Across the Web
let MongoDB return errors instead of preventing them
Hey @Prasad_Saya , I tried to add the “err instanceof MongoServerError” check as yours, but I got the error “MongoServerError is not defined”...
Read more >The field 'unique' is not valid for an _id index specification ...
We're using the Mongoose Node.js wrapper to create the Mongo indexes, so not adding the unique and background attributes isn't an option. Cheers!...
Read more >Mongoose v6.8.2: Validation
You can manually mark a field as invalid (causing validation to fail) by using doc.invalidate(...) Validators are not run on undefined values. The...
Read more >errors – Exceptions raised by the pymongo package
Exceptions raised by PyMongo. ... Raised when a connection to the database is lost and an attempt to auto-reconnect will be made. In...
Read more >Fullstack part3 | Saving data to MongoDB - Full stack open
We also don't want to return the mongo versioning field __v to the frontend. ... method will throw an error causing the returned...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
While I was trying to write a test case I realized the source of the error. We send the document as JSON to the client, where it is modified, then the client sends it back and we merge any changed data with a clean copy from the database, then save it.
the
$set : __v
part of the invalid query comes from the merge, and the$inc : __v
comes from mongoose internally, because an array field was modified.This problem can easily be solved by our code simply ignoring the __v field during the merge.
That said, if setting the __v field causes difficult to trace errors (“Field name duplication not allowed with modifiers” is all the information available without going to the mongo diaglog) perhaps a preemptive warning of some sort would be appropriate.
The simplest way to reproduce this error:
Please check mongoose version compatibility with MongoDB version: http://mongoosejs.com/docs/compatibility.html
There is a possibility that the MongoDB version you are using require higher Mongoose version.
In my case I use MLab MongoDB version 3.3.6 with Mongoose version 4 - Upgrading to Mongoose 5 solve the issue.