Added files via upload

master
Matt Huntington 10 years ago
parent 660484748c
commit 129d2d1b43

@ -0,0 +1,136 @@
# Databases - Mongo
## Lesson Objectives
1. show
1. use
1. create collection
1. insert
1. dropping
1. find
1. remove
1. count
## Show
1. `show dbs`
## Use
1. `use learn`
1. `db`
- show current database being used
## create collection
1. `db.createCollection('testCollection')`
## Insert
1. `db.testCollection.insert({foo:'true'})`
1. multi-lines
1. `show collections`
1. can use regular javascript with variables and functions
- watch out for cursors from find()
1. can insert array of objects
## Dropping
1. `db.dropDatabase();`
1. `db.collection.drop();`
## Find
```
db.employees.insert([{
name: 'Daisy',
dob: new Date(1992,2,13,7,47),
loves: ['carrot','papaya'],
weight: 600,
gender: 'm',
salary: 63
},{
name: 'Aurora',
dob: new Date(1991, 0, 24, 13, 0),
loves: ['carrot', 'grape'],
weight: 450,
gender: 'f',
salary: 43
},{
name: 'Unicrom',
dob: new Date(1973, 1, 9, 22, 10),
loves: ['energon', 'redbull'],
weight: 984,
gender: 'm',
salary: 182
},{
name: 'Roooooodles',
dob: new Date(1979, 7, 18, 18, 44),
loves: ['apple'],
weight: 575,
gender: 'm',
salary: 99
},{
name: 'Solnara',
dob: new Date(1985, 6, 4, 2, 1),
loves:['apple', 'carrot','chocolate'],
weight:550,
gender:'f',
salary:80
},{
name:'Ayna',
dob: new Date(1998, 2, 7, 8, 30),
loves: ['strawberry', 'lemon'],
weight: 733,
gender: 'f',
salary: 40
},{
name:'Kenny',
dob: new Date(1997, 6, 1, 10, 42),
loves: ['grape', 'lemon'],
weight: 690,
gender: 'm',
salary: 39
},{
name: 'Raleigh',
dob: new Date(2005, 4, 3, 0, 57),
loves: ['apple', 'sugar'],
weight: 421,
gender: 'm',
salary: 2
},{
name: 'Leia',
dob: new Date(2001, 9, 8, 14, 53),
loves: ['apple', 'watermelon'],
weight: 601,
gender: 'f',
salary: 33
},{
name: 'Pilot',
dob: new Date(1997, 2, 1, 5, 3),
loves: ['apple', 'watermelon'],
weight: 650,
gender: 'm',
salary: 54
},{
name: 'Nimue',
dob: new Date(1999, 11, 20, 16, 15),
loves: ['grape', 'carrot'],
weight: 540,
gender: 'f'
},{
name: 'Dunx',
dob: new Date(1976, 6, 18, 18, 18),
loves: ['grape', 'watermelon'],
weight: 704,
gender: 'm',
salary: 165}]);
```
1. `db.employees.find({gender:'m'})`
1. `db.employees.find({weight : { $gt : 700 }})`
- $lt, $lte, $gt, $gte, $ne, $exists: false, $in: ['orange', 'apple']
1. `db.employees.find({loves:'energon'})`
- if field is array, will return search within that array
1. AND
- `db.employees.find({gender: 'm', weight: {$gt: 700}})`
1. OR
- `db.employees.find({gender: 'f', $or: [{loves: 'apple'},{weight: {$lt: 500}}]})`
1. value between
- `db.employees.find( { salary: { $gte : 80, $lte : 165} } );`
## Remove
## Count

@ -0,0 +1,18 @@
# Mongo - Monitoring
## Lesson Objectives
1. Explain explain
1. Explain stats
1. Explain profile
## Explain Explain
1. `db.employees.find().explain()`
## Explain stats
1. `db.stats()`
1. `db.collectionName.stats()`
## Explain profile
1. `db.setProfilingLevel(2);`
1. `db.employees.find();`
1. `db.system.profile.find()`

@ -0,0 +1,171 @@
# Mongo - GeoSpacial
## Lesson Objectives
1. Explain GeoSpacial
1. Explain GeoJSON
1. Explain containers
1. Explain How To Create an Index
1. Explain $near
1. Explain $geoWithin
1. Explain $geoIntersects
## Explain GeoSpacial
## Explain GeoJSON
1. `{ type: "<GeoJSON type>" , coordinates: <coordinates> }`
1. Always list coordinates in `longitude`, `latitude` order.
1. point
- `{ type: "Point", coordinates: [ 40, 5 ] }`
1. line
- `{ type: "LineString", coordinates: [ [ 40, 5 ], [ 41, 6 ] ] }`
1. polygons
- array of coordinate arrays (rings)
- at least four points and have same start/end position
- one outer ring and 0+ inner rings
- cannot self intersect
- single ring
- `{ type: "Polygon", coordinates: [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ] }`
- multiple rings
- 1st ring is outer ring
- Any interior ring must be entirely contained by the outer ring
- Interior rings cannot intersect or overlap each other
- Interior rings cannot share an edge.
- http://docs.mongodb.org/manual/_images/index-2dsphere-polygon-with-ring.png
```
{
type : "Polygon",
coordinates : [
[ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ],
[ [ 2 , 2 ] , [ 3 , 3 ] , [ 4 , 2 ] , [ 2 , 2 ] ]
]
}
```
## Explain containers
1. MultiPoint
```
{
type: "MultiPoint",
coordinates: [
[ -73.9580, 40.8003 ],
[ -73.9498, 40.7968 ],
[ -73.9737, 40.7648 ],
[ -73.9814, 40.7681 ]
]
}
```
1. MultiLineString
```
{
type: "MultiLineString",
coordinates: [
[ [ -73.96943, 40.78519 ], [ -73.96082, 40.78095 ] ],
[ [ -73.96415, 40.79229 ], [ -73.95544, 40.78854 ] ],
[ [ -73.97162, 40.78205 ], [ -73.96374, 40.77715 ] ],
[ [ -73.97880, 40.77247 ], [ -73.97036, 40.76811 ] ]
]
}
```
1. MultiPolygon
```
{
type: "MultiPolygon",
coordinates: [
[ [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.9814, 40.7681 ], [ -73.958, 40.8003 ] ] ],
[ [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.958, 40.8003 ] ] ]
]
}
```
1. GeometryCollection
```
{
type: "GeometryCollection",
geometries: [
{
type: "MultiPoint",
coordinates: [
[ -73.9580, 40.8003 ],
[ -73.9498, 40.7968 ],
[ -73.9737, 40.7648 ],
[ -73.9814, 40.7681 ]
]
},
{
type: "MultiLineString",
coordinates: [
[ [ -73.96943, 40.78519 ], [ -73.96082, 40.78095 ] ],
[ [ -73.96415, 40.79229 ], [ -73.95544, 40.78854 ] ],
[ [ -73.97162, 40.78205 ], [ -73.96374, 40.77715 ] ],
[ [ -73.97880, 40.77247 ], [ -73.97036, 40.76811 ] ]
]
}
]
}
```
## Explain How To Create an Index
1. `db.collection.createIndex( { <location field> : "2dsphere" } )`
## Explain $near
```
db.test_data.insert({name: 'north', position : { type: "Point", coordinates: [0,1] } });
db.test_data.insert({name: 'east', position : { type: "Point", coordinates: [1,0] } });
db.test_data.insert({name: 'south', position : { type: "Point", coordinates: [0,-1] } });
db.test_data.insert({name: 'west', position : { type: "Point", coordinates: [-1,0] } });
db.test_data.createIndex({position: "2dsphere"});
db.test_data.find({
position: {
$near : {
$geometry: {
type: "Point",
coordinates: [ 2, 0 ]
},
$minDistance: 0,
$maxDistance: 200000
}
}
});
```
## Explain $geoWithin
```
db.test_data.find({
position: {
$geoWithin: {
$geometry: {
type : "Polygon" ,
coordinates: [[
[ 0.5, 0.5 ],
[ 1.5, 0.5 ],
[ 1.5, -0.5 ],
[ 0.5, -0.5 ],
[ 0.5, 0.5 ], // VERY IMPORTANT
]]
}
}
}
});
```
## Explain $geoIntersects
```
db.test_data.insert({ name : 'line' , "position" : { "type" : "LineString", "coordinates" : [ [1,0], [2,0] ] } } );
WriteResult({ "nInserted" : 1 });
db.test_data.find({
position: {
$geoIntersects: {
$geometry: {
type : "Polygon" ,
coordinates: [[
[ 0.5, 0.5 ],
[ 1.5, 0.5 ],
[ 1.5, -0.5 ],
[ 0.5, -0.5 ],
[ 0.5, 0.5 ], // VERY IMPORTANT
]]
}
}
}
});
```

