Mongodb Distinct Projection - Detailed Usage of Distinct

Introduction All the content of this article is related to MongoDB distinct projection. We’ll start with discussing projection and how to use it and then move onto the distinct operator.

MongoDB Projection

In MongoDB, a projection means that only the required fields are selected, not every field in the document. A document can contain many fields but not all fields may be required for a certain task and therefore you should simplify your task and speed up your request by only querying the necessary fields.

In MongoDB projection we only request the data we need from a document. For example if our document contains 100 fields (name, age, address, etc.) and we only want to display one piece of information, such as the value of the age field in all documents with name: "Jack". In such a case, we can use the projection to include only the required fields in the selection.

> db.users.find({name: "Jack"}, {age: 1})

The first parameter in the find operator let’s you filter the documents by certain conditions ( documents where the name is Jack), and the second parameter lets you specify which fields of those documents you want ( We want the age ).

Using the unit as a {age:1} parameter means that the query should only return the contents of the age attribute.

> db.users.find({"name":"Jack"},{"age":1})
{ "_id" : ObjectId("59382d2149f337809f210c67"), "age" : 28 }
{ "_id" : ObjectId("59383945032ed06deaffacda"), "age" : 32 }
>

If we don’t want to return the age field we’d use the age: 0:

> db.persons.find({name: "Jack"}, {age: 0})

It should be noted that the id field is included in the result example. So if you don’t want to display this field in the sample, you must explicitly specify: {“ id”: 0}

Alternatively, you can use true and false instead of 1 and 0:

> db.users.find({name: "Jack"}, {age: true, _id: false})

If we want to display all the documents we can leave the first bracket blank such as:

> db.users.find({}, {age: 1, _id: 0})

Find() method

The MongoDB’s find () method, the second optional parameter that this method receives in the MongoDB request document is a list of fields to retrieve. When the find () method is executed in MongoDB, all fields of the document are displayed by default. To restrict the displayed fields, the value corresponding to the list of fields must be set to 1 or 0 ( true or false ). 1 (true) is used to display the field and 0 (false) to hide the field.

The basic syntax of the find () method with the projection is as follows:

>db.COLLECTION_NAME.find({},{KEY:1})

Suppose the collection kbobjectrocket has the following data:

> db.kbobjectrocket.find({}, {'_id':1, 'title':1})
{ "_id" : 43, "title" : "MongoDB Distinct Projection" }
{ "_id" : 44, "title" : "NoSQL Database" }
{ "_id" : 45, "title" : "Python Tutorial" }
{ "_id" : 46, "title" : "MongoDB collection" }

We can display only the title in the results of the query using this:

> db.mycol.find({}, {'title':1,'_id':0})
{ "title" : "MongoDB Distinct Projection" }
{ "title" : "NoSQL Database" }
{ "title" : "Python Tutorial" }
{ "title" : "MongoDB collection" }

This following query gets all the documents with the title, by, and url fields:

> db.mycol.find({}, {'title':1,'by':1, 'url':1})
{ "_id" : 43, "title" : "MongoDB", "by" : "objectrocket", "url" : "https://kb.objectrocket.com/category/mongo-db" }
{ "_id" : 60, "title" : "Elasticresearch", "by" : "objectrocket", "url" : "https://kb.objectrocket.com/category/elasticsearch" }
{ "_id" : 34, "title" : "CockroachDB", "by" : "objectrocket", "url" : "https://kb.objectrocket.com/category/cockroach" }
{ "_id" : 12, "title" : "Redis", "by" : "objectrocket", "url" : "https://kb.objectrocket.com/category/redis" }

Note that the _id field is always displayed by default when the find () method is executed. If you do not need this field, you must set explicitly set _id: 0 or _id: false.

Read Also, How to find the number of distinct values with cardinality aggregations (https://kb.objectrocket.com/elasticsearch/how-to-find-the-number-of-distinct-values-with-cardinality-aggregations-in-elasticsearch-using-nodejs)

Detailed Usage of distinct in MongoDB

Role: Gets the unique values for the specified field in the collection and returns it as an array.

Syntax: db.collection_name.distinct (query, field, option) query: conditional query (document) field: specifies the field to be returned (string) * option: other options (document)

Let’s took at another way to accomplish the same thing: { distinct: ““, key: ““, query: }

  • Distinct: the collection to perform distinct
  • Key: the key to execute distinct
  • Query(optional): filter condition

Example: Get the distinct age values for all students:

> db.runCommand({"distinct":"school.students","key":"age"})
  {
     "values" : [
             20,
             21,
             22,
             23,
             24
        ],
       "stats" : {
        "n" : 10,
          "nscanned" : 10,
          "nscannedObjects" : 0,
          "timems" : 0,
          "cursor" : "BtreeCursor age_1"
     },
     "ok" : 1
 }

Simple way to use distinct to remove unnecessary fields in mongodb

The distinct MongoDB’s command lists all the different values for a given field. This command applies to regular fields, array fields, and array inline documents.

çdb.users.distinct('last_name'

Equivalent to the SQL statement:

select DISTINCT last_name from users

A simple example:

> db.addresses.insert({"zip-code": 10010})
> db.addresses.insert({"zip-code": 10010})
> db.addresses.insert({"zip-code": 99701})
> // shell helper:
> db.addresses.distinct("zip-code");
[ 10010, 99701 ]
> // running as a command manually:
> db.runCommand( { distinct: 'addresses', key: 'zip-code' } )
{ "values" : [ 10010, 99701 ], "ok"
//
> db.comments.save({"user": {"points": 25}})
> db.comments.save({"user": {"points": 31}})
> db.comments.save({"user": {"points": 25}})
> db.comments.distinct("user.points");
[ 25, 31 ]

Mongodb Distinct Projection

With grouping you also have the ability to count sets of data. Also, we can count the number of groups by linking the $ group commands.

Let’s suppose, we have a group of client records and these records consist of duplicate emails. We can filter by email addresses to know who is most likely to use our service. If you want to know how many people have used my website, you can count groups and get the number of different emails.

To count the number of groups we usually use $group.

How to count everything in a collection?

Simply count the number of documents in a collection that meet certain criteria.

Usage:

db.collection.count()

or

db.collection.find().count()

Parameter description: This is the target condition for the query. If you want to limit the maximum number of documents found, or if you want to skip the specified number of documents after counting, you need to use limit, skip.

Example:

db.collection.find().limit();db.collection.find().skip();

Conclusion

We have showed a few examples of how to find the distinct values of a field and how to use it with projection to limit the results to the fields you need. We hope you found these code examples helpful and you can apply them to your situation.

If you need someone to manage your MongoDB database and handle all the complexities and problems that come with keeping a production setup running smoothly please don’t hesitate to reach out to us at Object Rocket.

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.