MongoDB Basic Shell Commands (part-1)

MongoDB Basic Shell Commands (part-1)

Importing Data

When you are handling data in JSON format

mongoimport --uri="mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_db" --drop sample.json

Here the --drop flag is used to wipe out already existing data, the database is sample_db and the data file that you are importing is sample.json

When you are handling data in BSON format

mongorestore --uri "mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_db"  --drop dump

Here the BSON data is in the dump file

Exporting Data

When you are handling data in JSON format

mongoexport --uri="mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_db" --collection=sample --out=sample.json

Here the database is sample_db and the collection name is sample, the data is exported as sample.json file as output

When you are handling data in BSON format

mongodump --uri "mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_db"

Here the output will be a dump BSON file

Querying Data

  • Queries must use valid JSON.
  • Returned Documents will contain the requested field:value pairs in them.
  • Connect to the remote cluster using the following command on your shell
    mongo "mongodb+srv://<username>:<password>@<cluster>.mongodb.net/admin"
    
    The above command will open a mongo shell, where you can type mongo shell commands.
    show dbs
    
    This command will show you all the databases in that cluster while you are in mongo shell
    use sample_db
    
    This command will switch you to the sample_db database. Now you can perform actions on this database
    show collections
    
    This command will show you all the collections in the sample_db database that you previously switched to. Let's say there is a collection of users.

    find()

    db.users.find()
    
    Here the db object is pointing to the sample_db database. This query will return all the documents in the users collection. But one cursor at a time, you have to iterate through the returned result by typing it on the shell to have a look at all the documents, if there are many documents.

If you want to filter data based on a query, you have to pass the query as a JSON object inside find like this find({field: value}). Suppose you have a field: value like state: state name in the collection's documents. And you want to filter the documents which have the state NY. You have to create a query like the following...

db.users.find({"state": "NY"})

This above command will return all the documents in the collection which has the state NY.

If you just want to count the number of all the matched documents, you can use the following command. Here count is a cursor method which means it applies to the returned result object.

db.users.find({"state": "NY"}).count()

You can narrow down your search by passing more restrictions like the following.

db.users.find({"state": "NY", "city": "ALBANY"})

All the above find commands will return JSON in not the very readable way, so you can use pretty() cursor method to get a nice formatted more readable JSON response, like the following.

db.users.find({"state": "NY", "city": "ALBANY"}).pretty()

findOne()

You can get a random document from the collection using the findOne() method, this will return a beautifully formatted random document. This is very useful to get a glimpse of the document structure in a collection.

db.users.findOne()

MongoDB Basic Shell Commands (part-2)

Thanks for reading, any correction or recommendation is welcome.