Skip to content

Instantly share code, notes, and snippets.

@denzhel
Created February 11, 2021 06:37
Show Gist options
  • Select an option

  • Save denzhel/86ba6e29d916d870f384957f7023e9c1 to your computer and use it in GitHub Desktop.

Select an option

Save denzhel/86ba6e29d916d870f384957f7023e9c1 to your computer and use it in GitHub Desktop.
Return only unique keys in Mongo query

If you want to return only unique keys/fields in mongo query while searching inside a collection:

db.users.distinct("name")

If you want to know how many entries were returned:

db.users.distinct("name").length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment