Sometimes we have to apply the condition using the distinct method to find a particular document. ![]() We have successfully retrieved the distinct value from the collection.Īlso, check: MongoDB Auto Increment ID Distinct query in MongoDB with condition Return all the fields of the fname column.db.students.insertMany([Īfter that, we will apply the below query to return the distinct name (fname) of all the students present in the collection: db.student.distinct("fname") The following documents were inserted into the students’ collection. You will more understand this with the help of an example. In this topic, you will learn to find the distinct values for a given field across a single collection and return the result in an array. Note, you will the aggregation pipeline to retrieve distinct values using the $group operator. Optional, A document that specifies the options. The field name for which you want to return distinct values.Ī query that specifies the documents from which to retrieve the distinct values. The command contains the following fields: parameter ![]() Syntax: db.collection.distinct(field, query, options) The results document also contains an embedded document with query statistics and the query plan. ![]() The distinct method will return a document that contains an array of the distinct values. MongoDB finds the distinct values for a defined field over a single collection. Distinct query in MongoDB multiple fields.Distinct query in MongoDB with condition.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |