```meta title: MongoDB: Basic Operations and Architecture published: 2020-03-05T04:00:08+00:00 updated: 2020-04-08T17:36:25+00:00 ``` This is the second post in the MongoDB series, where we will take a look at the [CRUD operations](https://stackify.com/what-are-crud-operations/) they support, the data model and architecture used. Other posts in this series: * [MongoDB: an Introduction](/blog/ribw/mongodb-an-introduction/) * [MongoDB: Basic Operations and Architecture](/blog/ribw/mongodb-basic-operations-and-architecture/) (this post) * [Developing a Python application for MongoDB](/blog/ribw/developing-a-python-application-for-mongodb/) This post is co-authored wih Classmate, and in it we will take an explorative approach using the `mongo` command line shell to execute commands against the database. It even has TAB auto-completion, which is awesome! ---------- Before creating any documents, we first need to create somewhere for the documents to be in. And before we create anything, the database has to be running, so let’s do that first. If we don’t have a service installed, we can run the `mongod` command ourselves in some local folder to make things easier: ``` $ mkdir -p mongo-database $ mongod --dbpath mongo-database ``` Just like that, we will have Mongo running. Now, let’s connect to it using the `mongo` command in another terminal (don’t close the terminal where the server is running, we need it!). By default, it connects to localhost, which is just what we need. ``` $ mongo ``` ## Create ### Create a database Let’s list the databases: ``` > show databases admin 0.000GB config 0.000GB local 0.000GB ``` Oh, how interesting! There’s already some databases, even though we just created the directory where Mongo will store everything. However, they seem empty, which make sense. Creating a new database is done by `use`-ing a name that doesn’t exist. Let’s call our new database «helloworld». ``` > use helloworld switched to db helloworld ``` Good! Now the «local variable» called `db` points to our `helloworld` database. ``` > db helloworld ``` What happens if we print the databases again? Surely our new database will show up now… ``` > show databases admin 0.000GB config 0.000GB local 0.000GB ``` …maybe not! It seems Mongo won’t create the database until we create some collections and documents in it. Databases contain collections, and inside collections (which you can think of as tables) we can insert new documents (which you can think of as rows). Like in many programming languages, the dot operator is used to access these «members». ### Create a document Let’s add a new greeting into the `greetings` collection: ``` > db.greetings.insert({message: "¡Bienvenido!", lang: "es"}) WriteResult({ "nInserted" : 1 }) > show collections greetings > show databases admin 0.000GB config 0.000GB helloworld 0.000GB local 0.000GB ``` That looks promising! We can also see our new `helloworld` database also shows up. The Mongo shell actually works on JavaScript-like code, which is why we can use a variant of JSON (BSON) to insert documents (note the lack of quotes around the keys, convenient!). The [`insert`](https://docs.mongodb.com/manual/reference/method/db.collection.insert/index.html) method actually supports a list of documents, and by default Mongo will assign a unique identifier to each. If we don’t want that though, all we have to do is add the `_id` key to our documents. ``` > db.greetings.insert([ ... {message: "Welcome!", lang: "en"}, ... {message: "Bonjour!", lang: "fr"}, ... ]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) ``` ### Create a collection In this example, we created the collection `greetings` implicitly, but behind the scenes Mongo made a call to [`createCollection`](https://docs.mongodb.com/manual/reference/method/db.createCollection/). Let’s do just that: ``` > db.createCollection("goodbyes") { "ok" : 1 } > show collections goodbyes greetings ``` The method actually has a default parameter to configure other options, like the maximum size of the collection or maximum amount of documents in it, validation-related options, and so on. These are all described in more details in the documentation. ## Read To read the contents of a document, we have to [`find`](https://docs.mongodb.com/manual/reference/method/db.collection.find/index.html) it. ``` > db.greetings.find() { "_id" : ObjectId("5e74829a0659f802b15f18dd"), "message" : "¡Bienvenido!", "lang" : "es" } { "_id" : ObjectId("5e7487b90659f802b15f18de"), "message" : "Welcome!", "lang" : "en" } { "_id" : ObjectId("5e7487b90659f802b15f18df"), "message" : "Bonjour!", "lang" : "fr" } ``` That’s a bit unreadable for my taste, can we make it more [`pretty`](https://docs.mongodb.com/manual/reference/method/cursor.pretty/index.html)? ``` > db.greetings.find().pretty() { "_id" : ObjectId("5e74829a0659f802b15f18dd"), "message" : "¡Bienvenido!", "lang" : "es" } { "_id" : ObjectId("5e7487b90659f802b15f18de"), "message" : "Welcome!", "lang" : "en" } { "_id" : ObjectId("5e7487b90659f802b15f18df"), "message" : "Bonjour!", "lang" : "fr" } ``` Gorgeous! We can clearly see Mongo created an identifier for us automatically. The queries are also JSON, and support a bunch of operators (prefixed by `$`), known as [Query Selectors](https://docs.mongodb.com/manual/reference/operator/query/). Here’s a few:
Operation | Syntax | RDBMS equivalent |
---|---|---|
Equals |
{key: {$eq: value}}
Shorthand:
{key: value}
|
where key = value
|
Less Than |
{key: {$lte: value}}
|
where key < value
|
Less Than or Equal |
{key: {$lt: value}}
|
where key <= value
|
Greater Than |
{key: {$gt: value}}
|
where key > value
|
Greater Than or Equal |
{key: {$gte: value}}
|
where key >= value
|
Not Equal |
{key: {$ne: value}}
|
where key != value
|
And |
{$and: [{k1: v1}, {k2: v2}]}
|
where k1 = v1 and k2 = v2
|
Or |
{$or: [{k1: v1}, {k2: v2}]}
|
where k1 = v1 or k2 = v2
|