Created
October 1, 2012 22:52
-
-
Save aheckmann/3814988 to your computer and use it in GitHub Desktop.
This file contains 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 characters
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var assert = require('assert') | |
console.log('\n==========='); | |
console.log(' mongoose version: %s', mongoose.version); | |
console.log('========\n\n'); | |
var dbname = 'testing_gg-139a368323237477'; | |
console.log('dbname: %s', dbname); | |
mongoose.connect('localhost', dbname); | |
mongoose.connection.on('error', function () { | |
console.error('connection error', arguments); | |
}); | |
mongoose.set('debug', true) | |
var StandardDef = { | |
standard: {type: Schema.ObjectId, ref:"Standard", required:false}, | |
code: String, | |
description: String | |
}; | |
var Question = new Schema({ | |
correct_answer : String, | |
points: Number, | |
question_label: String, | |
standard: StandardDef, | |
}); | |
var AnswerKey = new Schema({ | |
questions : [ Question ] | |
}); | |
var Assignment = new Schema({ | |
name : {type: String, required: true}, | |
description : {type: String, "default": ""}, | |
answer_keys : [AnswerKey] | |
}); | |
// fix the data! | |
Assignment.pre('init', function (next, obj) { | |
var self = this; | |
if (Array.isArray(obj.answer_keys)) { | |
obj.answer_keys.forEach(function (key, idx) { | |
if (key.questions && 'Object' == key.questions.constructor.name) { | |
console.log('must convert to an array', key.questions); | |
var old = key.questions; | |
key.questions = []; | |
var keys = Object.keys(old); | |
var i = keys.length; | |
while (i--) { | |
key.questions[i] = old[i]; | |
} | |
self.markModified('answer_keys.' + idx) | |
} | |
}); | |
} | |
next() | |
}); | |
var A = mongoose.model('A', Assignment); | |
mongoose.connection.on('open', function () { | |
var _id = mongoose.Types.ObjectId("506a113554c4381f96000003"); | |
A.collection.insert({ | |
_id: _id, | |
answer_keys: [{ | |
_id : mongoose.Types.ObjectId("506a113554c4381f96000004"), | |
questions : { | |
"0" : { | |
"correct_answer" : "B", | |
"points" : 1, | |
"question_label" : "1", | |
"standard" : { | |
"code" : null, | |
"description" : null, | |
"standard" : null | |
} | |
} | |
} | |
}], | |
description : "", | |
"name" : "gg-139a368323237477" | |
}, function (err) { | |
if (err) return done(err); | |
A.findById(_id, function (err, doc) { | |
if (err) return done(err); | |
console.log('found: ', doc.answer_keys[0].questions); | |
doc.save(function (err) { | |
if (err) return done(err); | |
console.log('saved'); | |
A.findById(_id, function (err, doc) { | |
if (err) return done(err); | |
console.log('found: ', doc.answer_keys[0].questions); | |
done(err); | |
}) | |
}) | |
}); | |
}) | |
}); | |
function done (err) { | |
if (err) console.error(err.stack); | |
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