MongoDB is designed to provide high-availability access to your data. It does this by enabling you to maintain redundant copies of your data in a cluster called a replica set. For example, if we configure our Atlas cluster to be a 3-server replica set then in the events of the software or hardware failure of the primary server, on of the other server will step in to continue serving data to clients.

Getting the version of the installed MongoDB shell mongo --nodb

Compass - MongoDB GUI client

Hostname: cluster0-shard-00-00-jxeqq.mongodb.net
Port: 27017
Authentication: Username/Password
Username: your-username
Password: your-password
Replica Set Name: Cluster0-shard-0
Read Preference: Primary Preferred

SSL: System CA/ Atlas Deployment

Click "Add to Favorites" and enter FAV-NAME as the Favorite Name. Adding this connection as a favorite will enable you to easily connect to our class MongoDB deployment after closing and restarting Compass at some point in the future.

Now, click "Connect" and load the databases in the M001 class MongoDB deployment.

Notation Terminology

document <-> row

collection <-> table

field <-> columns

<database.collection>

<database.collection.document>

Each document (row) is a JSON object.

Mongo shell

Mongo Shell is a text based client. You need to install MongoDB Enterprise server on your system.

  1. After installing copy it in your home directory

  2. Open bash profile and type export PATH="~/mongodb-osx-x86_64-enterprise-3.4.13/bin:$PATH"

Atlas - MongoDB hosted MongoDB as a service platform

Connecting to our class ATLAS cluster from the Mongo shell

mongo "mongodb://cluster0-shard-00-00-jxeqq.mongodb.net:27017,cluster0-shard-00-01-jxeqq.mongodb.net:27017,cluster0-shard-00-02-jxeqq.mongodb.net:27017/test?replicaSet=Cluster0-shard-0" --authenticationDatabase admin --ssl --username your-username --password your-password

where test can be replaced by your database 100YWeatherSmall. For any cluster there is one and only one primary.

Commands

show dbs -> show databases residing in a cluster.

use video -> switching to another database (with name video).

show collections -> command for displaying collections in a database.

db -> refers to the current referred database.

db.movies.find().pretty() -> seeing documents that are there in the movies collection of video database.

db.movies.find().count() -> count the number of documents in the movies collection of video database.

The Atlas clusters we’ve looked at are replica sets. Replica sets are designed so that if the primary node goes down, one of the other nodes will step up to take its place so that clients can continue reading and writing data as if nothing had happened. The mongo shell is one such client.

To begin creating your Atlas Sandbox cluster, visit register for atlas and complete the account creation form you see on that page.

Connecting to our Atlas sandbox cluster from the Mongo shell

mongo "mongodb://sandbox-shard-00-00-2xiao.mongodb.net:27017,sandbox-shard-00-01-2xiao.mongodb.net:27017,sandbox-shard-00-02-2xiao.mongodb.net:27017/test?replicaSet=Sandbox-shard-0" --ssl --authenticationDatabase admin --username your-username --password your-password

Loading data (.js file) into your sandbox cluster

  1. cd M001 (in your home directory which contains the .js file)
  2. Run the mongodb shell:

    mongo "mongodb://sandbox-shard-00-00-2xiao.mongodb.net:27017,sandbox-shard-00-01-2xiao.mongodb.net:27017,sandbox-shard-00-02-2xiao.mongodb.net:27017/video?replicaSet=Sandbox-shard-0" --ssl --authenticationDatabase admin --username your-username --password your-password

    video is the db that you are loading your collections into. We can change to some other db as well.

  3. Once connected to sandbox cluster run load("loadMovieDataSet.js")

Connecting to our Atlas sandbox cluster from Compass

  1. Go to Compass -> Connect to host

  2. In the cloud.mongodb.com

    a.) Select the sandbox cluster Sandbox -> Select Primary node and copy the Url sandbox-shard-00-00-2xiao.mongodb.net and paste in the host name field of compass.

    b.) In the authentication phase -> select username/ password system and provide the username -> your-username and password -> your-password

    c.) Add Favorite name M001 Sandbox. Click Connect

Inserting Documents from MongoDB Shell

use video

show collections

One document at a time

Without _id (default id is supplied)

db.moviesScratch.insertOne({title:"Star Trek II: The Wrath of Khan", year:1982, imdb:"tt0084726"})

With _id

db.moviesScratch.insertOne({_id:"tt0084726", title:"Star Trek II: The Wrath of Khan", year:1982, imdb:"tt0084726"})

Many documents at a time

//Ordered

db.moviesScratch.insertMany(
[
{
"_id" : "tt0084726",
"title" : "Star Trek II: The Wrath of Khan",
"year" : 1982,
"type" : "movie"
},
{
"_id" : "tt0796366",
"title" : "Star Trek",
"year" : 2009,
"type" : "movie"
},
{
"_id" : "tt0084726",
"title" : "Star Trek II: The Wrath of Khan",
"year" : 1982,
"type" : "movie"
},
{
"_id" : "tt1408101",
"title" : "Star Trek Into Darkness",
"year" : 2013,
"type" : "movie"
},
{
"_id" : "tt0117731",
"title" : "Star Trek: First Contact",
"year" : 1996,
"type" : "movie"
}
]
);
//Unordered