@ -0,0 +1,46 @@
# Mongo - Update
## Lesson Objectives
1. Explain how to replace a record
1. Explain how to update certain values for a record
1. Explain update operators
1. Explain upserts
1. Explain multiple updates
## Explain how to replace a record
1.`db.employees.update({name:'Roooooodles'}, {weight: 590})`
## Explain how to update certain values for a record
```
db.employees.update(
{weight: 590},
{$set: {
name: 'Roooooodles',
dob: new Date(1979, 7, 18, 18, 44),
loves: ['apple'],
gender: 'm',
salary: 99}})
```
## Explain update operators
1. `db.employees.update({name: 'Pilot'}, {$inc: {salary: -2}})`
1. `db.employees.update({name: 'Pilot'}, {$mul: {salary: (1/2)}})`
1. `db.employees.update({name: 'Aurora'}, {$push: {loves: 'sugar'}})`
1. `db.employees.update({name: 'Aurora'}, {$pop: 1})`
1. `db.employees.update({name: 'Aurora'}, {$unset: {loves: ''}})`
1. `db.employees.update({name: 'Aurora'}, {$rename: {wrong_field_name : 'correct_field_name'}})`
1. http://docs.mongodb.org/manual/reference/operator/update/#update-operators
## Explain multiple updates
1. update() updates first entry
```
db.employees.update(
{},
{$set: {vaccinated: true}},
{multi:true});
db.employees.find({vaccinated: true});
```
## Explain upserts
1. `db.hits.update({page: 'employees'}, {$inc: {hits: 1}}); db.hits.find();`
1. `db.hits.update({page: 'employees'}, {$inc: {hits: 1}}, {upsert:true}); db.hits.find();`

@ -0,0 +1,114 @@
# Mongo - Mongoose
## Lesson Objectives
1. Explain ODM
1. Explain Schema
1. Explain Create/Save
1. Explain Find
1. Explain Update
1. Explain Remove
1. Explain Helper Functions
## Explain ODM
## Explain Schema
article.js
```javascript
var mongoose = require('mongoose');
mongoose.set('debug', true);
var Schema = mongoose.Schema;
var articleSchema = new Schema({
title: { type: String, required: true, unique: true },
author: { type: String, required: true },
body: String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
});
//Creating an Article class -- will be stored in 'articles' collection
var Article = mongoose.model('Article', articleSchema);
module.exports = Article;
```
1. String
1. Number
1. Date
1. Boolean
1. Mixed
1. ObjectId
1. Array
## Explain Create/Save
```javascript
var mongoose = require('mongoose');
var db = mongoose.connection;
var Article = require('./article.js');
//Setting up a new article
var newArticle = new Article({
title: 'Awesome Title',
author: 'Matt'
});
newArticle.hidden = false;
//connect to mongo
mongoose.connect('mongodb://localhost:27017/example');
//if the connection fails
db.on('error', function(){
console.log('error');
});
db.once('open', function() {
//we're connected!
//save article to the database
newArticle.save(function(err, article) {
if(err) {
console.log(err);
} else {
console.log(article);
}
mongoose.connection.close();
});
});
```
## Explain Find
```javascript
Article.find({author:'Matt'}, 'name -_id',function(err, articles){
console.log(articles);
mongoose.connection.close();
})
```
## Explain Update
```javascript
Article.update({ author: 'Matt' }, { $set : { author: 'Matthew' } }, { multi: true }, function (err, response) {...});
```
## Explain Remove
```javascript
Article.remove({author:'Matt'}, function(err){...});
```
```javascript
Article.findOne({title:'Harry Potter'}, function(err, article){
article.remove();
});
```
## Explain Helper Functions
1. findById
1. findOne
1. findByIdAndUpdate
1. findByIdAndRemove
1. findOneAndUpdate
1. findOneAndRemove

