0

I have some Mongo data of the form:

{
  "content": "Foo",
  "metadata": {
    "key1": "value",
    "key2": "another value",
    "key3": "and another value"
  }
}

The keys in the metadata subdocument could be named anything (I've called them key1, key2 and key3 above, but they could be called author, title and date for example). There will be an unknown number of them, and the names may also be unknown.

Is there an (aggregation?) query I can run to list all of the fields in the metadata subdocument, across all of the documents in my collection?

styvane
  • 59,869
  • 19
  • 150
  • 156
James Baker
  • 1,143
  • 17
  • 39
  • *an unknow number of them*: That is the source of your problems. You should consider to make "metadata" an array of subdocuments. Btw don't use the [`mongodb-aggregation`](https://meta.stackoverflow.com/questions/337096/let-burninate-mongodb-aggregation) tag. – styvane Nov 08 '16 at 15:22
  • The MapReduce approach seems to require creating a new (temporary) collection - is there no other way of doing it? The data and hence the fields are likely to be changing frequently, so I'd rather have it returned from a query that can be run than having to keep building new collections. – James Baker Nov 09 '16 at 09:09

0 Answers0