3

I am trying to figure out how to match a key and return all the values for that key. Is it possible to give the value as a wildcard? I want to return everything for that specific key using wildcard on the value.

db.collection.find({"key" :"*"})

Also I was hoping this would return the entire collection as well that had the key with the wildcard value match as well.

Max Powers
  • 1,119
  • 4
  • 23
  • 54

2 Answers2

12

You may be looking for something like this:

db.collection.find({"key": {$exists: true}})

This will return all documents in the collection where the key "key" is present. The entire document is returned.

Jeff J
  • 553
  • 2
  • 9
0

Yes:

db.collection.distinct("key")
A. Jesse Jiryu Davis
  • 23,641
  • 4
  • 57
  • 70
  • Thanks, I should have been more clear in my initially posting for this question. I was hoping to return the entire collection not just the collection with this value. – Max Powers Jun 18 '17 at 15:28
  • Sorry I don't understand your question. To get all documents in a collection, do `db.collection.find()`. Is that what you want? – A. Jesse Jiryu Davis Jun 18 '17 at 21:23