Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Created June 2, 2012 18:49
Show Gist options
  • Save aheckmann/2859582 to your computer and use it in GitHub Desktop.
Save aheckmann/2859582 to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('localhost', 'testing_testmodelfindtoObject');
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 () {
A.create({ name: '1' }, {name: '2' }, function (err) {
if (err) return console.error(err.stack||err);
A.find(function (err, docs) {
if (err) console.error(err.stack||err);
console.error(JSON.stringify(docs)); // [{"name":"1","_id":"4fca5e1c201c42e893000001"},{"name":"2","_id":"4fca5e1c201c42e893000002"}]
var vanilla = docs.map(function(d){ return d.toObject() });
vanilla[0].asdf= 23;
console.error('vanilla', JSON.stringify(vanilla)); // [{"name":"1","_id":"4fca5e1c201c42e893000001","asdf":23},{"name":"2","_id":"4fca5e1c201c42e893000002"}]
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