Skip to content

Instantly share code, notes, and snippets.

@sethblanchard
Last active November 29, 2015 02:38
Show Gist options
  • Save sethblanchard/78ba49e4904c7f94db8d to your computer and use it in GitHub Desktop.
Save sethblanchard/78ba49e4904c7f94db8d to your computer and use it in GitHub Desktop.
Node Replace
var mongo = require('mongoskin');
var db = mongo.db("mongodb://localhost/learnmongo");
db.bind('sample');
var test1 = {
first: 'Jane',
last: 'Doe',
sex: 'female'
}
var test2 = {
first: 'John',
last: 'Doe',
sex: 'male'
}
db.sample.insert(test1, function(err, result){
console.log('=== Initial Result ===');
console.log(err, result);
var objId = mongo.helper.toObjectID(result.ops[0]._id)
db.sample.replaceOne(
{_id: objId},
test2,
{upsert: false},
function(err, rslt){
console.log('=== Update Result ===');
console.log(err, rslt.result);
return;
}
);
});
@sethblanchard
Copy link
Author

This returns

=== Initial Result ===
null { result: { ok: 1, n: 1 },
  ops:
   [ { first: 'Jane',
       last: 'Doe',
       sex: 'female',
       _id: 565a62b6f573360649e79ebb } ],
  insertedCount: 1,
  insertedIds: [ 565a62b6f573360649e79ebb ] }
=== Update Result ===
null { ok: 1, nModified: 0, n: 0 }

Why doesn't the second record replace the first record? What am I missing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment