Created
August 10, 2011 14:54
-
-
Save aheckmann/1137004 to your computer and use it in GitHub Desktop.
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 characters
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var ObjectId = mongoose.Types.ObjectId; | |
console.error(mongoose.version); | |
var ResponseSchema = new Schema({}); | |
var AnswerSchema = new Schema({ | |
answer : String | |
, responses: [ResponseSchema] | |
}); | |
function lengthValidator (v) { | |
return v && !!v.length; | |
} | |
var QuestionSchema = new Schema({ | |
question: {type: String, validate: [lengthValidator, "can't be blank."]} | |
, answers : [AnswerSchema] | |
}); | |
var db = mongoose.createConnection('mongodb://localhost/question_'+ Date.now()); | |
var Question = db.model('Question', QuestionSchema); | |
var q = new Question({ question: 'Change Me', answers: [{answer: 'Dr Pepper'}] }); | |
q.save(function (err) { | |
if (err) return done(err); | |
console.error('saved question', q); | |
var params = { | |
question: 'TESTING' | |
, answers:[ | |
{ answer: 'Coke' } | |
, { answer: 'Pepsi' } | |
, { answer: '' } | |
] | |
} | |
var id = q.id; | |
Question.update({_id : id}, params, function (err) { | |
if (err) return done(err); | |
Question.collection.findOne({ _id: new ObjectId(id) }, function (err, question) { | |
//Question.findById(id, function (err, question) { | |
if (err) return done(err); | |
console.error('from db', question); | |
done(); | |
}); | |
}); | |
}); | |
function done (err) { | |
console.error('calling done'); | |
if (err) console.error(err); | |
db.db.dropDatabase(function (err) { | |
db.close(); | |
console.error('done'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment