question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

__v field causing mongo errors

See original GitHub issue

I’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:closed
  • Created 10 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
dillonkrugcommented, Feb 22, 2014

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:

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/test');

var Test = mongoose.model('Test', new mongoose.Schema({
    id: String,
    field: [{}],
}));

var doc = new Test();
doc.save(function(err) {
    Test.findOne(function(err, doc) {
        doc.__v = 123;
        doc.field.push({_id: 'xxxxx', type: 'nnn'});
        doc.save(function(err) {
            console.log(err);
        })
    })
})
0reactions
chenopcommented, Jul 23, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found