admin管理员组文章数量:1316422
According to The version field __v is suppose to change when array elements are shifted out of their original position.
I run a test code (Mongoose version 3.8.15):
var mongoose = require('mongoose');
var db = mongoose.connection;
mongoose.connect('mongodb://localhost:27017/node_test');
db.on('error', console.error.bind(console, 'connection error:'));
var testSchema = mongoose.Schema({
name: String,
arr: [Number]
})
var Test = mongoose.model('Test', testSchema);
var t = Test();
t.name = 'hi'
t.arr = [1, 2, 3, 4, 5, 6];
t.save(function (err, result) {
console.log(result)
Test.update({'name': 'hi'}, {$pull: {'arr': 3}}, function(err2, result2) {
console.log(result2)
Test.find({'name': 'hi'}, function(err3, result3) {
console.log(result3);
db.close();
});
});
});
Output:
{ __v: 0,
name: 'hi',
_id: 53f594a0113832871c2eea89,
arr: [ 1, 2, 3, 4, 5, 6 ] }
1
[ { _id: 53f594a0113832871c2eea89,
name: 'hi',
__v: 0,
arr: [ 1, 2, 4, 5, 6 ] } ]
So, number 3 is removed which introduced a disruptive change to the array if any code is to attempt to access it by the position of its index. Why isn't the version incremented?
According to http://aaronheckmann.tumblr./post/48943525537/mongoose-v3-part-1-versioning The version field __v is suppose to change when array elements are shifted out of their original position.
I run a test code (Mongoose version 3.8.15):
var mongoose = require('mongoose');
var db = mongoose.connection;
mongoose.connect('mongodb://localhost:27017/node_test');
db.on('error', console.error.bind(console, 'connection error:'));
var testSchema = mongoose.Schema({
name: String,
arr: [Number]
})
var Test = mongoose.model('Test', testSchema);
var t = Test();
t.name = 'hi'
t.arr = [1, 2, 3, 4, 5, 6];
t.save(function (err, result) {
console.log(result)
Test.update({'name': 'hi'}, {$pull: {'arr': 3}}, function(err2, result2) {
console.log(result2)
Test.find({'name': 'hi'}, function(err3, result3) {
console.log(result3);
db.close();
});
});
});
Output:
{ __v: 0,
name: 'hi',
_id: 53f594a0113832871c2eea89,
arr: [ 1, 2, 3, 4, 5, 6 ] }
1
[ { _id: 53f594a0113832871c2eea89,
name: 'hi',
__v: 0,
arr: [ 1, 2, 4, 5, 6 ] } ]
So, number 3 is removed which introduced a disruptive change to the array if any code is to attempt to access it by the position of its index. Why isn't the version incremented?
Share Improve this question asked Aug 21, 2014 at 6:44 huggiehuggie 18.2k29 gold badges86 silver badges142 bronze badges1 Answer
Reset to default 5The author of the article wasn't very clear when the version increment will be internally applied, because as you found out the version field is not updated when you're using the update mand.
If you replace the update mand with Mongoose pull method on your array the version field will be incremented:
var t = Test();
t.name = 'hi'
t.arr = [1, 2, 3, 4, 5, 6];
t.save(function (err, result) {
console.log(result);
// use Mongoose pull method on the array
t.arr.pull(3);
t.save(function(err2, result2) {
console.log(result2)
});
});
Results:
{ __v: 0,
name: 'hi',
_id: 53f59d2a6522edb12114b98c,
arr: [ 1, 2, 3, 4, 5, 6 ] }
{ __v: 1,
name: 'hi',
_id: 53f59d2a6522edb12114b98c,
arr: [ 1, 2, 4, 5, 6 ] }
Edit:
The update method on the model basically only builds and executes the query. The version checking / incrementing is done when you use the save method
本文标签: javascriptMongoose v when does it changeStack Overflow
版权声明:本文标题:javascript - Mongoose __v when does it change - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741985951a2408677.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论