CouchDB通过三个索引键进行查询和过滤 [英] CouchDB querying and filtering by three indexed keys

查看:206
本文介绍了CouchDB通过三个索引键进行查询和过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试按具有三个值的键进行排序和排序.但是让我们从文档结构开始:

I'm currently trying to order and sort by a key with three values. But lets start with the document structure:

{
    _id: "DOCIDGOESHERE01",
    type: "MESSAGE",
    date: "2011-08-24 06:49:02",
    author: "USERIDGOESHERE01",
    receiver: ["USERIDGOESHERE02", "USERIDGOESHERE03"],
    message: "ok let's do this"
}

主要目标是查询bedDB以查找选定用户发送给一个特定用户的消息,并按日期对其进行排序.有些邮件没有任何接收者,表明它们是公开的,任何人都可以阅读.

The main goal is to query couchDB for messages send by selected users to one specific user and order them by the date. Some messages don't have any receiver which indicates them to be public and can be read by anyone.

我当前使用的地图功能如下:

The map function i currently use looks like this:

function map(doc) {
    if(doc.receiver.lenth==0)
        emit([doc.date, null, doc.author], doc._id);
    else for(var idx in doc.receiver)
        emit([doc.date, doc.receiver[idx], doc.author], doc._id);
}

在查询bedDB HTTP接口时,我尝试了

When querying the couchDB HTTP interface i tryed requests like

HTTP GET xxx/messages?key=[{}, "USERIDGOESHERE02", {}]

HTTP POST xxx/messages
{
    keys: [
        [{}, "USERIDGOESHERE02", "USERIDGOESHERE01"],
        [{}, "USERIDGOESHERE02", "USERIDGOESHERE03"],
        [{}, "USERIDGOESHERE02", "USERIDGOESHERE04"],
    ]
}

但是所有这些都没有出现在我想要生成的文档列表中.您对此任务有什么建议吗?还是不可能用ouchDB建立这样的过滤结果? 提前非常感谢您!

but all of them didn't result in the list of documents i wanted to produce. Do you have any suggestions with this task? Or is it impossible to build such filtered results with couchDB? Thank you very much in advance!

推荐答案

在长的一维列表中,键始终按最小到最高的顺序排序. (我试图在

Keys are always sorted smallest to highest, in one long 1-dimensional list. (I have tried to describe this intuitively in The Parable of CouchDB but no idea if I succeeded!)

按最小到最大排序的数组是什么样的?如果您读取了视图中的所有键,则左侧值变化最小;中间值的变化大于左边的变化;右边的值变化最大.换句话说,数组键告诉CouchDB:首要任务是按key[0]进行排序,如果相等,则决胜局将为key[1];如果也相等,则下一个决胜局将为key[2],依此类推..."

What does an array sorted smallest-to-largest look like? If you read all the keys in a view, the left-hand value varies the least; the middle value varies more than the left; and the right-hand value varies the most. In other words, array keys tell CouchDB, "First priority is to sort by key[0], if that is equal, the tie-breaker will be key[1]; if those are equal also, the next tiebreaker is key[2], etc..."

因此,您可能希望您的键看起来像这样:

Therefore you probably want your keys to look like this:

[ "receiver_1", null      , a_date       ],
[ "receiver_1", "sender_A", some_date    ],
[ "receiver_1", "sender_B", another_date ],
[ "receiver_2", "sender_A", fourth_date  ],
[ "receiver_3", "sender_C", fifth_date   ],

要查找来自sender_B的接收者_1的所有消息以及公共消息,您需要两个查询,一个查询用于"receiver_1", null配对,另一个查询用于"receiver_1", "sender_B".您想知道任何日期,因此需要与发送者/接收者匹配的行的范围.不幸的是,HTTP POST查询不支持此功能.

To find all messages for receiver_1 from sender_B and also public messages, you need two queries, one for the "receiver_1", null pairings, and another for "receiver_1", "sender_B". You want to know any date, so you need a range of rows that match the sender/receiver. Unfortunately, the HTTP POST query does not support this.

您可以简单地查询每个选定的发件人(甚至使用线程或异步编程同时查询所有发件人).接收方和发送方是已知的,并且此示例允许从最小值(null)到最大值({})的范围,其中将包括所有日期.

You could simply query for each selected sender (even all at the same time using threads or asynchronous programming). The receiver and sender are known, and this example allows a range from the smallest value (null) to the largest ({}), which will include all the dates.

?startkey=["receiver_1",null,null]&endkey=["receiver_1",null,{}]
?startkey=["receiver_1","sender_B",null]&endkey=["receiver_1","sender_B",{}]

另一种选择是简化键并删除日期.

Another option is to simplify your keys and remove the dates.

[ "receiver_1", null      ],
[ "receiver_1", "sender_A"],
[ "receiver_1", "sender_B"],
[ "receiver_2", "sender_A"],
[ "receiver_3", "sender_C"],
[ "receiver_3", "sender_C"],
[ "receiver_3", "sender_C"],
[ "receiver_3", "sender_C"],
[ "receiver_3", "sender_C"],

现在,您可以再次使用HTTP POST API进行查询.邮件将返回按日期排序的 not .这还不错,您可以在客户端上对它们进行排序(或_list函数).记住,即使在我的第一个示例中,日期也不是完美排序的.

Now you can query with the HTTP POST API again. Messages will return not ordered by date. This is not so bad, you can sort them on the client (or a _list function). And remember, even in my first example the dates are not perfectly sorted either.

这篇关于CouchDB通过三个索引键进行查询和过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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