What is MongoDB?

MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need

  • MongoDB stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time

  • The document model maps to the objects in your application code, making data easy to work with

  • Ad hoc queries, indexing, and real time aggregation provide powerful ways to access and analyze your data

  • MongoDB is a distributed database at its core, so high availability, horizontal scaling, and geographic distribution are built in and easy to use

  • MongoDB is free and open-source, published under the GNU Affero General Public License

Learn more about how MongoDB works >

Makes development easy

MongoDB’s document model is simple for developers to learn and use, while still providing all the capabilities needed to meet the most complex requirements at any scale. We provide drivers for 10+ languages, and the community has built dozens more.

Choose your step

Here we are connecting to a locally hosted MongoDB database called test with a collection named restaurants.

# 1. Connect to MongoDB instance running on localhost
client = pymongo.MongoClient()

# Access the 'restaurants' collection in the 'test' database
collection = client.test.restaurants
# 2. Insert 
new_documents = [
  {
    "name": "Sun Bakery Trattoria",
    "stars": 4,
    "categories": ["Pizza","Pasta","Italian","Coffee","Sandwiches"]
  }, {
    "name": "Blue Bagels Grill",
    "stars": 3,
    "categories": ["Bagels","Cookies","Sandwiches"]
  }, {
    "name": "Hot Bakery Cafe",
    "stars": 4,
    "categories": ["Bakery","Cafe","Coffee","Dessert"]
  }, {
    "name": "XYZ Coffee Bar",
    "stars": 5,
    "categories": ["Coffee","Cafe","Bakery","Chocolates"]
  }, {
    "name": "456 Cookies Shop",
    "stars": 4,
    "categories": ["Bakery","Cookies","Cake","Coffee"]
  }
]

collection.insert_many(new_documents)
# 3. Query 
for restaurant in collection.find():
  pprint.pprint(restaurant)
# 4. Create Index 
collection.create_index([('name', pymongo.ASCENDING)])
# 5. Perform aggregation
pipeline = [
  {"$match": {"categories": "Bakery"}},
  {"$group": {"_id": "$stars", "count": {"$sum": 1}}}
]

pprint.pprint(list(collection.aggregate(pipeline)))
import pprint
import pymongo

def main():
  # 1. Connect to MongoDB instance running on localhost
  client = pymongo.MongoClient()

  # Access the 'restaurants' collection in the 'test' database
  collection = client.test.restaurants

  # 2. Insert 
  new_documents = [
    {"name":"Sun Bakery Trattoria", "stars":4, "categories":["Pizza","Pasta","Italian","Coffee","Sandwiches"]},
    {"name":"Blue Bagels Grill", "stars":3, "categories":["Bagels","Cookies","Sandwiches"]},
    {"name":"Hot Bakery Cafe","stars":4,"categories":["Bakery","Cafe","Coffee","Dessert"]},
    {"name":"XYZ Coffee Bar","stars":5,"categories":["Coffee","Cafe","Bakery","Chocolates"]},
    {"name":"456 Cookies Shop","stars":4,"categories":["Bakery","Cookies","Cake","Coffee"]}]

  collection.insert_many(new_documents)

  # 3. Query 
  for restaurant in collection.find():
      pprint.pprint(restaurant)

  # 4. Create Index 
  collection.create_index([('name', pymongo.ASCENDING)])

  # 5. Perform aggregation
  pipeline = [
    {"$match": {"categories": "Bakery"}},
    {"$group": {"_id": "$stars", "count": {"$sum": 1}}}]
  pprint.pprint(list(collection.aggregate(pipeline)))

if __name__ == '__main__':
  main()

MongoDB at scale

20,000,000+
downloads

400,000+
MongoDB University students

35,000+
GitHub repos

Need to run MongoDB?

We want to...

  • High availability through built-in replication and failover

  • Horizontal scalability with native sharding

  • End-to-end security

  • Native document validation and schema exploration with Compass

  • Always available global support

  • Management tooling for automation, monitoring, and backup

  • Fully elastic database as a service with built-in best practices

  • High availability through built-in replication and failover

  • Horizontal scalability with native sharding

  • End-to-end security

  • Native document validation and schema exploration with Compass

  • Always available global support

  • Management tooling for automation, monitoring, and backup

  • High availability through built-in replication and failover

  • Horizontal scalability with native sharding

  • End-to-end security

  • Native document validation and schema exploration with Compass

  • Always available global support

  • Fully elastic database as a service with built-in best practices

Trusted by thousands of teams