MongoDB is a popular, free, open source [[NoSQL]] document store database. A database can contain one or more collections of documents stored in [[JSON]] format. A collection can contain many individual documents. Each document has a [[primary key]]. Mongo can [[index]] on any attribute.
Mongo documents are **polymorphic**, which means the key-value pairs may differ. The schema is self-describing because JSON is human readable.
MongoDB supports horizontal scaling with automated node failure detection and failover. Read operations can scale across the clusters for parallelization. All writes go to the primary node and are replicated asynchronously to the secondary nodes (primary-secondary replication). There is no downtime for upgrades.
# installation
# query language
MongoDB uses a [[JavaScript]]-like [[query language]].
```javascript
// list all databases
show databases;
// list all collections
show collections;
// Method find on collection restaurants, returns all with pretty printing
db.restaurants.find().pretty()
// Return count of restaurants
db.restaurants.count();
// Return sorted list of distinct cuisines
db.restaurants.distinct("cuisine").sort();
// Return all Italian restaurants in zipcode 10023
db.restaurants.find({
"cuisine": "Italian",
"address.zipcode":"10023"}
);
// Return count of Mexican restuarants by borough, sorted descending by count
db.restaurants.aggregate([
{$match: {cuisine: "Mexican"}},
{$group: {_id: "$borough", count_borough:{$sum:1}}},
{$sort:{count_bororugh:-1}}
]);
```
Queries also support [[regular expression|regex]] between forward slashes `/ /`.