Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Created May 21, 2012 21:53
Show Gist options
  • Save aheckmann/2764948 to your computer and use it in GitHub Desktop.
Save aheckmann/2764948 to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('localhost', 'testing_oops');
mongoose.connection.on('error', function () {
console.error(arguments);
});
var schema = new Schema({
name: String
});
var A = mongoose.model('A', schema);
mongoose.connection.on('open', function () {
var a = new A({ name: 'oops' });
a.save(function (err, a) {
if (err) return console.error(err.stack||err);
/* // option 1
var b = new A;
b.init({ _id: a.id });
b.set({ name: 'new value' });
b.save(function (err) {
if (err) throw err;
A.findById(b, function (err, doc) {
if (err) throw err;
console.error(doc);
mongoose.connection.db.dropDatabase(function () {
mongoose.connection.close();
});
});
});
*/
// option 2
A.update({ _id: a._id }, { $set: { name: 'new val' }}, function (err) {
A.findById(a, function (err, doc) {
if (err) throw err;
console.error(doc);
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