@ -0,0 +1,71 @@
# Mongo - MapReduce
## Lesson Objectives
1. Explain what MapReduce is and why we have it
1. Explain structure of Map Reduce
1. Explain map function
1. Explain reduce function
1. Explain aggregating multiple values
1. Explain Multiple group by
## Explain what MapReduce is and why we have it
## Explain structure of Map Reduce
1. db.collectionName.mapReduce(mapFunction, reduceFunction, { query: {}, out:{} })
1. out
- collection name
- { [ replace | inline | merge | reduce ]: 1 }
## Explain map function
1. return key=>value pair
```
var emitter = function (){
if(this.gender === 'm'){
emit(this.name, { yum : this.loves[0], weight:this.weight });
}
}
db.employees.mapReduce(emitter, function(){}, {out:'mapTest'});
```
## Explain reduce function
1. if multiple values for a key, how to reduce
```
var emitter = function (){
emit(this.gender, this.weight);
}
var reducer = function (key, values){
return Array.sum(values);
}
db.employees.mapReduce(emitter, reducer, {out:'mapTest'});
```
## Multiple values
```javascript
var emitter = function (){
emit(this.gender, { weights: this.weight, money: this.salary });
}
var reducer = function(key, values){
var total_weight = 0;
var total_salary = 0;
for(var i=0; i<values.length; i++){
total_weight += values[i].weights;
total_salary += values[i].money;
}
return {total_weight: total_weight, total_salary: total_salary}
}
db.employees.mapReduce(emitter, reducer, { out: 'mapTest' });
db.mapTest.find();
```
## Multiple group by
```javascript
var emitter = function (){
emit(
{
gender: this.gender,
weight: this.weight
},
this.weight
);
}
```

@ -0,0 +1,27 @@
# Mongo - Advanced Find
## Lesson Objectives
1. Explain Field Selection
1. Explain Ordering
1. Explain Paging
1. Explain Count
## Explain Field Selection
1. `db.employees.find({}, {name: 1});`
- 1 for include
- 0 for exclude
1. `{name:1, _id: 0}` excludes _id
## Explain Ordering
1. `db.employees.find().sort({name: 1, salary: -1})`
- 1 for ascending
- -1 for descending
- won't sort on large set without index
## Explain Paging
1. `db.employees.find().sort({weight: -1}).limit(2).skip(1)`
- can help avoid issues when sorting on large un-indexed fields
## Explain Count
1. `db.employees.find({salary: {$gt: 50}}).count()`
- usual count is just a shortcut to this

@ -0,0 +1,13 @@
# Mongo - Backing Up
## Lesson Objectives
1. Explain mongodump
1. Explain mongorestore
## Explain mongodump
1. `mongodump -o ~/Desktop/mongo_db/export`
1. `mongodump --db learn -o ~/Desktop/mongo_db/export`
1. `mongodump --db learn --collection employees -o ~/Desktop/mongo_db/export`
## Explain mongorestore
1. `mongorestore --db learn --collection employees backup/learn/employees.bson`

