70

How would I get an array containing all values of a certain field for all of my documents in a collection?

db.collection:

{ "_id" : ObjectId("51a7dc7b2cacf40b79990be6"), "x" : 1 }
{ "_id" : ObjectId("51a7dc7b2cacf40b79990be7"), "x" : 2 }
{ "_id" : ObjectId("51a7dc7b2cacf40b79990be8"), "x" : 3 }
{ "_id" : ObjectId("51a7dc7b2cacf40b79990be9"), "x" : 4 }
{ "_id" : ObjectId("51a7dc7b2cacf40b79990bea"), "x" : 5 }

"db.collection.ListAllValuesForfield(x)" Result: [1,2,3,4,5]

Also, what if this field was an array?

{ "_id" : ObjectId("51a7dc7b2cacf40b79990be6"), "y" : [1,2] }
{ "_id" : ObjectId("51a7dc7b2cacf40b79990be7"), "y" : [3,4] }
{ "_id" : ObjectId("51a7dc7b2cacf40b79990be8"), "y" : [5,6] }
{ "_id" : ObjectId("51a7dc7b2cacf40b79990be9"), "y" : [1,2] }
{ "_id" : ObjectId("51a7dc7b2cacf40b79990bea"), "y" : [3,4] }

"db.collection.ListAllValuesInArrayField(y)" Result: [1,2,3,4,5,6,1,2,3,4]

Additionally, can I make this array unique? [1,2,3,4,5,6]

Community
  • 1
  • 1
chimpsarehungry
  • 1,775
  • 2
  • 17
  • 28

4 Answers4

129

db.collection.distinct('x')

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

John Petrone
  • 26,943
  • 6
  • 63
  • 68
  • 1
    This is just what I needed! What's interesting to note is that there is an additional parameter (the second one) that specifies a document query `db.collection.distinct('users.id', { document: { $in: [...] } }` – pkanev Jan 08 '19 at 09:14
  • 1
    The syntax for nested documents: `db.collection.distinct('x.nestedField')` – remidej Jul 04 '19 at 14:50
  • 1
    The always useful "distinct and count" aggregations / mongo shell extension for it: https://stackoverflow.com/questions/14924495/mongodb-count-num-of-distinct-values-per-field-key/14928718 – chimpsarehungry Aug 05 '19 at 22:02
  • I was looking for 'group by' just like in sql to get this list, distinct() worked very well and so simple! – cem çetin Dec 15 '21 at 07:47
10

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/

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
8

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

db.collection.find(
    { },
    { x: 1, y: 0, _id:0 }
)
martskins
  • 2,920
  • 4
  • 26
  • 52
  • Thanks for this answer, exactly what I needed. – metamaker Jul 05 '17 at 09:03
  • @lascort: This `find` not return "an array", but a cursor... To get an distinct array of `x` values with *this method,* you can do: `A = new Array(); db.collection.find().forEach( D => A.push(D.x) ); U = [... new Set(A)]` – 0zkr PM Oct 09 '18 at 17:53
2

db.collection_name.distinct("key/field_name") - This will return a list of distinct values in the key name from the entire dictionary.

Just make sure you don't use the curly brackets after round brackets.

dilip_nikhil
  • 369
  • 3
  • 6