List all values of a certain field in mongodb List all values of a certain field in mongodb database database

List all values of a certain field in mongodb


db.collection.distinct('x')

should give you an array of unique values for that field.


This would return an array of docs, containing just it's x value...

db.collection.find(    { },    { x: 1, y: 0, _id:0 })


Notice: My answer is a fork from the original answer.

Before any "thumbs up" here, "thumbs up" the accepted answer :).


db.collection.distinct("NameOfTheField")

Finds the distinct values for a specified field across a single collection or view and returns the results in an array.

Reference: https://docs.mongodb.com/manual/reference/method/db.collection.distinct/