@ -0,0 +1,170 @@
# Advanced Mongoose
## Lesson Objectives
1. Explain Methods
1. Explain Statics
1. Explain Indexes
1. Explain Populate
1. Explain SubDocuments
1. Explain Middleware
## Explain Methods
```javascript
articleSchema.methods.longTitle = function(){
return this.author + ": " + this.title;
}
console.log(article.longTitle());
```
## Explain Statics
```javascript
articleSchema.statics.search = function (name, cb) {
return this.find({
$or : [
{ title: new RegExp(name, 'i') },
{ author: new RegExp(name, 'i') }
]
}, cb);
}
Article.search('Some', function(err, data){
console.log(data);
});
```
## Explain SubDocuments
```javascript
var authorSchema = new Schema({
name: { type: String },
articles: [articleSchema]
});
var Article = mongoose.model('Article', articleSchema);
var Author = mongoose.model('Author', authorSchema);
var matt = new Author({name: 'Matt'});
var article_id;
matt.save(function(){
var article1 = new Article({title:'Awesome Title', author: matt._id});
article1.save(function(){
article_id = article1._id;
matt.articles.push(article1);
matt.save(showAll);
});
});
var showAll = function(err, author){
Author.findOne({}, function(err, result){
console.log(result.articles.id(article_id));
mongoose.connection.close();
});
};
```
1. `parent.children.create({ name: 'Aaron' });`
1. `parent.children.id(id).remove();`
1. `var parentSchema = new Schema({ children: [{ name: 'string' }] })`
## Explain Populate
```javascript
var mongoose = require('mongoose');
mongoose.set('debug', false);
mongoose.connect('mongodb://localhost:27017/test');
var Schema = mongoose.Schema;
var articleSchema = new Schema({
title: { type: String },
author: { type: Schema.Types.ObjectId, ref: 'Author' }
});
var authorSchema = new Schema({
name: { type: String }
});
var Article = mongoose.model('Article', articleSchema);
var Author = mongoose.model('Author', authorSchema);
var matt = new Author({name: 'Matt'});
matt.save(function(){
var article1 = new Article({title:'Awesome Title', author: matt._id});
article1.save(function(){
showAll();
});
});
var showAll = function(){
Article.find().populate('author').exec(function(error, article){
console.log(article);
mongoose.connection.close();
})
};
```
1. arrays of refs and updating refs with obj
```javascript
var authorSchema = new Schema({
name: { type: String },
articles: [{type: Schema.Types.ObjectId, ref: 'Articles'}]
});
var Article = mongoose.model('Article', articleSchema);
var Author = mongoose.model('Author', authorSchema);
var matt = new Author({name: 'Matt'});
var article_id;
matt.save(function(){
var article1 = new Article({title:'Awesome Title', author: matt._id});
article1.save(function(){
article_id = article1._id;
matt.articles.push(article1);
matt.save(showAll);
});
});
var showAll = function(err, author){
Author.find().populate('articles').exec(function(err, authors){
console.log(authors);
mongoose.connection.close();
});
};
```
## Explain Middleware
```javascript
articleSchema.pre('save', function(next){
console.log(this);
console.log('saving to backup database');
next();
});
```
async
```javascript
articleSchema.pre('save', true, function(next){
console.log(this);
console.log('saving to backup database');
next();
doAsync(done);
});
```
post
```javascript
articleSchema.post('save', function(next){
console.log('saving complete');
});
```
##Explain Indexes
```javascript
author: { type: String, required: true, index: true }, //in schema
{ autoIndex : false } // at end of schema
articleSchema.index({title:1, author:-1});
articleSchema.index({title:'text', author:'text'});
Article.ensureIndexes(function(){...});
Article.find({ $text : { $search: 'Awesome'} }, function(err, results){
console.log(results);
mongoose.connection.close();
});
```

@ -0,0 +1,37 @@
# Mongo - Aggregation
## Lesson Objectives
1. Explain what aggregation is
1. Explain the Mongo Aggregation Pipeline
1. Explain some basic aggregation functions
1. Explain $out
## Explain what aggregation is
## Explain the Mongo Aggregation Pipeline
1. `db.collectionName.aggregate([<stage>, <stage>]);`
1. http://docs.mongodb.org/manual/_images/aggregation-pipeline.png
## Explain some basic aggregation functions
1. `{ $match : { weight : { $lt : 600 } } }`
1. `{ $sort : { salary : -1 } }`
1. `{ $limit : 1 }`
1. `{ $skip : 1 }`
1. `{ $out : 'collectionName'}`
1. $group
- `{ $group : { _id : '$gender' } }`
- `{ $group : { _id : { 'weight' : '$weight', 'gender' : '$gender' } } } ] );`
- `{ $group : { _id : '$gender', numOfEachGender : { $sum : 1 } } }`
- `{ $group : { _id : '$gender', avgSalary : { $avg : '$salary' } } }`
- `{ $group : { _id : '$weight', namesArray : { $addToSet : '$name' } } }`
1. `{ $unwind : '$loves' }`
- `db.employees.aggregate([{$unwind : '$loves'}, {$group : { _id : '$loves', num : {$sum : 1}, names : {$addToSet : '$name'}}}])`
1. $project
- `{ $project : { name : 1, _id : 0 } }`
- `{ $project : { namezz :'$name' } }`
- expressions
- `{ $project : { awesomeName : { $concat : ['$name', ' is awesome', '!']}} }`
- http://docs.mongodb.org/manual/meta/aggregation-quick-reference/#aggregation-expressions
- `{ $project : { embedded_atrribute : '$embedded_object.foo' } }`
- `{ $project : { embedded_object.attribute : 1 } }`
- arrays?

