Skip to main content

MongoDB

In the MongoDB shell (mongosh), you can perform a wide range of administrative and data-related operations. Here's an overview of what you can do:

1. Managing Databases

Show all databases

show dbs

Switch to a specific database

use <database_name>

Show current database

db

Drop (delete) a database

db.dropDatabase()

2. Managing Collections

Show all collections in the current database

show collections

Create a new collection

db.createCollection("collection_name")

Drop (delete) a collection

db.collection_name.drop()

3. CRUD Operations (Create, Read, Update, Delete)

Insert data

db.collection_name.insertOne({ key: "value" })
db.collection_name.insertMany([{ key1: "value1" }, { key2: "value2" }])

Query data

db.collection_name.find()
db.collection_name.find({ key: "value" })
db.collection_name.findOne({ key: "value" })

Update data

db.collection_name.updateOne({ key: "value" }, { $set: { key: "new_value" } })
db.collection_name.updateMany({ key: "value" }, { $set: { key: "new_value" } })

Delete data

db.collection_name.deleteOne({ key: "value" })
db.collection_name.deleteMany({ key: "value" })

4. Managing Indexes

Create an index

db.collection_name.createIndex({ key: 1 }) // 1 for ascending, -1 for descending

Show indexes

db.collection_name.getIndexes()

Drop an index

db.collection_name.dropIndex("index_name")

5. Performing Aggregations

Use aggregation pipeline

db.collection_name.aggregate([
{ $match: { key: "value" } },
{ $group: {_id: "$key", total: { $sum: 1 } } }
])

6. Managing Users and Permissions

Create a new user

db.createUser({
user: "username",
pwd: "password",
roles: [{ role: "readWrite", db: "database_name" }]
})

Show users

db.getUsers()

Drop (delete) a user

db.dropUser("username")

7. System Administration

Show server status

db.serverStatus()

Show configuration details

db.runCommand({ getParameter: 1 })

Show statistics of a collection

db.collection_name.stats()

8. Security Checks

Since your output indicates that access control is not enabled, you may want to enable authentication to secure your database. Here are some steps:

  1. Enable access control (e.g., in the mongod.conf configuration file).

  2. Create users with specific roles.

Finding the Users You Created

Since you mentioned you created users through your endpoint, you can find them in the users collection in MongoDB. Here's how you can query the users:

Show collections (to confirm the users collection exists)

show collections

Query the users

db.users.find().pretty()

This will display all documents (users) in the users collection. If you're looking for a specific user, you can filter by a field like email:

db.users.find({ email: "<user@example.com>" }).pretty()