The massive volumes data generated by modern interconnected systems and devices has spawned a new kind of database known as NoSQL. Perhaps the best known of this new breed of non-relational database is MongoDB. Unlike traditional relational databases (RDBMSes), MongoDB does not contain tables. Instead, it stores data as collections of documents.
In the blog, we learned how to create a new database and collection using the Navicat for MongoDB database management & design tool. In today's follow-up, we'll learn about MongoDB documents and add some to our collection.
While MongoDB shares some of the same terms as those of traditional RDBMSes, others are unique to NoSQL databases. To help clarify, here's a table that compares RDBMS terminology to that of MongoDB:
RDBMSMongoDBDatabaseDatabaseTableCollectionTuple/RowDocumentcolumnFieldTable JoinEmbedded DocumentsPrimary KeyPrimary Key (Default key _id is provided by mongodb)MongoDB stores data as documents. BSON is a binary representation of JSON documents, though it contains additional data types, in addition to JSON. MongoDB documents are composed of field:value pairs and have the following structure:
{ field1: value1, field2: value2, field3: value3, ... fieldN: valueN }The value of a field can be any valid BSON data type, including other documents, arrays, and arrays of documents. Here's and example of a document that contains information about an American city. Notice the different data types:
// 1 { "_id": "01005", "city": "BARRE", "loc": [ -72.108354, 42.409698 ], "pop": NumberInt("4546"), "state": "MA" } // 2 { "_id": "01012", "city": "CHESTERFIELD", "loc": [ -72.833309, 42.38167 ], "pop": NumberInt("177"), "state": "MA" } // 3 //etc...In the last blog, we created a database named "my_mongo_db" and collection named "my_first_collection". Now, we'll add some data to the collection in the form of documents.
- The first step is to open the collection that we wish to add the document to. Select the "my_first_collection" object in the Object pane and click the Open Collection button on the Objects toolbar:
That will open the collection in a new tab. - You'll find the Document operations at the bottom of the tab. Click the Plus sign to add a document:
- In the Add Document dialog, enter the following fields, which are similar to those of the document samples above:{
"_id": "01005",
"city": "BARRE",
"loc": [
-72.108354,
42.409698
],
"pop": 4546,
"state": "MA"
}
- It's a good idea to validate the document before saving it. You can do that via the Validate button. The above data should produce a success message. Should errors be encountered, an error message will be presented with the first error in the document. The error will also include the line and column number in order to easily identify the error in the document:
- Click the Add button to close the dialog and save the new document. You should now see it in the Collection tab:
You can add more documents by following the same process as above:
Now that we've learned how to add documents to our collection, in the next blog, we'll cover how to view, delete, and edit documents in Navicat for MongoDB.
Cover the basics of querying, importing/exporting data, and working with the mongo shell and the MongoDB aggregation pipeline.
Covered in this MongoDB tutorial:
Show all lessons
Hide lessons
Introduction to MongoDB and Studio 3T Introduction to MongoDB Atlas Connecting to MongoDB The MongoDB Basics: Databases, Collections and Documents Using SQL in MongoDB Aggregation Importing and Exporting MongoDB Data Running MongoDB Queries on the mongo Shell
Start MongoDB 101: Introduction to MongoDBMongoDB 201: Querying MongoDB Data
Build upon the MongoDB basics
Total length: 3 hours
Learn how to perform CRUD operations, write find() and aggregation queries, filter elements from arrays, use SQL to query MongoDB, and create views from collections.
Covered in this MongoDB tutorial:
Show all lessons
Hide lessons
Performing CRUD (Create, Read, Update, Delete) Operations Building MongoDB find() Queries The MongoDB find() Method Working with the MongoDB Aggregation Pipeline The MongoDB Aggregate Method Querying Arrays Using MongoDB $elemMatch Querying Embedded Documents in MongoDB Arrays Querying MongoDB with SQL SELECT Statements Working with MongoDB Views
Start MongoDB 201: Performing CRUD OperationsMongoDB 301: Aggregation
Learn how to put MongoDB Aggregation to work
Total length: 3 hours
Learn how to create advanced Aggregations, work with arrays in the Aggregation pipeline, lookup data in other collections, use Studio 3T to debug your Aggregation. Also covered, using Reschema to restructure your data and using Aggregation for reporting with spreadsheets and Atlas Charts.
Covered in this MongoDB tutorial:
Show all lessons
Hide all lessons
Building a Basic Aggregation Introducing the Aggregation Editor Working with Arrays in the Aggregation Pipeline Adding Lookup Data to the Aggregation Pipeline Working with Reschema for MongoDB Reporting with Studio 3T Aggregations
Start MongoDB 301: AggregationKind words from our students
This is a good course. I have recommended my Advanced Data Technologies class to kickstart with it, they are learning MongoDB.
Dr. Sethunya Rosie Joseph, School of Computing & Informations Systems at Botswana Accountancy College
I am really happy that I can work independently after attending the MongoDB 101 and 201 courses. Thank you.
Academy 3T student
Many people come from the relational database world, so the initial relationship between relational concepts and MongoDB concepts make it so clear to understand. Nice!
MongoDB 101 student
This course is undoubtedly the best course on MongoDB.
MongoDB 101 student
As a beginner to MongoDB, this is the best way I can make myself comfortable with document-based NoSQL.
MongoDB 101 student
Thank you for creating this course. It’s very helpful for beginners.
MongoDB 101 student
Great, quick course. I struggle a bit with JSON, so Studio 3T was a great help to query an existing MongoDB database.
Luis, MongoDB 101 student
Thank you for providing true value to the people! This was very much appreciated!
MongoDB 101 student
It was good learning. Prior to this, I didn’t have much knowledge of NoSQL, but post this course, I am aware of the concepts in NoSQL based on MongoDB. Thanks for the course.
MongoDB 101 student
The course is very helpful for beginners who wish to learn MongoDB.
MongoDB 101 student
Very good information and material.
MongoDB 101 student
Great course and wonderful use to showcase your tool.
Michael, MongoDB 101 student
Extremely useful to learn the basics of MongoDB. Highly recommended.
MongoDB 101 student
I really liked the course, it is very well explained and the exercises were very practical.
MongoDB 101 student
Thanks for this excellent course.
MongoDB 101 student
Very nice basics course, and Studio 3T looks indeed very nice as well.
MongoDB 101 student
It provides a complete set of tutorials, easy to comprehend and follow.
Academy 3T student
About Academy 3T
Academy 3T is the most practical learning resource for MongoDB. Our MongoDB tutorials and courses are designed to get students up and running fast, regardless of one’s skill level or experience.
Developed by the creators of Studio 3T, the most widely-used professional tool for MongoDB, Academy 3T is an extension of 3T’s Education Program, which has supported more than 3,000 teachers, students, and researchers worldwide since 2017.