Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"update" and "findByIdAndUpdate" don't increment "__v" #1265

Closed
mjhm opened this issue Dec 30, 2012 · 5 comments
Closed

"update" and "findByIdAndUpdate" don't increment "__v" #1265

mjhm opened this issue Dec 30, 2012 · 5 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.

Comments

@mjhm
Copy link

mjhm commented Dec 30, 2012

The following example creates three documents and attempts to force the update of "__v" by altering the "dummy" array. It works in the first case with a "findOne/save" construct, but doesn't work for the atomic "update" and "findByIdAndUpdate" functions.

Since it is possible for find/save constructs to be interleaved with atomic updates, it seems that it the "update" functions should also increment "__v".

This came up in the context of: http://stackoverflow.com/questions/14079693/mongoose-findbyidandupdate-or-update-and-increment-how-to-increment-v An acceptable work around might be to allow "__v" to be incremented directly in an "update".

#! /usr/bin/node

var mongoose = require('mongoose');
var async = require('async');
mongoose.connect('mongodb://localhost/test');

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));

var kittySchema = mongoose.Schema({
    name: String,
    dummy: Array
});

var Kitten = mongoose.model('Kitten', kittySchema);

var fluffy1 = new Kitten({ name: 'fluffy1', dummy: [1] }),
    fluffy2 = new Kitten({ name: 'fluffy2', dummy: [1] }),
    fluffy3 = new Kitten({ name: 'fluffy3', dummy: [1] }),

    fluffy2Id = null,
    fluffy3Id = null;


async.series([
    function (openDone) {
        db.once('open', function () {
            openDone();
        });
    },
    function (fluffy1SaveDone) {
        fluffy1.save(function (err) {
            if (err) {
                console.log(err);
            }
            fluffy1SaveDone(err);
        });
    },

    function (fluffy2SaveDone) {
        fluffy2.save(function (err, kitty) {
            if (err) {
                console.log(err);
            }
            fluffy2Id = kitty._id;
            fluffy2SaveDone(err);
        });
    },

    function (fluffy3SaveDone) {
        fluffy3.save(function (err, kitty) {
            if (err) {
                console.log(err);
            }
            fluffy3Id = kitty._id;
            fluffy3SaveDone(err);
        });
    },

    function (nonAtomicTestDone) {
        Kitten.findOne({ name: /^fluffy1/ }, function (err, kitty) {
            console.log(kitty);
            kitty.dummy = [2];
            kitty.save(function (err) {
                nonAtomicTestDone(err);
            });
        });
    },

    function (updateTestDone) {
        Kitten.update({_id: fluffy2Id}, {$set: {dummy: [2]}}, 
            function (err) {
                updateTestDone(err);
            }
        );
    },

    function (findByIdTestDone) {
        Kitten.findByIdAndUpdate(fluffy3Id, {$set: {dummy: [2]}},
            function (err) {
                findByIdTestDone(err);
            }
        );
    }],

    function (err) {
        if (err) {
            console.log(err);
        }
        process.exit(0);
    }
);

RESULT:

{ "name" : "fluffy1", "_id" : ObjectId("50e09e0be3013e2811000001"), "dummy" : [ 2 ], "__v" : 1 }
{ "name" : "fluffy2", "_id" : ObjectId("50e09e0be3013e2811000002"), "dummy" : [ 2 ], "__v" : 0 }
{ "name" : "fluffy3", "_id" : ObjectId("50e09e0be3013e2811000003"), "dummy" : [ 2 ], "__v" : 0 }
@aheckmann
Copy link
Collaborator

Versioning was implemented to mitigate the doc.save() use case and is working as designed (not Model.update etc). One could simply send the following instead:

{$set: {dummy: [2]}, $inc: { __v: 1 }}

@mjhm
Copy link
Author

mjhm commented Jan 8, 2013

I'm not completely clear: Is the intent that "update" should never increment "__v" ? For example this apparently does not increment __v either:

Kitten.update({_id: fluffy2Id}, {$set: {dummy: [2]}, $inc: {__v: 1}},  function ()...);

Not a big deal since this is a corner case, but I'd like to have clarity.

@aheckmann
Copy link
Collaborator

looks like a bug.

aheckmann added a commit that referenced this issue Feb 12, 2013
@edwardstudy
Copy link

Hey,in my app.this issue is still stay.the mongoose version is 3.8.22.
sorce code:
Model schema
Route excute
As you can see, The schema is long and complicated.
In past, I used update can not increment the '__v' value like this

Dtree.update({"_id": id}, frontData, function(err){
    if(err){
        console.log('Error: updateDTree: DB failed to update due to ', err);
        res.send({'success':false, 'err':err});
    }else{
                console.log('Info: updateDTree: DB updated successully dtree');
                res.send({'success':true});
    }
});

so i it and use .update({}, {, $inc: { __v: 1}}, callback) in my code.Thx for your help

@vkarpov15
Copy link
Collaborator

Hi @edwardstudy, can you clarify what you mean? I don't quite understand. Could you perhaps provide a standalone file or mocha test that reproduces your issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

4 participants