MongoDB Basic Commands
I can provide you with a more extensive list covering various aspects of MongoDB operations. Keep in mind that some commands might be deprecated or modified in newer versions of MongoDB, so always refer to the official MongoDB documentation for the most up-to-date information. Here's a diverse selection of MongoDB commands categorized by their functionalities:
Database Operations:
- use <database>: Switches to a specific database.
- show dbs: Displays a list of all databases on the server.
- db.dropDatabase(): Deletes the current database.
- db.createCollection(): Creates a new collection in the current database.
Collection Operations:
- show collections: Lists all collections in the current database.
- db.collectionName.drop(): Deletes a collection.
- db.collectionName.insertOne(): Inserts a single document into a collection.
- db.collectionName.insertMany(): Inserts multiple documents into a collection.
- db.collectionName.find(): Retrieves documents from a collection.
- db.collectionName.updateOne(): Updates a single document.
- db.collectionName.updateMany(): Updates multiple documents.
- db.collectionName.deleteOne(): Deletes a single document.
- db.collectionName.deleteMany(): Deletes multiple documents.
Querying:
- db.collectionName.find().limit(n): Limits the number of documents returned.
- db.collectionName.find().sort({ field: 1 }): Sorts documents in ascending order.
- db.collectionName.find().sort({ field: -1 }): Sorts documents in descending order.
- db.collectionName.find({ field: { $gt: value } }): Retrieves documents with a field greater than a specified value.
- db.collectionName.find({ field: { $lt: value } }): Retrieves documents with a field less than a specified value.
- db.collectionName.find({ field: { $in: [value1, value2] } }): Retrieves documents where a field matches any value in an array.
Indexing:
- db.collectionName.createIndex({ field: 1 }): Creates a single-field index in ascending order.
- db.collectionName.createIndex({ field: -1 }): Creates a single-field index in descending order.
- db.collectionName.createIndex({ field1: 1, field2: -1 }): Creates a compound index.
Aggregation:
- db.collectionName.aggregate(): Performs aggregation operations on documents in a collection.
User Management:
- use admin: Switches to the admin database.
- db.createUser(): Creates a new user.
- db.updateUser(): Updates an existing user.
- db.dropUser(): Deletes a user.
Replica Sets:
- rs.initiate(): Initializes a replica set.
- rs.status(): Checks the status of the replica set.
- rs.add(): Adds a new member to the replica set.
Sharding:
- sh.addShard(): Adds a shard to a sharded cluster.
- sh.enableSharding(): Enables sharding on a database.
- sh.shardCollection(): Enables sharding on a collection.
Backup and Restore:
- mongodump: Creates a binary export of the contents of a database.
- mongorestore: Restores a database from the output created by mongodump.
Server Management:
- db.shutdownServer(): Shuts down the current server.
- db.serverStatus(): Provides an overview of the database server's current state.
Replication:
- rs.initiate(): Initiates a replica set.
- rs.add(): Adds a member to the replica set.
- rs.stepDown(): Forces the current primary to step down and initiate an election.
Profiling:
- db.setProfilingLevel(): Sets the profiling level for a database.
- db.getProfilingStatus(): Retrieves the current profiling status.
- db.system.profile.find(): Retrieves the current profiled operations.
Text Search:
- db.collectionName.createIndex({ field: "text" }): Creates a text index.
- db.collectionName.find({ $text: { $search: "searchQuery" } }): Performs a text search.
GridFS (Storing Large Files):
- db.fs.files.find(): Retrieves metadata about files stored in GridFS.
- db.fs.chunks.find(): Retrieves the actual file data stored in GridFS.
TTL (Time-to-Live) Collections:
- db.collectionName.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 }): Creates a TTL index.
- db.collectionName.find(): Automatically removes documents once their TTL expires.
Geospatial Queries:
- db.collectionName.createIndex({ location: "2dsphere" }): Creates a geospatial index.
- db.collectionName.find({ location: { $near: { $geometry: { type: "Point", coordinates: [longitude, latitude] }, $maxDistance: distanceInMeters } } }): Performs a geospatial query.
Change Streams:
- db.collectionName.watch(): Opens a change stream on a collection to watch for changes.
Bulk Write Operations:
- db.collectionName.bulkWrite(): Performs multiple write operations in bulk.
Transactions:
- session.startTransaction(): Starts a new transaction.
- session.commitTransaction(): Commits a transaction.
- session.abortTransaction(): Aborts a transaction.
Authentication:
- db.auth(): Authenticates a user against a database.
- db.changeUserPassword(): Changes the password of an existing user.
- db.logout(): Logs out the current session.
SSL/TLS Configuration:
- net.ssl.PEMKeyFile: Specifies the location of the PEM file containing the SSL certificate.
Configuration File Options:
- systemLog.path: Specifies the location of the MongoDB log file.
- storage.dbPath: Specifies the directory where MongoDB stores data files.
Explain Plan:
- db.collectionName.explain(): Provides information on the query execution plan.
- db.collectionName.find().explain("executionStats"): Provides detailed execution statistics.
Data Import/Export:
- mongoimport: Imports data from a JSON, CSV, or TSV file into a MongoDB collection.
- mongoexport: Exports data from a MongoDB collection to a JSON, CSV, or TSV file.
Diagnostic Commands:
- db.currentOp(): Displays information on the currently executing operations.
- db.killOp(): Terminates a specified operation.
Miscellaneous:
- db.version(): Retrieves the version of the MongoDB server.
- db.serverBuildInfo(): Retrieves build information for the MongoDB server.
Maintenance:
- db.repairDatabase(): Repairs a corrupted database.
- db.adminCommand({ compact: 'collectionName' }): Performs manual
0 Comments
CAN FEEDBACK
Emoji