Skip to content

Instantly share code, notes, and snippets.

@bentruyman
Forked from bnoguchi/mongoose.GH.341.js
Created September 11, 2011 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bentruyman/1209726 to your computer and use it in GitHub Desktop.
Save bentruyman/1209726 to your computer and use it in GitHub Desktop.
mongoose GH-341.js - Response to sebm
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/sebtest');
var Schema = mongoose.Schema;
var StackItemSchema = new Schema({
blob: Number
});
var StackSchema = new Schema({
name: String
, items: [StackItemSchema]
});
var Stack = mongoose.model('Stack', StackSchema);
function addItems (stack) {
console.log("The stack is ...");
console.log(stack);
console.log("\n");
console.log('Gonna add some items now');
stack.items.push({ blob:Math.random() });
stack.items.push({ blob:Math.random() });
stack.items.push({ blob:Math.random() });
stack.items.push({ blob:Math.random() });
console.log("... added an item. stack looks like ...");
console.log(stack);
console.log("\n");
stack.save( function (err, stack) {
if (err) throw err;
console.log("item push persisted to mongodb");
console.log(stack);
console.log("\n");
console.log("removing the item");
while (stack.items[0]) {
stack.items[0].remove();
}
console.log(stack);
console.log("\n");
stack.save( function (err, stack) {
if (err) throw err;
Stack.findOne({name:'The Stack'}, function (err, stack) {
if (err) throw err;
console.log("removal persisted to mongodb, not");
console.log(stack);
mongoose.disconnect();
});
});
});
}
Stack.findOne({name:'The Stack'}, function (err, stack) {
if (err) throw err;
if (!stack) {
return Stack.create({name: 'The Stack'}, function (err, stack) {
if (err) throw err;
addItems(stack);
});
}
return addItems(stack);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment