3
root: {
  dir: {
    subDir: {
      subSubDir: {
        ...
      },
      ...
    },
    subDir_2: {
      ...
    }
  },
  dir_2: {
    ...
  },
  ...
}

How do i retrieve only a single level of keys? E.g. i want to retrieve only keys under dir. Results should contain [subDir, subDir_2, ...] and nothing else: no values, no nested keys etc. What is the most performant way to do that?

stkvtflw
  • 12,092
  • 26
  • 78
  • 155
  • [check this](http://stackoverflow.com/questions/2298870/mongodb-get-names-of-all-keys-in-collection) – Neo-coder Dec 06 '16 at 14:44
  • Do you know structure of each doc or the key that you are searching for be can be on any depth? – Nenad Vracar Dec 06 '16 at 17:11
  • @NenadVracar at each particular moment i will know where exactly i want to search. I'll know exact node and path till this node. All i'll need is keys in it but not deeper. – stkvtflw Dec 06 '16 at 17:15
  • You could retrieve all results that have `root.dir` structure and then use `Object.keys()` and return only keys of `root.dir` (code in https://jsfiddle.net/Lg0wyt9u/1335/) but i think that is not what you want. I think what you are looking for is https://docs.mongodb.com/manual/aggregation/ but i am not sure. – Nenad Vracar Dec 06 '16 at 19:01
  • @NenadVracar, what if i have several magabytes of data in root.dir? what if more? data in there will outweigh size of keys by thousands, maybe millions times. – stkvtflw Dec 06 '16 at 19:38

1 Answers1

0

I think the only way to achieve your goal is to use recursive map-reduce. In this way you can handle the depth you want to reach. You have to check each depth level to see if there is just a simple element, an object or an array. Besides the performance won't be really good because map-reduce are slow to process data since they must convert documents from BSON to JSON and back to work. I used this approach to build a schema analyzer for mongoDB. Here is the link, if you want to take a look.
Maybe you can use map-reduce in a sharded cluster but it's better Hadoop! or Elasticsearch for more efficient performance.

harry-potter
  • 1,981
  • 5
  • 29
  • 55