
Here we discuss syntax, parameter, the working of distinct command with examples to implement. If suppose array contains a value as, 2], then distinct methods consider it as ] as a result.īelow example states will consider the array field as each element as separate values.ĭb.stud_array.insert( ).

If the field’s values were an array, then distinct methods consider each element of the field as a separate value. In MongoDB, the distinct aggregation command finds the distinct values for a specified field across a single collection.The unwind is performed to create a unique document for each element in the. When we have used or specifies collation in a distinct method, we need to specify a mandatory locale to define the field with collation in a distinct method. You can do this via aggregation framework in Compass, using unwind and group. Notice that all of the documents are returned since there are no two documents that have identical values for the team, position, and points fields.This will return only distinct data from the collection, if in one collection thousands of documents and we need only those documents which were distinct each other same time we have used distinct in MongoDB.This, as a method of counting, would be highly inefficient in terms of memory and resource and processor. The distinct method in MongoDB uses an index on the field while doing a distinct operation on collection. Instead, you would want to group on distinct values counting the amount of times that value exists, at which point you could easily add a stage to sum it up as the number of unique objects.The query is a handy and important parameter in a distinct method.īelow is the working of distinct command: Query: Query distinct method defines the document from which we have to retrieve the collection’s distinct value.It is available only if the document specifies the options. This is an additional option used in the distinct command. Options: This is an optional parameter in MongoDB distinct command.The field is an essential and useful parameter in a distinct method. Field: Field has specified that return distinct value from the field we have specified with distinct methods. If the query bar displays the Project option, you can specify which fields to return in the resulting data.This is used to retrieve distinct values from the collection. This is very important and useful to retrieve distinct data from a single collection. Distinct: This will return only distinct data from the collection. Syntax: db.collection.distinct (field, query, options) Parameters: The function must be provided with two mandatory parameters, field and query.Mongodb, get distinct values of a field and output the values as single document. We can retrieve distinct values from the collection by using distinct methods. Mongodb find() query : return only unique values (no duplicates) 0. Collection name: Collection name is defined as retrieved documents from the collection by using a distinct method. 6 Answers Sorted by: 30 +200 You can use db.collection.distinct to get back an array of unique values: > db.test.If you are using this method in the sharded cluster, then this method may return orphaned.

It takes three parameters first one is the field for which to return distinct values and the others are optional. Though, it may be possible to generalize this. In MongoDB, the distinct () method finds the distinct values for a given field across a single collection and returns the results in an array. Also, it is tightly coupled to the original schema of the document. PROS: Almost certainly the fastest method.ĬONS: Involves use of the complicated Aggregation API. distinct ( 'countries', query) You can specify a document field within an embedded document. "_id" : ObjectId("55fb59d9241fee91ac4cd884"), Call the distinct () method on a Collection object with a document field name parameter as a String to produce a list that contains one of each of the different values found in the specified document field as shown below: const distinctValues m圜oll.

#MONGODB COMPASS QUERY DISTINCT VALUES CODE#
OriginalId: ĭb.test.aggregate(/* from first code snippet */)

You could do that like this: db.test.aggregate([
