
- 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
List all values of a certain field in MongoDB?
To get the list of all values of certain fields in MongoDB, you can use distinct(). The syntax is as follows −
db.yourCollectionName.distinct( "yourFieldName");
To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[10,20,30]}); { "acknowledged" : true, "insertedId" : ObjectId("5c8fc89ed3c9d04998abf011") } > db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[40,50,60]}); { "acknowledged" : true, "insertedId" : ObjectId("5c8fc8abd3c9d04998abf012") } > db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[10,20,30]}); { "acknowledged" : true, "insertedId" : ObjectId("5c8fc8d7d3c9d04998abf013") } > db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[40,50,70]}); { "acknowledged" : true, "insertedId" : ObjectId("5c8fc8e2d3c9d04998abf014") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.listAllValuesOfCeratinFieldsDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8fc89ed3c9d04998abf011"), "ListOfValues" : [ 10, 20, 30 ] } { "_id" : ObjectId("5c8fc8abd3c9d04998abf012"), "ListOfValues" : [ 40, 50, 60 ] } { "_id" : ObjectId("5c8fc8d7d3c9d04998abf013"), "ListOfValues" : [ 10, 20, 30 ] } { "_id" : ObjectId("5c8fc8e2d3c9d04998abf014"), "ListOfValues" : [ 40, 50, 70 ] }
Here is the query to get the list all values of a certain field in MongoDB. We are displaying the record of the field ‘ListOfValues’ −
> db.listAllValuesOfCeratinFieldsDemo.distinct( "ListOfValues");
The following is the output −
[ 10, 20, 30, 40, 50, 60, 70 ]
- Related Articles
- Find all the non-distinct values of a field in MongoDB?
- Update all the values of a field with a specific string in MongoDB?
- How to get distinct list of sub-document field values in MongoDB?
- MongoDB Aggregate sum of values in a list of dictionaries for all documents?
- Check duplicates of certain field for inner array in MongoDB
- Get the duplicate values of a field in MongoDB?
- Limit number of values in a field using MongoDB?
- Limit number of values in a field with MongoDB?
- Find items that do not have a certain field in MongoDB?
- Fetch specific field values in MongoDB
- How to compare field values in MongoDB?
- MongoDB query to limit the returning values of a field?
- Find values group by another field in MongoDB?
- Set all values in a MySQL field to 0?
- How to query on list field in MongoDB?

Advertisements