Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Created August 10, 2011 14:54
Show Gist options
  • Save aheckmann/1137004 to your computer and use it in GitHub Desktop.
Save aheckmann/1137004 to your computer and use it in GitHub Desktop.
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