Skip to content

Instantly share code, notes, and snippets.

@kjirou
Last active August 29, 2015 14:00
Show Gist options
  • Save kjirou/95abfcade86c1474392b to your computer and use it in GitHub Desktop.
Save kjirou/95abfcade86c1474392b to your computer and use it in GitHub Desktop.
存在した _id が設定済みの doc を save() するとエラー
// 重複エラーが出る
var sandbox = new Sandbox({
_id: ObjectId('既存の_id'),
});
sandbox.someProp = 'New value';
sandbox.save(function(err, sandbox){
console.log(err);
});
//
// -> [MongoError: E11000 duplicate key error index: tomcom.sandboxes.$_id_ dup key: { : ObjectId('既存の_id') }]
//
// これはエラーにならない
Sandbox.findById(ObjectId('既存の_id'), function(err, sandbox){
sandbox.rootName = 'New value';
sandbox.save(function(err, sandbox_){
console.log(sandbox_);
});
});
// new Doc({_id:ObjectId('既存の_id')}) と findById したものは違う
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment