Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Created May 14, 2012 17:13
Show Gist options
  • Save aheckmann/2695130 to your computer and use it in GitHub Desktop.
Save aheckmann/2695130 to your computer and use it in GitHub Desktop.
var mongoose = require('./../mongoose');
console.error(
'\n===========', ' mongoose version: ', mongoose.version, '========\n\n'
);
var Schema = mongoose.Schema;
mongoose.connect('localhost', 'testing_knowIfItemIsAdded');
mongoose.connection.on('error', function () {
console.error('connection error', arguments);
});
var schema = new Schema({
_users: [Schema.ObjectId]
});
var A = mongoose.model('A', schema);
mongoose.connection.on('open', function () {
var id = new mongoose.Types.ObjectId;
var id2 = new mongoose.Types.ObjectId;
var a = new A({ _users: [id] });
a.save(function (err, a) {
if (err) return console.error(err.stack||err);
A.update({_id: a._id, _users: {$ne: id2}}, {$addToSet: {_users: id2}}, function (err, numUpdated) {
if (err) return console.error(err.stack||err);
console.error('updated %d docs', numUpdated);
A.findById(a, function (err, doc) {
if (err) console.error(err.stack||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