[go: up one dir, main page]

0% found this document useful (0 votes)
52 views2 pages

Mongodb Pymongo Cheatsheet

This document is a cheat sheet for MongoDB and PyMongo commands. It covers basic MongoDB shell commands, CRUD operations, query operators, and PyMongo setup and operations. The cheat sheet provides concise examples for each command and operation type.

Uploaded by

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

Mongodb Pymongo Cheatsheet

This document is a cheat sheet for MongoDB and PyMongo commands. It covers basic MongoDB shell commands, CRUD operations, query operators, and PyMongo setup and operations. The cheat sheet provides concise examples for each command and operation type.

Uploaded by

amish.singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Git Commands Cheat Sheet

1. MongoDB Shell Basics

mongo: Start the MongoDB shell.

show dbs: List all databases.

use <db>: Switch to or create a new database.

show collections: List collections in the current database.

db.dropDatabase(): Drop the current database.

2. CRUD Operations (Shell)

db.collection.insertOne({...}): Insert a single document.

db.collection.insertMany([{...}, {...}]): Insert multiple documents.

db.collection.find(): Find all documents.

db.collection.findOne(): Find the first document.

db.collection.updateOne(filter, update): Update a single document.

db.collection.updateMany(filter, update): Update multiple documents.

db.collection.deleteOne(filter): Delete a single document.

db.collection.deleteMany(filter): Delete multiple documents.

3. Query Operators

$gt / $lt / $gte / $lte: Greater than / less than / etc.

$in / $nin: Match any/not any in an array.

$or / $and: Combine multiple conditions.

$regex: Use regular expressions in queries.

$exists: Check if a field exists.

4. PyMongo Setup

from pymongo import MongoClient: Import PyMongo client.

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


Git Commands Cheat Sheet

db = client['mydatabase']: Access a database.

collection = db['mycollection']: Access a collection.

5. PyMongo CRUD

collection.insert_one({...}): Insert a single document.

collection.insert_many([{...}, {...}]): Insert multiple documents.

collection.find(): Retrieve all documents.

collection.find_one(): Retrieve a single document.

collection.update_one(filter, {'$set': {...}}): Update one document.

collection.update_many(filter, {'$set': {...}}): Update multiple documents.

collection.delete_one(filter): Delete one document.

collection.delete_many(filter): Delete multiple documents.

6. PyMongo Querying

collection.find({'age': {'$gt': 20}}): Query with condition.

collection.find({'name': {'$regex': '^A'}}): Regex query.

collection.find().sort('age', 1): Sort ascending.

collection.find().limit(5): Limit number of results.

You might also like