Giter Club home page Giter Club logo

mongodb-crud's Introduction

General Assembly Logo

An Introduction to MongoDB

Instructions

Fork and clone this repository.

Objectives

By the end of the lesson, students should be able to:

  • Use the MongoDB shell to interact with MongoDB databases and collections
  • Create, Read, Update, and Delete documents in MongoDB collections using the MongoDB shell.

Prerequisites

Basic JavaScript.

Required readings:

Installation

We'll run brew install mongodb then execute the first two commands suggested.

On Ubuntu:

Run sudo apt-get install mongodb

Introduction

Relational databases are good at modeling data that fits into tables. What do you use if your data isn't that structured?

Perhaps a noSQL data-store.

MongoDB is a schema-less document-store that organizes documents in collections. What does this mean?

Terminology

Relational Database Term MongoDB Term
database database
table collection
row document
column field

Create a database

Code along

We'll use mongo-crud as the database to hold our tables and mongo to interact with it. mongo is MongoDB's command line client which lets us execute commands interactively and from scripts.

First let's fire up our server:

mongod

In a new tab start the mongo shell:

$ mongo mongo-crud
MongoDB shell version: 3.0.7
connecting to: mongo-crud
>

The command to list databases is show databases (or show dbs):

> show databases
local  0.078GB
>

Unlike PostgreSQL, MongoDB lets us select a database that hasn't been created. When we add a collection, the database will be created.

If we didn't specify the database on the command line we can connect to a database with use <database name>:

MongoDB shell version: 3.0.7
connecting to: test
> use mongo-crud
switched to db mongo-crud
> show databases
local  0.078GB
>

Create a collection

Code along

Our first collection will be called people. It has no entries.

> show collections
> db.people.count();
0

This is a common pattern in MongoDB: you can refer to things that don't yet exist, and it will cooperate. MongoDB won't create them until you give it something to remember.

Adding a document to a collection.

  • Inserting data - Overview of adding documents to a collection.
  • db..insert() - detailed documentation of MongoDB's insert collection method.
  • Importing data - overview of MongoDB's mongoimport command line utility.
  • mongoimport - detailed documentation of MongoDB's mongoimport command line utility.

MongoDB's mongoimport command will let us load bulk data from a JSON or CSV file.

Demonstration

First we'll load data in bulk from sample-data/csv/people.csv. We'll save the command in scripts/import/people.sh.

mongoimport --db=mongo-crud --collection=people --type=csv --headerline --file=sample/csv/people.csv

If we want to clear the collection before the import we pass the --drop flag.

Run this script by typing:

sh path_to_file.sh

Now that we've inserted data into it, the mongo-crud database and the people collection both exist.

$ mongo mongo-crud
MongoDB shell version: 3.0.7
connecting to: mongo-crud
> show dbs
local       0.078GB
mongo-crud  0.078GB
> show collections
people
system.indexes
> db.people.count();
2438

Next we'll use the insert collection method to add a few more people. We'll save our invocations in insert/people.js. We'll execute that script using the mongo load method. Let's give these people a middle_initial or a nick_name. Note that the attributes we choose for these people need not match those from the data we loaded in bulk.

> load('scripts/insert/people.js');

MongoDB uses JSON natively (technically BSON), which makes it well suited for JavaScript applications. Conveniently, MongoDB lets us specify the JSON as a JavaScript object.

Code along

Together we'll add a few cities then we'll bulk load sample-data/csv/cities.csv.

Practice

Add a pet to the pets collection using insert then bulk load sample-data/csv/pets.csv.

Next add a person to the people collection using insert then bulk load sample-data/csv/people.csv.


Retrieving documents from a collection

  • Querying - Overview of retrieving data from MongoDB.
  • Queries - More detailed overview on retrieving data.
  • find - Detailed documentation on the find collection method.
  • findOne - Detailed documentation on the findOne collection method.
  • Data aggregation - Overview of summarizing documents.
  • aggregate - Detailed documentation on the aggregate collection method.

Demonstration

Let's see some what we can learn about the people in the database.

Note: When using the REPL the .pretty() method can be quite helpful.

What do we see?

  • MongoDB gave each of our documents a unique ID field, called _id.

  • MongoDB doesn't care that some documents have fewer or more attributes.

Code along

Together we'll build a query to get all the cities. How about with some over a certain population size.

Woah, this looks like javascript. I wonder if we could use javascript?...

Practice

Write a query to get all the animals of a kind born before 2010.

Challenge: Using the docs select all animals that are fish, sort them by date.


Changing the data in documents in a collection

  • Updating Data - overview of changing documents
  • update - detailed documentation of MongoDB's update collection method.
  • Update Operators - The different modifications we can make during an update.

Demonstration

MongoDB makes it easy to add an array of items to a document. We'll update some people and give them some pets.

Code along

Let's update some cities population.

Practice

Update weight for pets then people.


Deleting documents

  • Removing Data - Overview of removing documents from a collection.
  • remove - detailed documentation of MongoDB's remove collection method.

If we want to clean up, db.<collection>.drop(); drops the specified collection and db.dropDatabase(); drops the current database.

Demonstration

We'll remove a few people from the data-store.

Code along

Let's remove all the cities with CA as a region

Practice

Remove pets born before 1996 then people taller than 5'11".

Additional resources

mongodb-crud's People

Contributors

cwilbur avatar realweeks avatar

Watchers

Jeremiah Damiron avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.