
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
MongoDB query to get distinct FirstName values from documents
For distinct values, use distinct(). Let us create a collection with documents −
> db.demo303.insertOne({FirstName:"Chris",LastName:"Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ea0f6f8647eb59e56202f") } > db.demo303.insertOne({FirstName:"John",LastName:"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ea104f8647eb59e562030") } > db.demo303.insertOne({FirstName:"Chris",LastName:"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ea10df8647eb59e562031") } > db.demo303.insertOne({FirstName:"John",LastName:"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ea121f8647eb59e562032") } > db.demo303.insertOne({FirstName:"David",LastName:"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ea136f8647eb59e562033") }
Display all documents from a collection with the help of find() method −
> db.demo303.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e4ea0f6f8647eb59e56202f"), "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : ObjectId("5e4ea104f8647eb59e562030"), "FirstName" : "John", "LastName" : "Doe" } { "_id" : ObjectId("5e4ea10df8647eb59e562031"), "FirstName" : "Chris", "LastName" : "Smith" } { "_id" : ObjectId("5e4ea121f8647eb59e562032"), "FirstName" : "John", "LastName" : "Smith" } { "_id" : ObjectId("5e4ea136f8647eb59e562033"), "FirstName" : "David", "LastName" : "Miller" }
Following is the query to get the distinct FirstName values −
> db.demo303.distinct("FirstName");
This will produce the following output −
[ "Chris", "John", "David" ]
- Related Articles
- MongoDB query to get only distinct values
- MongoDB query to find documents with specific FirstName and LastName
- MongoDB query for counting the distinct values across all documents?
- Get distinct values from a column in MongoDB?
- Get distinct record values in MongoDB?
- MongoDB query to get minimum and maximum value from documents including some duplicate records
- MongoDB query to skip documents
- Get the maximum mark records from a collection with documents in MongoDB query
- MongoDB query to find matching documents given an array with values?
- MongoDB query to get only specific fields in nested array documents?
- MongoDB query to get documents with multiple conditions set in $or?
- Get Distinct Values with Sorted Data in MongoDB?
- MongoDB - Query embedded documents?
- How to get max values for distinct elements in MongoDB
- MongoDB query to find on field combination of FirstName and LastName?

Advertisements