Last active
December 21, 2015 09:49
-
-
Save aheckmann/6287665 to your computer and use it in GitHub Desktop.
Revisions
-
aheckmann revised this gist
Aug 20, 2013 . 1 changed file with 21 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ var mongoose = require('mongoose'); var Schema = mongoose.Schema; var assert = require('assert') console.log('\n==========='); console.log(' mongoose version: %s', mongoose.version); @@ -12,37 +13,44 @@ mongoose.connection.on('error', function () { console.error('connection error', arguments); }); var AddressSchema = Schema({ street: String, city: String, zip: Number }) var schema = new Schema({ name: String , addresses: { type: [AddressSchema], validate: validator } }); function validator (array) { var bad = []; var ok = array.every(function (address) { if (!address.isModified()) return true; if (!address.city /*etc */) { bad.push(address); return false; } return true }) if (!ok) { this.invalidate('addresses', 'missing value', bad) } return true; } var A = mongoose.model('A', schema); mongoose.connection.on('open', function () { var a = new A({ name: '1652' , addresses: [{ street: '5 What Street', city: 'Palo Alto' }] }); a.save(function (err) { if (err) { @@ -52,7 +60,15 @@ mongoose.connection.on('open', function () { A.findById(a, function (err, doc) { console.log('found', doc); doc.addresses[0].city = ''; doc.save(function (err) { if (err) { console.log(err) // error! return done(); } done(); }) }) }) }); -
aheckmann created this gist
Aug 20, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,64 @@ var mongoose = require('mongoose'); var Schema = mongoose.Schema; console.log('\n==========='); console.log(' mongoose version: %s', mongoose.version); console.log('========\n\n'); var dbname = 'goosetest-1652'; mongoose.connect('localhost', dbname); mongoose.connection.on('error', function () { console.error('connection error', arguments); }); var AddressSchema = Schema({ street: String, city: String, zip: Number }) var schema = new Schema({ name: String , addresses: { type: [AddressSchema], validate: validator } }); function validator (array) { var bad = []; var ok = array.every(function (address) { if (!address.city /*etc */) { bad.push(address); return false; } return true }) if (!ok) { this.invalidate('addresses', bad) } return true; } var A = mongoose.model('A', schema); mongoose.connection.on('open', function () { var a = new A({ name: '1652' }); a.addresses.push({ street: '123 Fake Street' }); a.addresses.push({ street: '5 What Street', city: 'Palo Alto' }); a.save(function (err) { if (err) { console.log(err) return done(); } A.findById(a, function (err, doc) { console.log('found', doc); done(); }) }) }); function done () { mongoose.connection.db.dropDatabase(function () { mongoose.connection.close(); }); }