📅  最后修改于: 2023-12-03 15:17:41.723000             🧑  作者: Mango
MongoDB is a popular NoSQL database that provides high scalability, performance, and flexibility. The MongoDB shell is a command-line interface that enables developers to interact with the database and perform various operations like query, insert, update, delete, and more. In this article, we will explore some useful MongoDB shell commands that every developer should know.
To see a list of all the databases in your MongoDB server, use the show databases
command as follows:
show databases;
This command will return a list of all the databases available in the server.
To use a specific database, run the use
command followed by the name of the database:
use myDatabase
This command will use the myDatabase
database and return a message that confirms this.
Once you have selected a database, you can view the collections within this database using the show collections
command:
show collections
This command will show all the collections inside the selected database.
To insert a new document into a collection, use the following command:
db.collectionName.insert({key:value, key:value});
Replace collectionName
with the name of the collection you want to add to, and key:value
with the data you want to add.
To find an existing document within a collection, use the find
command followed by a document filter:
db.collectionName.find({key:value});
Replace collectionName
with the name of the collection you want to search in, and key:value
with the data you want to find.
To update an existing document within a collection, use the update
command followed by a document filter and the new document data:
db.collectionName.update({key:value}, {$set:{key:newValue}});
Replace collectionName
with the name of the collection you want to update, key:value
with the data you want to update, and key:newValue
with the new data.
To remove an existing document within a collection, use the remove
command followed by a document filter:
db.collectionName.remove({key:value});
Replace collectionName
with the name of the collection you want to remove from and key:value
with the data you want to remove.
To perform data aggregation on a collection, use the aggregate
command:
db.collectionName.aggregate({$group:{_id:key, value:{$sum:1}}});
Replace collectionName
with the name of the collection you want to aggregate, key
with the data you want to group by, and value
with the operation you want to perform on the data.
To create an index on a collection, use the createIndex
command:
db.collectionName.createIndex({key:1});
Replace collectionName
with the name of the collection you want to create an index on and key
with the data you want to create an index for.
To drop an existing index on a collection, use the dropIndex
command:
db.collectionName.dropIndex({key:1});
Replace collectionName
with the name of the collection you want to drop an index on and key
with the data you want to remove the index for.
To backup and restore a MongoDB database, use the mongodump
and mongorestore
commands respectively:
mongodump --db myDatabase
mongorestore --db myDatabase dump/myDatabase
Replace myDatabase
with the name of the database you want to backup and restore.
In this article, we have covered some useful MongoDB shell commands that can help you interact with your database and perform various operations. These commands are just a few examples of the many available commands. By using the MongoDB shell, developers can manage their databases with ease and efficiency.