-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
$inc a default non 0 value with findOneAndUpdate and upsert true #3617
Comments
Use the |
using
if I use
It doesn't start at 766, but 1, same issue as before. If I remove the |
Oops, I'm using mongo 3.0.0 probably is a bit old :( will update and try again!, sorry & thx |
mongoose 4.1.5, mongodb version shouldn't be an issue right? EDIT: updated everything to latest, still not working. any suggestions? |
No setDefaultsOnInsert should work fine against mongodb 3.0. I'll double check |
I think that is not allowed to use Basically what I want to do is something like this (working example): var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var RefSchema = new Schema({
_id: {type: String, default: 'refs'},
ref: {type: Number, default: 776}
});
RefSchema.statics.increment = function (callback) {
var query = {_id: 'refs'};
var update = {$inc: {ref: 1}};
var options = {upsert: true, 'new': true, setDefaultsOnInsert: true};
this.findOneAndUpdate(query, update, options, callback);
};
var RefModel = mongoose.model('Ref', RefSchema);
var OrderSchema = new Schema({
total: Number,
ref: Number
});
OrderSchema.pre('save', function (next) {
if (!this.isNew) {
return next();
}
var self = this;
RefModel.increment(function (err, doc) {
self.ref = doc.ref;
next(err);
});
});
var OrderModel = mongoose.model('Order', OrderSchema);
var orders = [];
for (var i = 0; i < 100; i++) {
orders.push({total: (i + 1) * 3.4});
}
OrderModel.create(orders, function (err, orders) {
console.log('orders', orders);
}); So this is working but it is starting the Again, what it suspect is that with upsert, it will create the document with the value of the I am trying to find where is this happening.. will update asap if if i find something. anything from your side? |
I found this conversation https://groups.google.com/forum/#!topic/mongodb-user/A93n4zEXClk that relates to this ticket in mongodb https://jira.mongodb.org/browse/SERVER-6566 so far what i understand is that this could be achieved with an array of updates as it is proposed in the jira ticket. |
So if you specify > db.test.updateOne({}, { $inc: { test: 1 }, $setOnInsert: { test: 50 } }, { upsert: true });
2016-02-02T16:24:59.148-0500 E QUERY [thread1] uncaught exception: WriteError({
"index" : 0,
"code" : 16836,
"errmsg" : "Cannot update 'test' and 'test' at the same time",
"op" : {
"q" : {
},
"u" : {
"$inc" : {
"test" : 1
},
"$setOnInsert" : {
"test" : 50
}
},
"multi" : false,
"upsert" : true
}
}) :
undefined As such, there's no good way for mongoose to have sane handling for Also, I opened up a separate issue #3835 that's somewhat related to this one. |
@dciccale, I know this has been too long. But, were you able to find a solution for this thing ? |
hi, i am having trouble starting a counter
n
at a specified number and then increasing by 1 usingfindOneAndUpdate
andupsert: true
.what i expect is the creation of a document with the
n
property starting at776
and then increasing from that value.The text was updated successfully, but these errors were encountered: