0% found this document useful (0 votes)
15 views3 pages

MongoDB NodeJS Python CheatSheet

The document provides a comprehensive overview of MongoDB commands for both Node.js and Python, detailing operations for reading, creating, updating, and deleting documents. It includes specific code examples for each command in both programming languages, as well as bonus utilities applicable to both. This serves as a practical guide for developers working with MongoDB in these environments.

Uploaded by

Mayank Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

MongoDB NodeJS Python CheatSheet

The document provides a comprehensive overview of MongoDB commands for both Node.js and Python, detailing operations for reading, creating, updating, and deleting documents. It includes specific code examples for each command in both programming languages, as well as bonus utilities applicable to both. This serves as a practical guide for developers working with MongoDB in these environments.

Uploaded by

Mayank Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

MongoDB Commands in Node.

js and Python

Node.js MongoDB Commands

Node.js (Using `mongodb` driver)


const { MongoClient } = require('mongodb');
const uri = "mongodb://localhost:27017";
const client = new MongoClient(uri);
await client.connect();
const db = client.db("myDB");
const collection = db.collection("myCollection");
READ
- find(query): collection.find({ name: "John" }).toArray();
- findOne(query): collection.findOne({ _id: ObjectId("...") });
CREATE
- insertOne: collection.insertOne({ name: "Alice", age: 25 });
- insertMany: collection.insertMany([{ name: "Bob" }, { name: "Charlie" }]);
UPDATE
- updateOne: collection.updateOne({ name: "Alice" }, { $set: { age: 26 } });
- updateMany: collection.updateMany({ age: { $lt: 30 } }, { $inc: { age: 1 } });
- findOneAndUpdate: collection.findOneAndUpdate({ name: "Bob" }, { $set: { age:
40 } });
DELETE
- deleteOne: collection.deleteOne({ name: "Alice" });
- deleteMany: collection.deleteMany({ age: { $gt: 60 } });
- findOneAndDelete: collection.findOneAndDelete({ name: "Bob" });

Python MongoDB Commands

Python (Using `pymongo`)


from pymongo import MongoClient
client = MongoClient("mongodb://localhost:27017/")
db = client["myDB"]
collection = db["myCollection"]
READ
- find: collection.find({ "name": "John" })
- find_one: collection.find_one({ "name": "John" })
CREATE
- insert_one: collection.insert_one({ "name": "Alice", "age": 25 })
- insert_many: collection.insert_many([{ "name": "Bob" }, { "name": "Charlie" }])
collection_name.insert_one({
"messages": [
{"role": "user", "content": "My name is mayank"}
]
})

UPDATE
MongoDB Commands in Node.js and Python
- update_one: collection.update_one({ "name": "Alice" }, { "$set": { "age": 26 } })
- update_many: collection.update_many({ "age": { "$lt": 30 } }, { "$inc": { "age":
1 }
})
- find_one_and_update: collection.find_one_and_update({ "name": "Bob" }, { "$set":
{
"age": 40 } })
DELETE
- delete_one: collection.delete_one({ "name": "Alice" })
- delete_many: collection.delete_many({ "age": { "$gt": 60 } })
- find_one_and_delete: collection.find_one_and_delete({ "name": "Bob" })

Bonus Utilities (Both)

Bonus Utilities (Both Languages):


- count_documents({}) Count documents
- distinct("field") Get unique values
- aggregate([...]) Run aggregation pipeline
- replace_one() Replace an entire document
- bulk_write([...]) Perform multiple operations at once

You might also like