MongoDB

MongoDB NoSQL database commands and queries

20 commands
Search & Copy
Save Favorites
20
Total Commands
100%
Free Access
Lightning Fast

Available Commands

Click on any command to copy it instantly

20 commands available
20 of 20
Resources

Master MongoDB

Become proficient in MongoDB NoSQL database

mongodb

Connect to MongoDB

Connect to a MongoDB server

mongo --host localhost --port 27017
mongodb
connect
cli
mongodb

List Databases

Display all databases on the server

show dbs
mongodb
databases
list
mongodb

Select Database

Switch to a specific database

use mydb
mongodb
database
select
mongodb

List Collections

Show collections in the current database

show collections
mongodb
collections
list
mongodb

Insert One Document

Add a single document to a collection

db.users.insertOne({name: 'Alice', age: 25})
mongodb
insert
document
mongodb

Insert Multiple Documents

Add multiple documents at once

db.users.insertMany([{name:'Bob', age:30}, {name:'Charlie', age:28}])
mongodb
insert
multiple
mongodb

Find All Documents

Retrieve all documents from a collection

db.users.find()
mongodb
find
query
mongodb

Find with Filter

Retrieve documents matching a filter

db.users.find({age: 25})
mongodb
find
filter
mongodb

Update One Document

Update a single document

db.users.updateOne({name:'Alice'}, {$set:{age:26}})
mongodb
update
document
mongodb

Update Multiple Documents

Update multiple documents at once

db.users.updateMany({age:25}, {$set:{active:true}})
mongodb
update
multiple
mongodb

Delete One Document

Delete a single document from a collection

db.users.deleteOne({name:'Alice'})
mongodb
delete
document
mongodb

Delete Multiple Documents

Delete multiple documents at once

db.users.deleteMany({age:30})
mongodb
delete
multiple
mongodb

Create Index

Create an index on a field

db.users.createIndex({name:1})
mongodb
index
performance
mongodb

Drop Index

Remove an index from a collection

db.users.dropIndex('name_1')
mongodb
index
drop
mongodb

Count Documents

Count number of documents in a collection

db.users.countDocuments()
mongodb
count
query
mongodb

Aggregate Documents

Perform aggregation operations on a collection

db.orders.aggregate([{$match:{status:'pending'}}, {$group:{_id:'$item', total:{$sum:'$qty'}}}])
mongodb
aggregate
analytics
mongodb

Get Distinct Values

Retrieve unique values for a field

db.users.distinct('age')
mongodb
distinct
query
mongodb

Drop Collection

Remove an entire collection

db.users.drop()
mongodb
drop
collection
mongodb

Rename Collection

Rename an existing collection

db.users.renameCollection('customers')
mongodb
rename
collection
mongodb

Drop Database

Delete the entire database

db.dropDatabase()
mongodb
drop
database

Missing a command?

Help us improve this collection by suggesting commands that should be added. Your contributions help the entire developer community!