Skip to content

Commit

Permalink
Merge branch 'arnif-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbsig committed Nov 14, 2014
2 parents 36387c2 + f3c1c34 commit 2517225
Showing 1 changed file with 91 additions and 10 deletions.
101 changes: 91 additions & 10 deletions Week10/MongoDB.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ To enable a sharding service in MongoDB you need to set up a shardered cluster s
Additional reading material about [Sharding] (http://docs.mongodb.org/manual/core/sharding-introduction/)


MongoDB does not support traditional sql query language. Instead it offers its own query language and it is easy
MongoDB does not support traditional sql query language. Instead it offers its own query language and it is easy
to find good information about that on the official MongoDB website.

MongoDB is a database server that have to be installed to be used.
MongoDB is a database server that have to be installed to be used.
##Setup
Ubuntu:
Ubuntu:
> sudo apt-get install mongodb
Mac OSX using brew:
Expand All @@ -94,9 +94,9 @@ Mac OSX alternative:
> cp -R -n mongodb-osx-x86_64-2.6.4/ mongodb
The server is started from the command line with the command:
The server is started from the command line with the command:

> sudo service mongod start
> sudo service mongod start
This command starts up the mongo daemon.

MongoDB needs a data folder and it has to be created with sudo rights. By default it stores its data in /var/lib and logs in /var/log/mongdb, but you can also create your one like this:
Expand All @@ -109,19 +109,19 @@ And then behind closed doors let’s give everyone access rights to this folder:
#Mongo

Mongo is a console based client that can be used to query data in MongoDB. There are also other tools available online
Mongo is a console based client that can be used to query data in MongoDB. There are also other tools available online
that have more visual interface as [Robomongo](http://robomongo.org/)

This command lists all databases
> show database
> show database
This command switches or creates the database mydb
> use mydb
This command shows all collections in mydb.
> show collections
To create a collection, create the JSON object
To create a collection, create the JSON object
> var x = {‘user’: ‘hlysig’, ‘course’:’Forritun 1’, ‘grade’:’4’}
To insert into the grades collection give the command:
Expand All @@ -136,7 +136,7 @@ Further information can be found on [mongoDB](http://www.mongodb.org)
> npm install mongoose
##Getting started
To include mongoose in your project
To include mongoose in your project
> var mongoose = require('mongoose');
> mongoose.connect('mongodb://localhost/test');
Expand Down Expand Up @@ -173,7 +173,7 @@ person.save(function (err, person) {
if (err) return console.error(err);
person.info(); //Should say "My name is Dabs"
```
To query for all persons we can do
To query for all persons we can do
```
Person.find(function (err, persons) {
if (err) return console.error(err);
Expand All @@ -182,5 +182,86 @@ Person.find(function (err, persons) {
```
For more information about queries see [here](http://mongoosejs.com/docs/queries.html)

# Validating in mongoose

* Validation is defined in the schema
* Validation occurs when document attempts to be saved, after default values have been saved
* Validation is asynchronously recursive, when you call the save function validation is executed. If an error occurs your save function callback receives it.

## Simple validation

Simple validation is declared by passing a function to validate and error type to your SchemaType.

Example:
```
function validator (v) {
return v.length > 10;
};
new Schema({
name: { type: String, validate: [validator, 'my error type'] }
})
```

Alternatively you can do the same with this:

```
var schema = new Schema({
name: String
})
schema.path('name').validate(function (v) {
return v.length > 5;
}, 'my error type');
```

## Regular expression

You can also validate by using regular expression

Example:
```
var schema = new Schema({
name: { type: String, validate: /[a-z]/ }
});
```

## Asynchronous validation

You can define a validator function with two parameters like:
```
schema.path('name').validate(function (v, fn) {
// my logic
}, 'my error type');
```

Then the function fn will be called with true or false depending on whether the validator passed
This allows for calling other models and querying data asynchronously from your validator.


## Built in validators.

* Strings:

*enum: takes a list of allowed strings.*
```
var Post = new Schema({
type: { type: String, enum: ['smuu', 'foo', 'bar'] }
})
```

* Numbers:

*min: minimum value*
```
var Person = new Schema({
age: { type: Number, min: 10 }
})
```

*max: maxmimum value*
```
var Person = new Schema({
age: { type: Number, max: 42 }
})
```

0 comments on commit 2517225

Please sign in to comment.