Basic Query in the MongoDB Shell

After installing mongoDB on your local machine run the below command, it will connect the local mongoDB shell.

$ mongo

Within the shell the below command represents the current selected DB. Type the below command in your shell to display the current selected DB

$ db

If we want to show all the available DB in your database server type the below command.

$ show dbs

If we want to switch between databases, then run the below command it will switch the database.

$ use mydb

Switching database doesn't create the database, it will create the DB when first collection or document will be created.

If you want to create a collection explicitly, then run the below command.

$ db.createCollection()

The create collection method take two parameters. The first one is the collection name and the second one is for the specific options of the collection. (like: Schema validator)

$ db.createCollection( <name>, {...options})

Lets create a collection implicitly by inserting some user data. See the below command.

$ db.users.insertMany([
    {
        firstName: "Alex",
        lastName: "Balkin"
    },
    {
        firstName: "Tom",
        lastName: "Xu"
    }
])

Acknowledgement

The operation will return some auto generated unique IDs for the insertions.

{
    "acknowledged" : true,
    "insertedIds" : [
        ObjectId("60d832029f3f6108a53af686"),
        ObjectId("60d832029f3f6108a53af687")
    ]
}

The insertMany([{...}, ...]) operation will create both the database mydb and users collection if they are not already exist.

To verify the insertion we can query the collection.

$ db.users.find({})

The above query will return the users data.

{ "_id" : ObjectId("60d832029f3f6108a53af686"), "firstName" : "Alex", "lastName" : "Balkin" }
{ "_id" : ObjectId("60d832029f3f6108a53af687"), "firstName" : "Tom", "lastName" : "Xu" }

We can see that the query result are shown in the shell, but the result is not formatted.

We can also format the result for better view.

$ db.users.find({}).pretty()
Result
{
    "_id" : ObjectId("60d832029f3f6108a53af686"),
    "firstName" : "Alex",
    "lastName" : "Balkin"
}
{
    "_id" : ObjectId("60d832029f3f6108a53af687"),
    "firstName" : "Tom",
    "lastName" : "Xu"
}

In the above query we can see we pass an empty object to the find query method that's because we want all our users data as a result.

If we want to get a specific user data then we have to send a query filter instead of empty query filter. See the below query.

$ db.users.find({
    _id: ObjectId("60d832029f3f6108a53af686")
}).pretty()
Result
{
    "_id" : ObjectId("60d832029f3f6108a53af686"),
    "firstName" : "Alex",
    "lastName" : "Balkin"
}

If we want to select some specific filed from our document we can also do that by passing a projection document as a second parameter of the find query. Let's see and example.

$ db.users.find({
    _id: ObjectId("60d832029f3f6108a53af686")
}, {firstName: 1, _id: 0 }).pretty()
  • 1 to include a field in the returned documents
  • 0 to exclude a field in the returned documents
Result
{ "firstName" : "Alex" }

If we want to exclude the _id filed then we have to specify it in the projection document the default behavior of the _id field does not exclude from the query projection.

We are done today, we will continue tomorrow. Happy Learning 🎉.

25