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

setDefaultsOnInsert doesn't work if update is empty #3835

Closed
vkarpov15 opened this issue Feb 2, 2016 · 3 comments
Closed

setDefaultsOnInsert doesn't work if update is empty #3835

vkarpov15 opened this issue Feb 2, 2016 · 3 comments
Milestone

Comments

@vkarpov15
Copy link
Collaborator

var assert = require('assert');
var mongoose = require('mongoose');

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

mongoose.set('debug', true);

var schema = new mongoose.Schema({
  test: { type: Number, default: 8472 },
  name: String
});

var MyModel = mongoose.model('MyModel', schema);

MyModel.update({}, { }, { setDefaultsOnInsert: true, upsert: true }, function(error) {
  assert.ifError(error);
  MyModel.findOne({}, function(error, doc) {
    assert.ifError(error);
    console.log(doc);
  });
});
@vkarpov15 vkarpov15 added this to the 4.4.1 milestone Feb 2, 2016
@vkarpov15 vkarpov15 modified the milestones: 4.4.2, 4.4.1, 4.4.3, 4.4.4 Feb 3, 2016
@vkarpov15 vkarpov15 modified the milestones: 4.4.5, 4.4.4 Feb 15, 2016
@vkarpov15 vkarpov15 modified the milestones: 4.4.6, 4.4.5 Feb 23, 2016
@vkarpov15 vkarpov15 modified the milestones: 4.4.7, 4.4.6 Mar 3, 2016
@vkarpov15 vkarpov15 modified the milestones: 4.4.8, 4.4.7, 4.4.9 Mar 11, 2016
@vkarpov15 vkarpov15 modified the milestones: 4.4.10, 4.4.9, 4.4.11 Mar 23, 2016
@vkarpov15 vkarpov15 modified the milestones: 4.4.12, 4.4.11, 4.4.13 Apr 2, 2016
@yinbucheng
Copy link

How to write mongodb grammar? I still make the same mistake

@yinbucheng
Copy link

db.dataReference.update({ "dataId": "test" },
{ "$inc": { "referCnt": 1 },
"$set": { "updatedTimestamp": "1652257641341" },
"$setOnInsert": { "createdTimestamp": "1652257641341" ,"referCnt":100 }}
)

=============
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 40,
"errmsg" : "Updating the path 'referCnt' would create a conflict at 'referCnt'"
}
})
4.4.6

@vkarpov15
Copy link
Collaborator Author

@yinbucheng I'd recommend doing a separate update with $setOnInsert. So await db.dataReference.updateOne({ dataId: 'test' }, { $setOnInsert: { referCnt: 100 } }, { upsert: true }) followed by DataReference.update({}, { $inc: { referCnt: 1 } }) without the $setOnInsert or upsert.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants