Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Created September 20, 2012 15:00
Show Gist options
  • Save aheckmann/3756437 to your computer and use it in GitHub Desktop.
Save aheckmann/3756437 to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var assert = require('assert')
console.log('\n===========');
console.log(' mongoose version: %s', mongoose.version);
console.log('========\n\n');
var dbname = 'testing_objectidNull';
console.log('dbname: %s', dbname);
mongoose.connect('localhost', dbname);
mongoose.connection.on('error', function () {
console.error('connection error', arguments);
});
var schema = new Schema({
name: String
, oid: { type: Schema.Types.ObjectId, default: null }
});
var A = mongoose.model('A', schema);
mongoose.connection.on('open', function () {
var a = new A({ name: 'null objectId' });
a.save(function (err, a) {
if (err) return done(err);
var b = new mongoose.Types.ObjectId;
A.findOneAndUpdate({_id : a._id },{oid : b },function(err,doc){
console.log('updated', doc);
done(err);
});
})
});
function done (err) {
if (err) console.error(err.stack);
mongoose.connection.db.dropDatabase(function () {
mongoose.connection.close();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment