MongoDB Find and Distinct

Introduction

One of the most important operations in any kind of database is retrieving data. Data can be retrieved all at once or filtered. MongoDB provides the find operation to retrieve data. The data is stored in documents, which in turn are stored in collections. By default, the find operation will retrieve all the documents in a collection. We can also filter data using the query and projection in find operation. Apart from the find operation, there is one other operation that returns an array of all the distinct values of the given field. This operation is known as distinct. In this article, we will discuss the MongoDB find and distinct operations with examples.

find operation

The find operation is the primary operation of retrieving data from a collection. Let see the syntax of the find operation.

db.<collection-name>.find()

Using the find operation this way will result in al the documents present in the collection. Let’s see how it works.

> db.details.find()
{ "_id" : ObjectId("5d6545866e08386d93a75157"), "name" : "John", "age" : 21, "location" : "New York" }
{ "_id" : ObjectId("5d6545916e08386d93a75158"), "name" : "Sam", "age" : 25, "location" : "Texas" }
{ "_id" : ObjectId("5d6545a06e08386d93a75159"), "name" : "Lisa", "age" : 23, "location" : "Chicago" }
{ "_id" : ObjectId("5d6545da6e08386d93a7515a"), "name" : "Ron", "age" : 26, "location" : "Chicago" }
{ "_id" : ObjectId("5d6545e56e08386d93a7515b"), "name" : "Mark", "age" : 29, "location" : "Texas" }
>

In the above command, we use the find operation with the details collection. This resulted in all the documents present in it. But to make it much clearer to read, we can use the pretty operation with the find operation.

> db.details.find().pretty()
{
        "_id" : ObjectId("5d6545866e08386d93a75157"),
        "name" : "John",
        "age" : 21,
        "location" : "New York"
}
{
        "_id" : ObjectId("5d6545916e08386d93a75158"),
        "name" : "Sam",
        "age" : 25,
        "location" : "Texas"
}
{
        "_id" : ObjectId("5d6545a06e08386d93a75159"),
        "name" : "Lisa",
        "age" : 23,
        "location" : "Chicago"
}
{
        "_id" : ObjectId("5d6545da6e08386d93a7515a"),
        "name" : "Ron",
        "age" : 26,
        "location" : "Chicago"
}
{
        "_id" : ObjectId("5d6545e56e08386d93a7515b"),
        "name" : "Mark",
        "age" : 29,
        "location" : "Texas"
}
>

This looks much better now! Now let’s discuss the query and projection part.

Query and Projection

The query is actually a condition according to which the find operation will match the documents. It will only return those documents where the query part is matched. Suppose from our details collection, we want to retrieve documents where the location is Texas. Let’s see how can we do this.

> db.details.find({"location": "Texas"}).pretty()
{
        "_id" : ObjectId("5d6545916e08386d93a75158"),
        "name" : "Sam",
        "age" : 25,
        "location" : "Texas"
}
{
        "_id" : ObjectId("5d6545e56e08386d93a7515b"),
        "name" : "Mark",
        "age" : 29,
        "location" : "Texas"
}
>

Yes! We got all the documents where the location is Texas. Pay attention to the command.

db.details.find({ location: "Texas" }).pretty();

When we used the find operation earlier, we did not pass anything. But when we have to filter the data according to some condition, we have to pass the condition to the find operation enclosed in curly brackets. There are a lot of options for the query. We can filter according to a specific value, or by using operators such as greater than or less than, and many other options are available. But there is a second parameter also for the find operation. This is called projection.

The projection specifies which fields we want to return. By default, every field of a document is returned by the find operation. But we can always specify the fields to be returned by defining projection.

> db.details.find({},{_id:0,name:1}).pretty()
{ "name" : "John" }
{ "name" : "Sam" }
{ "name" : "Lisa" }
{ "name" : "Ron" }
{ "name" : "Mark" }
>

Observe the above command and the result. In the find operation, we passed two parameters – first empty set of curly brackets and second, another set of the curly brackets, this time something written inside. The second part is the projection. The empty part is the query. We are not specifying any query here but it is neccesary to pass the first parameter while using projections. The projection is always the second parameter of the find operation.

{_id:0,name:1}

The above projection will return only the name field in every document. We set the value of the name as 1. This means only the name field will return. But we also set the value of _id as 0. We have to do this because, by default, the find operation will always return the _id field.

Combining query and projection

Let’s see an example of the find operation with a combination of the query and projection.

> db.details.find({location: "Texas"},{_id:0,name:1,age:1}).pretty()
{ "name" : "Sam", "age" : 25 }
{ "name" : "Mark", "age" : 29 }
>

The above find operation returns the names and ages of all the documents where the location is Texas.

distinct operation

The distinct operation is slightly different from the find operation. The distinct returns an array of all the distinct values of the field specified. There can be thousands of documents in a collection. There can be fields common in all the documents and these fields can have similar values. This is where distinct is helpful. Let’s see the syntax of the distinct operation.

db.<collection-name>.distinct(<field-name>)

In the distinct operation, it is neccesary to pass the name of the field or it will result in an error. Let’s understand the distinct operation with the help of an example.

> db.details.distinct("location")
[ "New York", "Texas", "Chicago" ]
>

The above command returns all the distinct locations. We have five documents in the details collection but the array returned contains only three values. This means there were documents with the same location. This is how distinct operation works in MongoDB.

Query

Along with the field name, we can also pass a query with it. It is similar to the query part of the find operation. The distinct will not check where the query does not match. Let’s understand this with the help of an example.

> db.details.distinct("location",{"name": "John"})
[ "New York" ]
>

The above command will only look for the distinct locations where the name is John.

Conclusion

Both the find and distinct operations are very useful when it comes to retrieving data from MongoDB. The find operation returns the data all at once or according to the query and projection. The distinct operation has a special functionality of retrieving unique values of a specified field.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.