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 Working with NoSQL Databases 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.
Comparing MongoDB and RDBMS Objects
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:
RDBMS | MongoDB |
---|---|
Database | Database |
Table | Collection |
Tuple/Row | Document |
column | Field |
Table Join | Embedded Documents |
Primary Key | Primary Key (Default key _id is provided by mongodb) |
MongoDB Documents Explained
MongoDB stores data as BSON 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...
Creating a New Document in Navicat for MongoDB
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:
Conclusion
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.