开发者

No Idea how to create a specific MapReduce in CouchDB

开发者 https://www.devze.com 2023-02-20 14:56 出处:网络
I\'ve got 3 types of documents in my db: { param: \"a\", timestamp: \"t\" } (Type 1) { param: \"b\", partof: \"a\"

I've got 3 types of documents in my db:

{
param: "a",
timestamp: "t"
} (Type 1)

{
param: "b",
partof: "a"
} (Type 2)

{
param: "b",
timestamp: "x"
} (Type 3)

(I can't alter the layout...;-( )

Type 1 defines a start timestamp, it's like the start event. A Type 1 is connected to several T开发者_开发问答ype 3 docs by Type 2 documents.

I want to get the latest Type 3 (highest timestamp) and the corresponding type 1 document.

How may I organize my Map/Reduce?


Easy. For highly relational data, use a relational database.


As user jhs stated before me, your data is relational, and if you can't change it, then you might want to reconsider using CouchDB.

By relational we mean that each "type 1" or "type 3" document in your data "knows" only about itself, and "type 2" documents hold the knowledge about the relation between documents of the other types. With CouchDB, you can only index by fields in the documents themselves, and going one level deeper when querying using includedocs=true. Thus, what you asked for cannot be achieved with a single CouchDB query, because some of the desired data is two levels away from the requested document.

Here is a two-query solution:

{
    "views": {
        "param-by-timestamp": {
            "map": "function(doc) { if (doc.timestamp) emit(doc.timestamp, [doc.timestamp, doc.param]); }",
            "reduce": "function(keys, values) { return values.reduce(function(p, c) { return c[0] > p[0] ? c : p }) }"
        },      
        "partof-by-param": {
            "map": "function(doc) { if (doc.partof) emit(doc.param, doc.partof); }"
        }       
    }   
}

You query it first with param-by-timestamp?reduce=true to get the latest timestamp in value[0] and its corresponding param in value[1], and then query again with partof-by-param?key="<what you got in previous query>". If you need to fetch the full documents together with the timestamp and param, then you will have to play with includedocs=true and provide with the correct _doc values.

0

精彩评论

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