Posted By

lemcoe9 on 10/02/11


php array key foreach column mongodb nosql mongo complex

Versions (?)

List of Keys Used in MongoDB Collection

 / Published in: PHP

Takes the collection 'david' and gives a unique array of the keys used.

  1. $m = new Mongo();
  2. $db = $m->test;
  3. $collection = $db->david;
  4. $cursor = $collection->find();
  5. $array = iterator_to_array($cursor);
  6. $keys = array();
  7. foreach ($array as $k=>$v) {
  8. foreach ($v as $a=>$b) {
  9. $keys[] = $a;
  10. }
  11. }
  12. $keys = array_values(array_unique($keys));

Report this snippet  


RSS Icon Subscribe to comments
Posted By: JamesCropcho on April 29, 2012

Hi lemcoe9, I wanted to you let you know that as this seems like a common problem, I have created an open source to analyze the keys, in a collection, called Variety.

Check it out here: and let me know if you have an issues or any questions.

Good luck!

You need to login to post a comment.