Collections in MongoDB
In the previous article we looked at installing a work environment for MongoDB, and we ran our first command which showed us all the databases. We learned that as opposed to MySQL, we don’t need to create a database before hand but rather that a database in MongoDB is created at the moment we enter our first collection. So what is a collection, and how do we use it? What did we do wrong? All this and more right here.
In order to use a database, we need to use it! I know, crazy, right? It’s just like in MySQL—even the syntax. Let’s type a bit:
use test
As opposed to the bothersome MySQL, here we don’t need to use a semicolon. This is already a good reason to use MongoDB, but in this particular case I really just being bitter.
MongoDB will let us know that we’ve moved to test, but if we use our showdbs command to see all of the databases:
showdbs
We see that it didn’t return our test database. This is because we need to create a collection! What again is a collection? A collection is the MongoDB equivalent of a table. It is a collection of data. For example, pages, users, etc. Every collection represents some sort of entity, and contains JSON based objects called documents, and each object can be completely different. Take our test for example, which could contain two documents where each one looks totally different.
Confused? That’s what we have examples for. Let’s create a document:
my_first_document = { name : "ran" }
Notice that I’ve only created a document and haven’t put it anywhere yet. Just like in any programming language, I created a variable but didn’t do anything with it yet. A document can be anything and any object, for instance:
my_children = { name : ["Omri","Kfir","Daniel","Michak"] }
The document is essentially a JSON object and I can go nuts with it as much as I want. But we’re going a bit to fast—let’s make a collection, now that we’ve got our document, which is just floating around aimlessly in space. Let’s give that document life by putting it into a collection. The name of the collection will be testData. It goes like this:
db.testData.insert(my_first_document)
The feedback we get will be:
WriteResult({ "nInserted" : 1 })
In one fell swoop a few things were created here, the first of which being a database! Along with the creation of the collection, the database test was also created. If we run the show dbs command we’ll finally see it. Additionally, a collection called testData was also created. To display all collections that we have in the database that we’re currently using, we can use the following command:
show collections
And if we run it, we’ll see our amazing collection! In addition, we made a document. First abstractly, and then in actuality by using the command:
db.testData.insert(my_first_document)
In general we can make the following comparison:
MySQL |
MongoDB |
db |
db |
table |
collection |
row |
document |
It’s not an exact correspondence, for instance as opposed to MySQL we can enter in various types of documents inside of a single collection and this won’t create a scheme that if too difficult to work with. But we’ll get to that in the next article in which we’ll talk all about extracting data from MongoDB.
About the author: Ran Bar-Zik is an experienced web developer whose personal blog, Internet Israel, features articles and guides on Node.js, MongoDB, Git, SASS, jQuery, HTML 5, MySQL, and more. Translation of the original article by Aaron Raizen.
Recent Stories
Top DiscoverSDK Experts
Compare Products
Select up to three two products to compare by clicking on the compare icon () of each product.
{{compareToolModel.Error}}
{{CommentsModel.TotalCount}} Comments
Your Comment