db.moviesScratch.insertMany(
[
{
"_id" : "tt0084726",
"title" : "Star Trek II: The Wrath of Khan",
"year" : 1982,
"type" : "movie"
},
{
"_id" : "tt0796366",
"title" : "Star Trek",
"year" : 2009,
"type" : "movie"
},
{
"_id" : "tt0084726",
"title" : "Star Trek II: The Wrath of Khan",
"year" : 1982,
"type" : "movie"
},
{
"_id" : "tt1408101",
"title" : "Star Trek Into Darkness",
"year" : 2013,
"type" : "movie"
},
{
"_id" : "tt0117731",
"title" : "Star Trek: First Contact",
"year" : 1996,
"type" : "movie"
}
],
{
"ordered": false 
}
);

Filtering with queries

In compass

{$and:[{"awards.wins":2},{"awards.nominations":2}]}

In MongoDB command shell

db.movieDetails.find({$and:[{"awards.wins":2},{"awards.nominations":2}]}).count()

db.movies.find({cast:["Jeff Bridges", "Tim Robbins"]}).pretty()

db.movieDetails.find({genres: 'Family'}).count()

db.movieDetails.find({"genres.1": 'Western'}).count()

Cursors

The find() method returns a cursor. A cursor is essentially a pointer to the current location in a result set. For queries that return more than just a few documents, MongoDB will return the results in batches to our client (Mongo Shell). We use the cursor in our client to iterate through the results.

Projections

Projections reduce the network overhead and processing requirements by limiting the fields that are returned in resulting documents. In the query projection is added as the second argument to find() method.

Example:

db.movies.find({genre:"Action, Adventure"}, {title: 1})

_id field is returned as well by default and you can exclude it by supplying _id:0 as follows db.movies.find({genre:"Action, Adventure"}, {title: 1, _id: 0})

If we want to exclude couple of fields then we need to type as: db.movies.find({genre:"Action, Adventure"}, {viewerRating: 0, viewerVotes: 0, runtime: 0, _id: 0})

Updating documents

db.movieDetails.updateOne({
title: "The Martian"
},{
$set:{
poster: "http://ia.media-imdb.com/images/M/MV5BMTc2MTQ3MDA1Nl5BMl5BanBnXkFtZTgwODA3OTI4NjE@._V1_SX300.jpg"    
}
})
db.movieDetails.updateOne({
title: "The Martian"
},{
$set:{
"awards": {
"wins": 8,
"nominations": 14,
"text": "Nominated for 3 Golden Globes. Another 8 wins and 14 nominations."
}    
}
});

$set replaces the value of field with the specified value.

Difference between updateOne and updateMany is that updateMany will make same modification to all documents that match the filter.

db.movieDetails.updateMany({
rated: null
},{
$unset:{
rated: ""    
}
})

We can insert while doing the update step as shown in the below example.

db.movieDetails.updateOne({
"imdb.id": detail.imdb.id
},{
$set: detail
},{
upsert: true
});

Upsert updates the documents matching the filter. If there are none, insert the update document as the new document in the collection.

db.movieDetails.replaceOne({
"imdb.id": detail.imdb.id
},
detailDoc
);

QUERY OPERATORS

db.movieDetails.find({runtime: {$gt: 90}})

db.movieDetails.find({runtime: {$gt: 90}}, {_id: 0, title: 1, runtime: 1})

db.movieDetails.find({runtime: {$gt: 90, $lt: 120}}, {_id: 0, title: 1, runtime: 1})

db.movieDetails.find({runtime: {$gte: 90, $lte: 120}}, {_id: 0, title: 1, runtime: 1})

db.movieDetails.find({runtime: {$gte: 180}, "tomato.meter": 100}, {_id: 0, title: 1, runtime: 1})

db.movieDetails.find({rated: {$ne: "UNRATED"}}, {_id: 0, title: 1, rated: 1})

db.movieDetails.find({rated: {$in: ["G", "PG"]}}, {_id: 0, title: 1, rated: 1})

db.movieDetails.find({rated: {$in: ["G", "PG", "PG-13"]}}, {_id: 0, title: 1, rated: 1}).pretty()

db.movieDetails.find({rated: {$in: ["R", "PG-13"]}}, {_id: 0, title: 1, rated: 1}).pretty()

COMPARISON OPERATORS

db.movies.find({cast: {$in: ["Jack Nicholson", "John Huston"]}, viewerRating: {$gt: 7}, mpaaRating: "R"}).count()

Note: The above post is in reference to material related to M001-Basics course offered by MongoDB University.