DEV Community

Cover image for Basic Query in the MongoDB Shell
Prosen Ghosh
Prosen Ghosh

Posted on • Updated on • Originally published at dev.to

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

$ show dbs
Enter fullscreen mode Exit fullscreen mode

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

$ use mydb
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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})
Enter fullscreen mode Exit fullscreen mode

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"
    }
])
Enter fullscreen mode Exit fullscreen mode

Acknowledgement

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

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

Enter fullscreen mode Exit fullscreen mode

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({})
Enter fullscreen mode Exit fullscreen mode

The above query will return the users data.

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

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()
Enter fullscreen mode Exit fullscreen mode
Result
{
    "_id" : ObjectId("60d832029f3f6108a53af686"),
    "firstName" : "Alex",
    "lastName" : "Balkin"
}
{
    "_id" : ObjectId("60d832029f3f6108a53af687"),
    "firstName" : "Tom",
    "lastName" : "Xu"
}

Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode
Result
{
    "_id" : ObjectId("60d832029f3f6108a53af686"),
    "firstName" : "Alex",
    "lastName" : "Balkin"
}
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode
  • 1 to include a field in the returned documents
  • 0 to exclude a field in the returned documents
Result
{ "firstName" : "Alex" }
Enter fullscreen mode Exit fullscreen mode

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 🎉.

Discussion (0)