Skip to content

Instantly share code, notes, and snippets.

@eladcandroid
Created April 15, 2018 12:18
Show Gist options
  • Save eladcandroid/5ced3eb5d0f9e8e5d77e337895d01c0e to your computer and use it in GitHub Desktop.
Save eladcandroid/5ced3eb5d0f9e8e5d77e337895d01c0e to your computer and use it in GitHub Desktop.
All mongoose commands to teach
const assert = require('assert');
const mongoose = require('mongoose');
// ES6 Promises
mongoose.Promise = global.Promise;
// Connect to mongodb
mongoose.connect('mongodb://localhost/testaroo');
mongoose.connection.once('open', function(){
console.log('Connection has been made, now make fireworks...');
}).on('error', function(error){
console.log('Connection error:', error);
});
const Schema = mongoose.Schema;
// Create a Schema and a Model
const MarioCharSchema = new Schema({
name: String,
weight: Number
});
const MarioChar = mongoose.model('mariochar', MarioCharSchema);
// Drop the characters collection before each test
beforeEach(function(done){
// Drop the collection
mongoose.connection.collections.mariochars.drop(function(){
done();
});
const char = new MarioChar({
name: 'Mario'
});
char.save().then(function(){
assert(!char.isNew);
done();
});
});
// Create tests
it('Finds a record from the database', function(done){
MarioChar.findOne({name: 'Mario'}).then(function(result){
assert(result.name === 'Mario');
done();
});
});
it('Finds a record by unique id', function(done){
MarioChar.findOne({_id: char._id}).then(function(result){
assert(result._id.toString() === char._id.toString());
done();
});
});
it('Deletes a record from the database', function(done){
MarioChar.findOneAndRemove({name: 'Mario'}).then(function(){
MarioChar.findOne({name: 'Mario'}).then(function(result){
assert(result === null);
done();
});
});
});
it('Updates the name of a record', function(done){
MarioChar.findOneAndUpdate({name: 'Mario'}, {name: 'Luigi'}).then(function(){
MarioChar.findOne({_id: char._id}).then(function(result){
assert(result.name === 'Luigi');
done();
});
});
});
it('Adds 1 to the weight of every record', function(done){
MarioChar.update({}, { $inc: { weight: 1 } }).then(function(){
MarioChar.findOne({name: 'Mario'}).then(function(record){
assert(record.weight === 51);
done();
});
});
});
it('Creates an author with sub-documents', function(done){
var pat = new Author({
name: 'Patrick Rothfuss',
books: [{title: 'Name of the Wind', pages: 400}]
});
pat.save().then(function(){
Author.findOne({name: 'Patrick Rothfuss'}).then(function(record){
assert(record.books.length === 1);
done();
});
});
});
const BookSchema = new Schema({
title: String,
pages: Number
});
const AuthorSchema = new Schema({
name: String,
books: [BookSchema]
});
const Author = mongoose.model('author', AuthorSchema);
it('Adds a book to an author', function(done){
var pat = new Author({
name: 'Patrick Rothfuss',
books: [{title: 'Name of the Wind', pages: 400}]
});
pat.save().then(function(){
Author.findOne({name: 'Patrick Rothfuss'}).then(function(record){
// add a book to the books collection
record.books.push({title: "Wise Man's Fear", pages: 500});
record.save().then(function(){
Author.findOne({name: 'Patrick Rothfuss'}).then(function(record){
assert(record.books.length === 2);
done();
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment