Ticker

6/recent/ticker-posts

MongoDB Shell Commands

MongoDB Shell Commands

MongoDB is a NoSQL database that allows you to interact with it using various methods, including the MongoDB Shell. The MongoDB Shell is a powerful command-line interface that enables you to interact with your MongoDB databases, collections, and documents using JavaScript-like syntax.

Connecting to a MongoDB Server:

To connect to a MongoDB server using the MongoDB Shell, open your terminal or command prompt and type:


mongo

This will establish a connection to the default MongoDB server running on your localhost. If you want to connect to a specific server or provide connection options, you can use:

bash
mongo mongodb://hostname:port/databaseName

Basic Commands:

  1. use

    • Syntax: use <databaseName>
    • Description: Switches to the specified database.
    • Example: use mydb
  2. show dbs

    • Syntax: show dbs
    • Description: Lists all available databases on the MongoDB server.
    • Example: show dbs
  3. show collections

    • Syntax: show collections
    • Description: Lists all collections in the current database.
    • Example: show collections

CRUD Operations:

  1. db.collection.insertOne()

    • Syntax: db.collection.insertOne({<document>})
    • Description: Inserts a single document into the specified collection.
    • Example: db.users.insertOne({ name: "John Doe", age: 30, email: "john@example.com" })
  2. db.collection.insertMany()

    • Syntax: db.collection.insertMany([<document1>, <document2>, ...])
    • Description: Inserts multiple documents into the specified collection.
    • Example: db.users.insertMany([{ name: "Jane Doe", age: 25, email: "jane@example.com" }, { name: "Bob Smith", age: 35, email: "bob@example.com" }])
  3. db.collection.find()

    • Syntax: db.collection.find({<query>})
    • Description: Retrieves documents from the collection that match the specified query.
    • Example: db.users.find({ age: { $gte: 30 } })
  4. db.collection.updateOne()

    • Syntax: db.collection.updateOne({<filter>}, { $set: {<update>} })
    • Description: Updates a single document that matches the filter with the given update.
    • Example: db.users.updateOne({ name: "John Doe" }, { $set: { age: 31 } })
  5. db.collection.updateMany()

    • Syntax: db.collection.updateMany({<filter>}, { $set: {<update>} })
    • Description: Updates all documents that match the filter with the given update.
    • Example: db.users.updateMany({ age: { $gte: 30 } }, { $set: { isAdult: true } })
  6. db.collection.deleteOne()

    • Syntax: db.collection.deleteOne({<filter>})
    • Description: Deletes a single document that matches the filter.
    • Example: db.users.deleteOne({ name: "Jane Doe" })
  7. db.collection.deleteMany()

    • Syntax: db.collection.deleteMany({<filter>})
    • Description: Deletes all documents that match the filter.
    • Example: db.users.deleteMany({ age: { $lt: 18 } })

Indexing:

  1. db.collection.createIndex()

    • Syntax: db.collection.createIndex({<field>: 1})
    • Description: Creates an ascending index on the specified field in the collection.
    • Example: db.users.createIndex({ email: 1 })
  2. db.collection.getIndexes()

    • Syntax: db.collection.getIndexes()
    • Description: Retrieves all the indexes in the collection.
    • Example: db.users.getIndexes()

Aggregation:

MongoDB supports aggregation pipelines that allow you to process and transform data in various ways. Aggregation stages include $match, $group, $project, $sort, and many more.

Example of an aggregation query:

javascript
db.orders.aggregate([
{ $match: { status: "completed" } },
{ $group: { _id: "$customer", totalAmount: { $sum: "$amount" } } }
]);

This will retrieve the total amount spent by each customer for orders with the status "completed."

Conclusion:

MongoDB Shell commands allow you to interact with MongoDB databases efficiently. You can perform CRUD operations, manage indexes, and use aggregation pipelines to process data according to your requirements. With this brief documentation, you should have a good starting point to explore the MongoDB Shell and its capabilities.

    Post a Comment

    0 Comments