开发者

MongoDB MapReduce Stats Engine

开发者 https://www.devze.com 2023-03-05 00:54 出处:网络
I\'m trying to develop a generic reporting engine for a MongoDB system which will aggregate information from a set of documents.I won\'t know the structure of the documents in advance of the query run

I'm trying to develop a generic reporting engine for a MongoDB system which will aggregate information from a set of documents. I won't know the structure of the documents in advance of the query running.

e.g. One set of documents might look like this:

{"drink": "Tea","accompaniments": ["Biscuits","Crisps"]}
{"drink": "Tea","accompaniments": ["Biscuits"]}
{"drink": "Coffee","accompaniments": ["Biscuits","Crisps"]}
{"drink": "Coffee","accompaniments": ["Nuts","Biscuits"]}
{"drink": "Beer","accompaniments": ["Nuts","Crisps"]}
{"drink": "Beer","accompaniments": ["Nuts"]}
{"drink": "Beer","accompaniments": ["Crisps"]}

Here, I need results such as:

{drink:{Tea:2,Coffee:2,Beer:3},accompaniments:{biscuits:4,Crisps:4,Nuts:3}}

Another document set might look like this:

{"favcolor": "Red","shapes": ["Square","Circle"]}
{"favcolor": "Red","shapes": ["Square"]}
{"favcolor": "Green","shapes": ["Circle"]}

There may be any number of different document formats.

So, I can't 'hard-code' the map-reduce functions with named keys. Also, notice that I need to grab 开发者_开发百科info from nested arrays and/or documents - I am in control of how the documents are built, so, to use one example, I could make the accompaniments array a proper set of nested documents if need be (in this example they're built from multiple-choice check-boxes).

What would be the most efficient map-reduce function to grab all the stats from each set of documents? I'm really hoping that there will be 'one' map-reduce that will be able to grab the data from each set. Thanks very much.


I'm not sure about my concept, it's just an idea.

Your two example datasets have common structure. Both contain documents with to properties: one has string value, second is an array. So your map-reduce should iterate over document properties and check the type of particular property. Then agregate according to type and property name.

0

精彩评论

暂无评论...
验证码 换一张
取 消