Basic Mongo shell commands
MongoDB is an open-source, cross-platform, and distributed document-based database designed for ease of application development and scaling. It is also a leading NoSQL database.
Mongo Shell
The Mongo shell is an interactive JavaScript interface to MongoDB. You can use the mongo shell to query and update data and perform administrative operations.
Mongo Shell is the quickest way to connect, configure, query, and work with your MongoDB database. It acts as a command-line interface (CLI) of the MongoDB server.
Installation of the mongo shell
The mongo shell is included as part of the MongoDB server installation. If you have already installed MongoDB globally, you can use it from CLI.
Alternatively, if you would like to download the mongo shell separately from the MongoDB Server, you can install the shell as a standalone package for Windows, Linux, or macOS.
Connect to MongoDB database
Open the mongo shell from CLI and it will automatically connect them to the MongoDB database. Default mongo instance run on localhost and default port 27017.
Mongo shell commands
Now we will work with some mongo shell commands. If you want to check if the mongo shell working properly or not, type db and it will show the default database named test. This command is also used to check your currently selected database.
Show available Database
To show all available databases, run the following commands
show dbs
or
db.getMongo().getDBNames()
or
db.adminCommand('listDatabases')
Select a DB
To select a particular database to access, use the following command. This command will create a DB if not exists.
use authdb
Show all the collections in selected DB
To show all the collections in the selected database, use the following commands.
show collections
or
show tables
or
db.getCollectionNames()
Show all the functions with the database
To show all the functions that can be used with the selected database, use the following commands.
db.authdb.help();
Drop an existing database
To drop an existing database, use the following command
db.dropDatabase()
Create or insert an item
To create or insert an item in database collection, use the following command.
db.Person.insert({Name: 'John', Age: 26});
or
db.Person.save({Name: 'John', Age: 26});
The difference between insert and save is that if the passed document contains an _id field, if a document already exists with that _id it will be updated instead of being added as new.
You can also use indertOne to insert a single item or insertMany to insert multiple items into the database collection.
Update a whole item or object
To update a whole item or object, you can use the following command.
db.Person.update({Name: 'John'},{Age: 25,Name: 'Smith'});
or
db.Person.updateOne({Name: 'John'},{Age: 25,Name: 'Smith'});
or
db.Person.updateMany({Name: 'John'},{Age: 25,Name: 'Smith'});
In this example, the updateOne will replace only the first matching item and updateMany will replace all the matching items with the given conditions.
Update a single field
To update a single field of an item, use the following command.
db.Person.update({name: 'John'}, {$set: {Age: 25}});
This command will only update the first matching item.
To update multiple items at a time, use the following command.
db.Person.update({name: 'John'}, {$set: {Age: 25}},{multi: true});
This command will update all the matching items.
Delete items
To delete all the items matching a condition, use the following command.
db.Person.remove({name: 'John'});
To remove a single item, use the following command.
db.Person.remove({name: 'John'}, true);
You can also use the following command to remove all the items from a collection just not to pass any argument or pass an empty object.
db.Person.remove({});
or
db.Person.remove();
Find item
To find items from a collection with the Name “John”, use the following command.
db.Person.find({Name: 'John'})
To show the items pretty, use the following command.
db.Person.find({Name: 'John'}).pretty()
Summary
These are the basic mongo shell commands we can use. There also have some advanced commands I will discuss in the next article.
Happy coding!!