couchdb-startkey endkey不能按预期过滤带有键数组的记录 [英] couchdb - startkey endkey doesn't filter records with key arrays as expected

查看:153
本文介绍了couchdb-startkey endkey不能按预期过滤带有键数组的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Couch-DB map-reduce视图,该视图会输出该视图;

 {
rows: [
    {
        key: [
            "2014-08-20",
            2,
            "registration"
        ],
        value: 2
    },
    {
        key: [
            "2014-08-20",
            2,
            "search"
        ],
        value: 3
    },
    {
        key: [
            "2014-08-21",
            2,
            "registration"
        ],
        value: 3
    },
    {
        key: [
            "2014-08-21",
            2,
            "search"
        ],
        value: 4
    }
]
}

我需要查询2014-08-20和2014-08-21之间的所有记录.同时,我需要中间的整数值为2,最后一个值为"registration". /p>

我的卷曲请求URL如下

BASE_URL?group=true&startkey=["2014-08-20",1,"registration"]
&endkey=["2014-08-21",1,"registration"]

这将滤除日期,但不滤除其他任何元素(整数值和字符串).有人知道发生了什么事吗?

任何帮助将不胜感激.

更新

我的文档结构看起来像这样

[
    {
        platform_version: 2,
        UDID: "EWHSJeNp20sBFuzqcorkKVVi",
        session: {
            timestamp: "2014-08-20T00:00:00.000Z",
            session_id: "kOnNIhCNQ31LlkpEPQ7XnN1D",
            ip: "202.150.213.66",
            location: "1.30324,103.5498"
        },
        events: [
            {
                event_type: "search",
                timestamp: "2014-08-21T00:00:00.000Z",
                location: "1.30354,103.5301",
                attributes: {

                }
            }
        ]
    },
    {
        platform_version: 2,
        UDID: "EWHSJeNp20sBFuzqcorkKVVi",
        session: {
            timestamp: "2014-08-21T00:00:00.000Z",
            session_id: "kOnNIhCNQ31LlkpEPQ7XnN1D",
            ip: "202.150.213.66",
            location: "1.30324,103.5498"
        },
        events: [
            {
                event_type: "search",
                timestamp: "2014-08-21T00:00:00.000Z",
                location: "1.30354,103.5301",
                attributes: {

                }
            }
        ]
    },
    {
        platform_version: 2,
        UDID: "EWHSJeNp20sBFuzqcorkKVVi",
        session: {
            timestamp: "2014-08-20T00:00:00.000Z",
            session_id: "kOnNIhCNQ31LlkpEPQ7XnN1D",
            ip: "202.150.213.66",
            location: "1.30324,103.5498"
        },
        events: [
            {
                event_type: "click",
                timestamp: "2014-08-21T00:00:00.000Z",
                location: "1.30354,103.5301",
                attributes: {

                }
            }
        ]
    }
]

和地图缩小功能看起来像这样.

function(doc) {

date = doc.session.timestamp.split("T")[0];
eventArray = doc.events;
for (i = 0; i < eventArray.length; i++) { 

    emit([doc.app_version,eventArray[i].event_type,date],1);

}


}

更改键的顺序后它开始工作.但仍然不能使用通配符查询所有事件类型.

解决方案

您要获取的第二个数组项与1的第二个数组项不同的文档是因为在CouchDB中,将限制(startkeyendkey)与映射键进行了比较,方法是使用词典顺序.按照字典顺序[1,1] < [1,2] < [2,1] < [2,2].

您需要的是多维查询(CouchDB不支持),其他客户端过滤(这可能会增加CouchDB和您的应用程序之间传输的数据)或使用而不是["2014-08-21", 1, "registration"].

I have Couch-DB map-reduce view which outputs this;

 {
rows: [
    {
        key: [
            "2014-08-20",
            2,
            "registration"
        ],
        value: 2
    },
    {
        key: [
            "2014-08-20",
            2,
            "search"
        ],
        value: 3
    },
    {
        key: [
            "2014-08-21",
            2,
            "registration"
        ],
        value: 3
    },
    {
        key: [
            "2014-08-21",
            2,
            "search"
        ],
        value: 4
    }
]
}

I need to query all the records that has between 2014-08-20 and 2014-08-21 Also the same time I need the integer value in the middle to be 2 and the last value to be "registration".

My curl request URL looks like this

BASE_URL?group=true&startkey=["2014-08-20",1,"registration"]
&endkey=["2014-08-21",1,"registration"]

This filters out the date but none of the other elements (the integer value and the string). Anyone got an idea whats happening?

Any help would be appreciated.

UPDATE

My Document structure looks something like this

[
    {
        platform_version: 2,
        UDID: "EWHSJeNp20sBFuzqcorkKVVi",
        session: {
            timestamp: "2014-08-20T00:00:00.000Z",
            session_id: "kOnNIhCNQ31LlkpEPQ7XnN1D",
            ip: "202.150.213.66",
            location: "1.30324,103.5498"
        },
        events: [
            {
                event_type: "search",
                timestamp: "2014-08-21T00:00:00.000Z",
                location: "1.30354,103.5301",
                attributes: {

                }
            }
        ]
    },
    {
        platform_version: 2,
        UDID: "EWHSJeNp20sBFuzqcorkKVVi",
        session: {
            timestamp: "2014-08-21T00:00:00.000Z",
            session_id: "kOnNIhCNQ31LlkpEPQ7XnN1D",
            ip: "202.150.213.66",
            location: "1.30324,103.5498"
        },
        events: [
            {
                event_type: "search",
                timestamp: "2014-08-21T00:00:00.000Z",
                location: "1.30354,103.5301",
                attributes: {

                }
            }
        ]
    },
    {
        platform_version: 2,
        UDID: "EWHSJeNp20sBFuzqcorkKVVi",
        session: {
            timestamp: "2014-08-20T00:00:00.000Z",
            session_id: "kOnNIhCNQ31LlkpEPQ7XnN1D",
            ip: "202.150.213.66",
            location: "1.30324,103.5498"
        },
        events: [
            {
                event_type: "click",
                timestamp: "2014-08-21T00:00:00.000Z",
                location: "1.30354,103.5301",
                attributes: {

                }
            }
        ]
    }
]

and the map reduce function looks like this.

function(doc) {

date = doc.session.timestamp.split("T")[0];
eventArray = doc.events;
for (i = 0; i < eventArray.length; i++) { 

    emit([doc.app_version,eventArray[i].event_type,date],1);

}


}

It started working after I change the order of the keys. but still, I can't use a wildcard to query all the event types.

解决方案

You are getting documents with different second array item different then 1 because in the CouchDB the limits (startkey and endkey) are compared to map keys using lexicographical order. In the lexicographical order [1,1] < [1,2] < [2,1] < [2,2].

What you need is either multidimensional queries (which are not supported by CouchDB), additional client-side filtering (which may increase data transferred between CouchDB and your app) or additional server-side filtering with list function (which increase processing time of queries).

If your app needs filtering using range only on the first element of key array (just like in your example query), you would solve your problem easily by placing the item at the last position of array, eg. emitting [1, "registration", "2014-08-21"] instead of ["2014-08-21", 1, "registration"].

这篇关于couchdb-startkey endkey不能按预期过滤带有键数组的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