MongoDB University Documentation

Document

A way to organize and store data as a set of field-value pairs.

{
    <field> : <value>
}

Collection

An organized store of documents in MongoDB, usually with common fields between documents.

What is MongoDB Atlas?

Atlas is -

  • Database as a service
  • MongoDB is used at the core of atlas data storage and retrieval

Cluster Deployment


Replica Set - a few connected machines that store the same data to ensure that if something happens to one of the machines the data will remain intact. Comes from the word replicate - to copy something.

Instance - a single machine locally or in the cloud, running a certain software, in our case it is the MongoDB database.

Cluster - group of servers that store your data.

Chapter - 2

Importing and Exporting Data

MongoDB%20University%200d54c5c9b8fe42a79f7e2c7c23a70695/Untitled%201.png

To learn more about other mongoimport supported formats check out this documentation page.

SRV connection string - a specific format used to establish a connection between your application and a MongoDB instance. Click here to learn more.

Code used in this lecture:

mongodump --uri "mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies"

mongoexport --uri="mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies" --collection=sales --out=sales.json

mongorestore --uri "mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies"  --drop dump

mongoimport --uri="mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies" --drop sales.json

MongoDB%20University%200d54c5c9b8fe42a79f7e2c7c23a70695/Untitled%202.png

MongoDB%20University%200d54c5c9b8fe42a79f7e2c7c23a70695/Untitled%203.png

MongoDB%20University%200d54c5c9b8fe42a79f7e2c7c23a70695/Untitled%204.png

Data Explorer


Namespace - The concatenation of the database name and collection name is called a namespace.

We looked at the sample_training.zips collection and issued the following queries:

  • {"state": "NY"}
  • {"state": "NY", "city": "ALBANY"}

Find Command


Connect to the Atlas cluster:

mongo "mongodb+srv://<username>:<password>@<cluster>.mongodb.net/admin"

show dbs

use sample_training

show collections

db.zips.find({"state": "NY"})

it iterates through the cursor.

db.zips.find({"state": "NY"}).count()

db.zips.find({"state": "NY", "city": "ALBANY"})

db.zips.find({"state": "NY", "city": "ALBANY"}).pretty()

MongoDB%20University%200d54c5c9b8fe42a79f7e2c7c23a70695/Untitled%205.png

MongoDB%20University%200d54c5c9b8fe42a79f7e2c7c23a70695/Untitled%206.png

MongoDB%20University%200d54c5c9b8fe42a79f7e2c7c23a70695/Untitled%207.png

Chapter - 3

Inserting New Document

MongoDB%20University%200d54c5c9b8fe42a79f7e2c7c23a70695/Untitled%208.png

MongoDB%20University%200d54c5c9b8fe42a79f7e2c7c23a70695/Untitled%209.png

Inserting new Documents

To learn more about Schema Validation, check out our excellent documentation page on that subject.

In this lesson we used the following commands:

mongoimport --uri="mongodb+srv://<username>:<password>@<cluster>.mongodb.net/sample_supplies" sales.json

Step one: Connect to the Atlas cluster

mongo "mongodb+srv://<username>:<password>@<cluster>.mongodb.net/admin"

Step two: navigate to the database that we need:

use sample_training

Step three, get a random document from the collection:

db.inspections.findOne();

Step four, copy this random document, and try to insert in into the collection:

db.inspections.insert({
      "_id" : ObjectId("56d61033a378eccde8a8354f"),
      "id" : "10021-2015-ENFO",
      "certificate_number" : 9278806,
      "business_name" : "ATLIXCO DELI GROCERY INC.",
      "date" : "Feb 20 2015",
      "result" : "No Violation Issued",
      "sector" : "Cigarette Retail Dealer - 127",
      "address" : {
              "city" : "RIDGEWOOD",
              "zip" : 11385,
              "street" : "MENAHAN ST",
              "number" : 1712
         }
  })

db.inspections.insert({
      "id" : "10021-2015-ENFO",
      "certificate_number" : 9278806,
      "business_name" : "ATLIXCO DELI GROCERY INC.",
      "date" : "Feb 20 2015",
      "result" : "No Violation Issued",
      "sector" : "Cigarette Retail Dealer - 127",
      "address" : {
              "city" : "RIDGEWOOD",
              "zip" : 11385,
              "street" : "MENAHAN ST",
              "number" : 1712
         }
  })

db.inspections.find({"id" : "10021-2015-ENFO", "certificate_number" : 9278806}).pretty()

insert() order

In this lesson we used the following commands:

Insert three test documents:

db.inspections.insert([ { "test": 1 }, { "test": 2 }, { "test": 3 } ])

Insert three test documents but specify the _id values:

db.inspections.insert([{ "_id": 1, "test": 1 },{ "_id": 1, "test": 2 },
                       { "_id": 3, "test": 3 }])

Find the documents with _id: 1

db.inspections.find({ "_id": 1 })

Insert multiple documents specifying the _id values, and using the"ordered": false option.

db.inspections.insert([{ "_id": 1, "test": 1 },{ "_id": 1, "test": 2 },
                       { "_id": 3, "test": 3 }],{ "ordered": false })

Insert multiple documents with _id: 1 with the default "ordered": true setting

db.inspection.insert([{ "_id": 1, "test": 1 },{ "_id": 3, "test": 3 }])

View collections in the active db

show collections

Switch the active db to training

use training

View all available databases

show dbs

Updating Document - Mongo Shell

To learn more about all available update operators in MQL, visit our excellent documentation page.

Commands used in this lesson:

Connect to your Atlas Cluster.

mongo "mongodb+srv://<username>:<password>@<cluster>.mongodb.net/admin"

Use the sample_training database as your database in the following commands.

use sample_training

Find all documents in the zips collection where the zip field is equal to "12434".

db.zips.find({ "zip": "12534" }).pretty()

Find all documents in the zips collection where the city field is equal to "HUDSON".

db.zips.find({ "city": "HUDSON" }).pretty()

Find how many documents in the zips collection have the city field equal to "HUDSON".

db.zips.find({ "city": "HUDSON" }).count()

Update all documents in the zips collection where the city field is equal to "HUDSON" by adding 10 to the current value of the"pop" field.

db.zips.updateMany({ "city": "HUDSON" }, { "$inc": { "pop": 10 } })

Update a single document in the zips collection where the zip*field is equal to* "12534" by setting the value of the "pop" field to 17630.

db.zips.updateOne({ "zip": "12534" }, { "$set": { "pop": 17630 } })

Update a single document in the zips collection where the zip*field is equal to* "12534" by setting the value of the "popupation"field to 17630.

db.zips.updateOne({ "zip": "12534" }, { "$set": { "population": 17630 } })

Find all documents in the grades collection where the student_id*field is* 151 , and the class_id field is 339.

db.grades.find({ "student_id": 151, "class_id": 339 }).pretty()

Find all documents in the grades collection where the student_id*field is* 250 , and the class_id field is 339.

db.grades.find({ "student_id": 250, "class_id": 339 }).pretty()

Update one document in the grades collection where the student_id*is 250 , and the class_id field is 339 , by adding a document element to the "scores" array.

db.grades.updateOne({ "student_id": 250, "class_id": 339 },
                    { "$push": { "scores": { "type": "extra credit",
                                             "score": 100 }
                                }
                     })

Built With

Share this project:

Updates