Skip to content

Instantly share code, notes, and snippets.

@azylman
Created March 12, 2013 01:40
Show Gist options
  • Save azylman/5139583 to your computer and use it in GitHub Desktop.
Save azylman/5139583 to your computer and use it in GitHub Desktop.
var DM, Document, PM, Property, Schema, assert, async, docs, mongoose;
mongoose = require('mongoose');
Schema = mongoose.Schema;
async = require('async');
assert = require('assert');
mongoose.connect('localhost', 'simple_test');
Document = new Schema({
name: {
type: String,
required: true
}
});
Property = new Schema({
data: {
type: Schema.Types.Mixed,
required: true,
"default": {}
},
doc: {
type: Schema.ObjectId,
required: true,
index: true,
ref: 'Document'
}
});
Property.pre('save', function(next) {
console.log('pre save', this);
return next();
});
Property.post('save', function(doc, count) {
return console.log('post save', doc);
});
DM = mongoose.model('Document', Document);
PM = mongoose.model('Property', Property);
docs = [];
docs.push(new DM({
name: 'doc1'
}));
docs.push(new DM({
name: 'doc2'
}));
async.waterfall([
function(cb_wf) {
return async.forEach(docs, (function(doc, cb_fe) {
return doc.save(cb_fe);
}), cb_wf);
}, function(cb_wf) {
var prop;
console.log("CREATING PROP 1");
prop = new PM({
data: {},
doc: docs[0]._id
});
return prop.save(cb_wf);
}, function(prop, count, cb_wf) {
return PM.findById(prop._id, cb_wf);
}, function(prop, cb_wf) {
assert.deepEqual(prop.data, {}, "got " + (JSON.stringify(prop.data)) + " instead of {}");
console.log("MODIFYING PROP 1");
prop.data.my_key = 'my_value';
prop.markModified('data');
return prop.save(cb_wf);
}, function(prop, count, cb_wf) {
return PM.findById(prop._id, cb_wf);
}, function(prop, cb_wf) {
assert.deepEqual(prop.data, {
my_key: 'my_value'
}, "got " + (JSON.stringify(prop.data)) + " instead of {my_key: 'my_value'}");
console.log("CREATING PROP 2");
prop = new PM({
data: {},
doc: docs[1]._id
});
return prop.save(cb_wf);
}, function(prop, count, cb_wf) {
return PM.findById(prop._id, cb_wf);
}
], function(err, prop) {
assert.deepEqual(prop.data, {}, "got " + (JSON.stringify(prop.data)) + " instead of {} for property " + (JSON.stringify(prop.toJSON())));
return console.log("Done!");
});
@azylman
Copy link
Author

azylman commented Mar 12, 2013

Produces:

CREATING PROP 1
pre save { doc: 513e875c010fd2c841000001, _id: 513e875c010fd2c841000003 }
post save { __v: 0,
  doc: 513e875c010fd2c841000001,
  _id: 513e875c010fd2c841000003 }
MODIFYING PROP 1
pre save { doc: 513e875c010fd2c841000001,
  _id: 513e875c010fd2c841000003,
  __v: 0,
  data: { my_key: 'my_value' } }
post save { doc: 513e875c010fd2c841000001,
  _id: 513e875c010fd2c841000003,
  __v: 0,
  data: { my_key: 'my_value' } }
CREATING PROP 2
pre save { doc: 513e875c010fd2c841000002, _id: 513e875c010fd2c841000004 }
post save { __v: 0,
  doc: 513e875c010fd2c841000002,
  _id: 513e875c010fd2c841000004 }

/Users/azylman/Git/node_modules/mongoose/lib/utils.js:413
        throw err;
              ^
AssertionError: got {"my_key":"my_value"} instead of {} for property {"doc":"513e875c010fd2c841000002","_id":"513e875c010fd2c841000004","__v":0,"data":{"my_key":"my_value"}}
    at Promise.<anonymous> (/Users/azylman/Git/test/simple_test_case_maybe.js:94:10)
    at Promise.addBack (/Users/azylman/Git/node_modules/mongoose/lib/promise.js:128:8)
    at Promise.EventEmitter.emit (events.js:96:17)
    at Promise.emit (/Users/azylman/Git/node_modules/mongoose/lib/promise.js:66:38)
    at Promise.complete (/Users/azylman/Git/node_modules/mongoose/lib/promise.js:77:20)
    at Query.findOne (/Users/azylman/Git/node_modules/mongoose/lib/query.js:1533:15)
    at model.Document.init (/Users/azylman/Git/node_modules/mongoose/lib/document.js:229:11)
    at model.init (/Users/azylman/Git/node_modules/mongoose/lib/model.js:192:36)
    at Query.findOne (/Users/azylman/Git/node_modules/mongoose/lib/query.js:1531:12)
    at exports.tick (/Users/azylman/Git/node_modules/mongoose/lib/utils.js:408:16)

As you can see, the second property is stored in to mongo correctly according to its pre and post save handlers, but findById adds the same data property that we saved to the first property.

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