July 13, 2021 • ––– views
MongoDB Linux commands
Overview
This page covers MongoDB specific commands on Linux, most of them will probably be used within MongoDB shell. You can use it as a reference in case you want to do some task quickly, or you might find something new and beneficial when using MongoDB.
List all databases
show dbs
Select a database
use mydb_name
List all collections for a database
This should be executed after selecting a database
show collections
Create a new collection for a database
This should be executed after selecting a database, the keyword db
here is a reference to the database you selected.
db.createCollection('collection_name')
List all indexes for a collection
This should be executed after selecting a database, the keyword db
here is a reference to the database you selected, replace collection_name
with the collection you want to list it's indexes.
db.collection_name.getIndexes()
New Index
This should be executed after selecting a database, the keyword db
here is a reference to the database you selected, collection_name
is the collection you want to create the new index for. field_name
is the field you want the indexed.
List the collection indexes after to verify the result.
db.collection_name.createIndex({ field_name: 1 })
Dropping Index
This should be executed after selecting a database, the keyword db
here is a reference to the database you selected, collection_name
is the collection you want to drop the index from. field_name
is the field that contains the index.
List the collection indexes after to verify the result.
db.collection_name.dropIndex({ field_name: 1 })