MongoDB is a document-oriented database system. It is used to store JSON objects.
The problem it solves is working with heterogenous data which does not fit into a schema or where creating a schema would be overly complicated. If it fits into a schema, use a SQL database like MySQL/MariaDB or Posgres. If you just need to store key/value pairs, use a Key-Value Store.
Concepts
- Document
- The basic unit of MongoDB, similar to a row in SQL databases. Documents are analogous to JSON objects but exist in the database in a more type-rich format known as BSON.1
- Collection
- A grouping of MongoDB documents. A collection is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection have a similar or related purpose.2
- Database
- A physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.3
DB Installation
On Ubuntu, you can simply run
sudo apt-get install mongodb
DB CLI Tools
Start the mongo shell and show help:
$ mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2020-05-11T10:36:11.938+0200 I STORAGE [initandlisten]
2020-05-11T10:36:11.938+0200 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-05-11T10:36:11.938+0200 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-05-11T10:36:12.720+0200 I CONTROL [initandlisten]
2020-05-11T10:36:12.720+0200 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-05-11T10:36:12.720+0200 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2020-05-11T10:36:12.720+0200 I CONTROL [initandlisten]
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memory, 'global' is default
use <db_name> set current database
db.foo.find() list objects in collection foo
db.foo.find( { a : 1 } ) list objects in foo where a == 1
it result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit quit the mongo shell
List all databases:
> show dbs
admin 0.000GB
local 0.000GB
Use a db:
> use admin
switched to db admin
Create a new collection:
> db.createCollection("examplecollection")
{ "ok" : 1 }
Show collections:
> show collections
examplecollection
system.version
Insert document into a collection:
> db.examplecollection.insertOne({'username': 'moose', 'status': ['admin', 'active']})
{
"acknowledged" : true,
"insertedId" : ObjectId("5eb9146abea1517a1e9e8245")
}
> db.examplecollection.insertOne({'username': 'test', 'status': ['active'], 'foo': 'bar'})
{
"acknowledged" : true,
"insertedId" : ObjectId("5eb914a6bea1517a1e9e8246")
}
Show existing documents of a collection:
> db.examplecollection.find({})
{ "_id" : ObjectId("5eb9146abea1517a1e9e8245"), "username" : "moose", "status" : [ "admin", "active" ] }
{ "_id" : ObjectId("5eb914a6bea1517a1e9e8246"), "username" : "test", "status" : [ "active" ], "foo" : "bar" }
Find document with exact matches:
> db.examplecollection.find({'username': 'moose'})
{ "_id" : ObjectId("5eb9146abea1517a1e9e8245"), "username" : "moose", "status" : [ "admin", "active" ] }
Fuzzy regex search:
> db.examplecollection.find({'username': /oose$/})
{ "_id" : ObjectId("5eb9146abea1517a1e9e8245"), "username" : "moose", "status" : [ "admin", "active" ] }
Before we continue, we need to create a user
> db.createUser({user: "testuser", pwd: "abc123", roles: [{role: "userAdminAnyDatabase", db: "admin"}]})
Successfully added user: {
"user" : "testuser",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
Python
Install the database driver pymongo
.
from pymongo import MongoClient
client = MongoClient("mongodb://localhost:27017/")
with client:
db = client.admin
# Get all
documents = db.examplecollection.find()
for document in documents:
print(document)
# Delete the test user
db.examplecollection.delete_many({"username": "test"})
There is of course a lot more to write about MongoDB, but those are the very basics.
Professional Setup
You might want to run MongoDB at a trusted provider like AWS, Google Cloud.
The key things to think about are database replications and sharding. Those topics are essentially about speed, load balancing, scalability, data loss / inconsistencies.
See also
- Official Website
- Stack Exchange: