Bradley Braithwaite
1 import {
2 Learning,
3 JavaScript,
4 AngularJS
5 } from 'Brad on Code.com'
6 |
Check out my online course: AngularJS Unit Testing in-depth with ngMock.

MEAN Stack Tutorial Part 3 - Database

Part 3 of the MEAN Stack Tutorial.

12 Feb 2015 Free Software Tutorials by Bradley Braithwaite
mean stack tutorial

This is Part 3 of a 5 part Mean Stack Tutorial.

The database has been used to build out the app but until now it’s been a little like a magic box. Let’s look at a few features of MongoDB.

MongoDB has document oriented storage, dynamic schemas and data is stored as JSON-style documents. We are using schemas in this tutorial as that’s how mongoose works, but you do not have to stick to a schema as with relational databases. Two documents (rows) in the same collection (table) can have different schemas.

See this document comparing sql terminology with MongoDB if concepts like collections and documents are new to you.

The key thing to remember is that we are using the mongoose driver to interact with MongoDB from the Node.js server side code. This layer does a lot of the work for us, so we will only take quick look at Mongo.

Something that may seem a little odd at first to some is how the databases are created automatically from the connection strings once the app is fired up. This is configurable but is the easiest way to work in a dev environment.

Windows Users: You first need to navigate to where the installation of MongoDB is i.e. cd C:\mongodb\bin_. Then you can run the _mongod and mongo commands.

If the database is not running, run the command:

mongod

Once the database processs is running you can view some stats at http://localhost:28017.

I use http://robomongo.org which is a very useful cross-platform tool for databse management but you can also interact with mongo using the interative javascript shell, which we will look at next.

Basic Shell Commands

You can access via the terminal using:

mongo

To list all databases:

show dbs

To switch to a database:

use northwindnode-dev

List the collections (tables) in the active database:

show collections

Show all the documents from the categories collection:

db.categories.find();

Show a count of how many documents are in the categories collection:

db.categories.count();

Create a category:

db.categories.insert({ name: 'Electronics' });

Search for this new document:

db.categories.find({ name: "Electronics" }).pretty();

The result is:

{ 
  "_id" : ObjectId("54d7edbc84e3603a777589b6"), 
  "name" : "Electronics" 
}

Create a category with a different schema in the same collection:

db.categories.insert({ title: 'Electronics' });

Let’s query this new document with a different schema but within the same categories collection:

db.categories.find({ title: 'Electronics' }).pretty();

The result is:

{ 
  "_id" : ObjectId("54d7ef3c84e3603a777589b8"), 
  "title" : "Electronics"
}

Update a category:

db.categories.update({ name: 'Electronics'  }, { $set: { name: 'Gadgets' } });

To check it was updated:

 db.categories.find({ name: 'Gadgets' }).pretty();

Finally, to remove the category:

db.categories.remove({ name: 'Gadgets' });

There are many more commands which are located at: http://docs.mongodb.org/v2.2/reference/mongo-shell/

The API of the mongoose abstraction used in this project will differ slightly from the commands in the mongo-shell but it’s useful to have an idea of what is underneath mongoose.

Next Tutorial

The next tutorial works through the front-end code and AngularJS.

View the Full Source on Github

The full source for this project is hosted on Github. Note that this version is evolving and may differ slightly from this tutorial as new features are added for the subsequent tutorials.

https://github.com/bbraithwaite/NorthwindNode

SHARE
Don't miss out on the free technical content:

Subscribe to Updates

CONNECT WITH BRADLEY

Bradley Braithwaite Software Blog Bradley Braithwaite is a software engineer who works for search engine start-ups. He is a published author at pluralsight.com. He writes about software development practices, JavaScript, AngularJS and Node.js via his website . Find out more about Brad. Find him via: