Home MongoDB
Post
Cancel

MongoDB

MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. It means 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.

  • Database: Contains collections. It’s a physical container for data.
  • Collection: A group of MongoDB documents. It’s equivalent to a table in a relational database.
  • Document: A single record in a collection. Documents are JSON-like field-and-value pairs.

MongoDB is schema-less, which means a collection does not enforce document structure. This flexibility can be powerful, but it’s also important to ensure that your application correctly manages data.

MongoDB with Python (using pymongo)

Installation

1
pip install pymongo

Basic Usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from pymongo import MongoClient

# Connect to MongoDB
client = MongoClient('mongodb://localhost:27017/')

# Select Database
db = client['mydatabase']

# Select Collection
collection = db['mycollection']

# Insert Document
collection.insert_one({"name": "John", "address": "Highway 37"})

# Find One Document
x = collection.find_one()

# Find All Documents
for x in collection.find():
  print(x)

# Query the Collection
for x in collection.find({"name": "John"}):
  print(x)

# Update Document
collection.update_one({"name": "John"}, {"$set": {"address": "Redwood Avenue"}})

# Delete Document
collection.delete_one({"name": "John"})

# Close Connection
client.close()

MongoDB with Node.js (using mongodb package)

Installation

1
npm install mongodb

Basic Usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("mydatabase");

  // Insert Document
  dbo.collection("mycollection").insertOne({ name: "John", address: "Highway 37" }, function(err, res) {
    if (err) throw err;
    console.log("1 document inserted");
  });

  // Find One Document
  dbo.collection("mycollection").findOne({}, function(err, result) {
    if (err) throw err;
    console.log(result.name);
  });

  // Find All Documents
  dbo.collection("mycollection").find({}).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
  });

  // Query the Collection
  dbo.collection("mycollection").find({ name: "John" }).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
  });

  // Update Document
  dbo.collection("mycollection").updateOne({ name: "John" }, { $set: { address: "Redwood Avenue" }}, function(err, res) {
    if (err) throw err;
    console.log("1 document updated");
  });

  // Delete Document
  dbo.collection("mycollection").deleteOne({ name: "John" }, function(err, obj) {
    if (err) throw err;
    console.log("1 document deleted");
    db.close();
  });
});
This post is licensed under CC BY 4.0 by the author.