DEV Community

Dmitry Romanoff
Dmitry Romanoff

Posted on

12 Useful MongoDB Commands Every Developer Should Know

MongoDB is a powerful NoSQL database, and while it’s easy to get started, having a good handle on some basic commands can save you time and help you understand your data better.

Here are 12 essential MongoDB commands that I find incredibly useful for everyday database management, querying, and development tasks.


1. show databases

Lists all available databases on your MongoDB server.

rs0 [direct: primary] test> show databases 
Enter fullscreen mode Exit fullscreen mode

Example Output:

dima_database_1 48.00 KiB newdatabase 1.45 MiB newdatabase_copy 1.41 MiB sample_database 32.00 KiB stressTestDB 34.64 MiB test 128.00 KiB test_db 18.16 MiB testdb 32.00 KiB 
Enter fullscreen mode Exit fullscreen mode

2. use mydatabase

Switch to the specified database. If it doesn’t exist, MongoDB will create it once you insert data.

use newdatabase 
Enter fullscreen mode Exit fullscreen mode

Output:

switched to db newdatabase 
Enter fullscreen mode Exit fullscreen mode

3. show collections

Displays all collections (similar to tables in SQL) in the current database.

rs0 [direct: primary] newdatabase> show collections 
Enter fullscreen mode Exit fullscreen mode

Output:

collection_1 collection_10 collection_2 ... 
Enter fullscreen mode Exit fullscreen mode

4. db.collection.countDocuments()

Returns the number of documents in a collection.

db.collection_1.countDocuments() 
Enter fullscreen mode Exit fullscreen mode

Output:

500 
Enter fullscreen mode Exit fullscreen mode

5. db.collection.find().limit(n).forEach(printjson)

Prints the first n documents from a collection in a readable JSON format.

db.collection_1.find().limit(3).forEach(printjson); 
Enter fullscreen mode Exit fullscreen mode

Output:

{ _id: ObjectId("686ba3cd8d1e2b93aec59f35"), name: "Item_0", value: 767, created_at: ISODate("2025-07-07T10:39:08.904Z") } ... 
Enter fullscreen mode Exit fullscreen mode

6. Custom Formatted Output with forEach

This version prints documents with custom formatting for easier visual scanning.

db.collection_1.find().limit(3).forEach(function(doc) { const id = (doc._id && doc._id.str) ? doc._id.str.substring(0, 6) : "------"; const name = doc.name ? doc.name.toString().padEnd(10) : "N/A".padEnd(10); const value = (doc.value !== undefined) ? doc.value.toString().padStart(5) : " N/A"; const date = (doc.created_at && doc.created_at.toISOString) ? doc.created_at.toISOString().substr(0, 19) : "no-date"; print(id + "\t" + name + "\t" + value + "\t" + date); }); 
Enter fullscreen mode Exit fullscreen mode

Output:

------ Item_0 767 2025-07-07T10:39:08 ------ Item_1 763 2025-07-07T10:39:09 ------ Item_2 601 2025-07-07T10:39:09 
Enter fullscreen mode Exit fullscreen mode

7. Filtered Count with Regex

Count documents where the name field starts with "Item".

db.collection_1.countDocuments({ name: /^Item/ }) 
Enter fullscreen mode Exit fullscreen mode

Output:

500 
Enter fullscreen mode Exit fullscreen mode

8. deleteMany with $in Filter

Delete specific documents based on matching values.

db.collection_1.deleteMany({ name: { $in: ["Item_20", "Item_25"] } }); 
Enter fullscreen mode Exit fullscreen mode

Output:

{ acknowledged: true, deletedCount: 2 } 
Enter fullscreen mode Exit fullscreen mode

9. insertMany to Add Documents

Add multiple new documents at once.

db.collection_1.insertMany([ { name: "Item_234", value: 234, created_at: new Date() }, { name: "Item_345", value: 345, created_at: new Date() }, { name: "Item_456", value: 456, created_at: new Date() } ]); 
Enter fullscreen mode Exit fullscreen mode

Output:

{ acknowledged: true, insertedIds: { '0': ObjectId("686bb71a123fdce1b6c63238"), '1': ObjectId("686bb71a123fdce1b6c63239"), '2': ObjectId("686bb71a123fdce1b6c6323a") } } 
Enter fullscreen mode Exit fullscreen mode

10. updateOne to Modify Documents

Update documents based on a condition.

db.collection_1.updateOne({ name: "Item_234" }, { $set: { name: "Item_2345" } }); db.collection_1.updateOne({ name: "Item_345" }, { $set: { name: "Item_3456" } }); db.collection_1.updateOne({ name: "Item_456" }, { $set: { name: "Item_4567" } }); 
Enter fullscreen mode Exit fullscreen mode

Output:

{ acknowledged: true, matchedCount: 1, modifiedCount: 1 } 
Enter fullscreen mode Exit fullscreen mode

11. Aggregation with $group, $sort, $limit

Aggregate documents to see how many times each name occurs.

db.collection_1.aggregate([ { $group: { _id: "$name", count: { $sum: 1 } } }, { $sort: { count: -1 } }, { $limit: 5 } ]); 
Enter fullscreen mode Exit fullscreen mode

Output:

[ { _id: "Item_4567", count: 2 }, { _id: "Item_3456", count: 2 }, { _id: "Item_2345", count: 2 }, { _id: "Item_451", count: 1 }, { _id: "Item_185", count: 1 } ] 
Enter fullscreen mode Exit fullscreen mode

12. Drop the Entire Database

⚠️ Be careful with this one! This will delete the selected database.

use newdatabase db.dropDatabase() 
Enter fullscreen mode Exit fullscreen mode

Output:

{ ok: 1, dropped: 'newdatabase' } 
Enter fullscreen mode Exit fullscreen mode

🔚 Conclusion

These commands are essential building blocks when working with MongoDB, especially when interacting directly via the shell. Whether you're debugging, inspecting data, or performing CRUD operations, they’ll come in handy.

Top comments (0)