@ -0,0 +1,43 @@
# Mongo - Data Modeling
## Lesson Objectives
1. Explain how to relate documents using foreign keys
1. Explain how to use Arrays
1. Explain how to use Embedded Documents
1. Explain what denormalization is and when to use it
## Explain how to relate documents using foreign keys
```
db.employees.insert({_id: ObjectId( "4d85c7039ab0fd70a117d730"), name: 'Leto'});
db.employees.insert({_id: ObjectId( "4d85c7039ab0fd70a117d731"), name: 'Duncan', manager: ObjectId( "4d85c7039ab0fd70a117d730")});
db.employees.insert({_id: ObjectId( "4d85c7039ab0fd70a117d732"), name: 'Moneo', manager: ObjectId( "4d85c7039ab0fd70a117d730")});
```
1. to find: `db.employees.find({manager: ObjectId( "4d85c7039ab0fd70a117d730")})`
## Explain how to use Arrays
```
db.employees.insert({
_id: ObjectId( "4d85c7039ab0fd70a117d733"),
name: 'Siona',
manager: [
ObjectId( "4d85c7039ab0fd70a117d730"),
ObjectId( "4d85c7039ab0fd70a117d732")] })
```
1. to find: `db.employees.find({manager: ObjectId( "4d85c7039ab0fd70a117d730")})`
1. index uses dot notation
- `db.employees.find({ 'manager.0': ObjectId("4d85c7039ab0fd70a117d730")})`
## Explain how to use Embedded Documents
```
db.employees.insert({
_id: ObjectId( "4d85c7039ab0fd70a117d734"),
name: 'Ghanima',
family: {
mother: 'Chani',
father: 'Paul',
brother: ObjectId( "4d85c7039ab0fd70a117d730")}})
```
1. can be queried using dot notation
- `db.employees.find({ 'family.mother': 'Chani'})`
## Explain what denormalization is and when to use it

@ -0,0 +1,43 @@
# Mongo - Performance
## Lesson Objectives
1. Explain indexes
1. Explain text indexes
1. Explain replication
1. Explain sharding
## Explain indexes
1. `db.employees.ensureIndex({name: 1});`
1. `db.employees.dropIndex({name: 1});`
1. `db.employees.ensureIndex({name: 1}, {unique: true});`
1. indexes on embedded fields
1. indexes on arrays
1. `db.employees.ensureIndex({name: 1, salary: -1});`
## Explain text indexes
1. `db.articles.createIndex({ subject : "text", content : "text" })`
1. drop text index
- `db.articles.getIndexes()`
- `db.articles.dropIndex('index_name')`
1. `db.articles.find( { $text: { $search: "coffee" } } )`
1. `db.articles.find( { $text: { $search: "leche", $language: "es" } } )`
- stop words (a, the, and, etc)
1. `db.articles.find( { $text: { $search: "bake coffee cake" } } )`
1. `db.articles.find( { $text: { $search: "\"coffee cake\"" } } )`
1. `db.articles.find( { $text: { $search: "bake coffee -cake" } } )`
1. `db.articles.find( { $text: { $search: "cake" } }, { score: { $meta: "textScore" } } )`
```
db.articles.find(
{ $text: { $search: "cake" } },
{ score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } )
```
## Explain replication
1. Writes sent to secondary DBs asynchonously
1. Reads can happen on secondaries
- although data may be stale
1. If primary goes down, a secondary is chosen to be new primary
## Explain sharding
1. split data across multiple servers
Loading…
Cancel
Save