

The syntax is as follows db.yourCollectionName.distinct (yourFieldName) To understand the above syntax, let us create a collection with document. distinct returns a document that contains an array of the distinct values. You can use distinct () method in MongoDB to get distinct record values.

I usually ignore null and empty arrays if the field is an array. Definition distinct Finds the distinct values for a specified field across a single collection. Select $unwind and then fill in the field you want, prefixed with $. If you go into Compass to the collection you want and go to the aggregation tab, you can unwind and group as such: I am trying to group my date but can't get mongo to count only the dates once and treat the ones that show up more than once as just one.Here's a way that I've been using in Compass. The db.collection.distinct() method provides a. There are 7 objects with date field but as you can see shows up twice so that means that there are only a total count of 6 days. Finds the distinct values for a specified field across a single collection and returns the results in an array. In other words, distinct values are unique values. The following example returns the distinct values for the field sizes from all documents in the inventory collection: db. It eventually helps us return only unique values for a field we specify in the mongo shell. The distinct () function returns an array of unique values for a specified field across a single collection. I installed only MongoDB Compass and connecting to MongoDB Atlas cluster. The distinct () function returns an array of unique values for a specified field across a single collection. prasad at 2:54 Thanks Prasad for your response. The unwind is performed to create a unique document for each element in the. Also, please post text of a sample document (you can copy from Compass). You can do this via aggregation framework in Compass, using unwind and group. Example: db.pets.distinct ( 'type' ) Result: 'Bat', 'Cat', 'Dog' These three values are the distinct values for the type field in the above documents. Distinct values are those values with redundant duplicates removed. don't have mongo Shell', A MongoDB installation includes Mongo Shell program.

I have this set of objects above, and I am trying to count the amount of unique dates. In MongoDB, the db.collection.distinct () method finds the distinct values for a specified field across a single collection or view and returns the results in an array.
