问题描述
我目前有一个名为beacon_logs的文档,每次我进入信标范围时都会记录日志。数据类似于以下内容:
I currently have a document called beacon_logs, and it logs every time I walk into range of a beacon. The data looks similar to this:
{
"_id": "00198cd8f0fc510dbad06bf24e93f55b",
"_rev": "1-e90f025935847b0412923e4ba472cf2a",
"device": "gwen",
"beaconUUID": "123",
"distance": "0.0",
"timestamp": 1487443924
},
{
"_id": "00198cd8f0fc510dbad06bf24e93f55c",
"_rev": "1-e90f025935847b0412923e4ba472cf2a",
"device": "gwen",
"beaconUUID": "123",
"distance": "0.1",
"timestamp": 1487443925
},
{
"_id": "01ab15fd3a1c7c37ba147be8c56fe389",
"_rev": "1-587035fb7a71962c21f91b86aca56a77",
"device": "gwen",
"beaconUUID": "456",
"distance": "0.87",
"timestamp": 1487031602
},
{
"_id": "01ab15fd3a1c7c37ba147be8c56fe388",
"_rev": "1-587035fb7a71962c21f91b86aca56a77",
"device": "gwen",
"beaconUUID": "456",
"distance": "0.87",
"timestamp": 1487031603
}
然后查看以下视图:
function (doc) {
emit([doc.beaconUUID,doc.timestamp], doc);
}
我想要得到的只是获得所有特定的beaconuuid(即123 ),并按时间戳进行排序。这是我写的查询:
What I want is to get only get all of a certain beaconuuid (i.e.123) and have it also sort by timestamp. This is the query I wrote:
*DB_NAME*/_design/*DDOC_NAME*/_view/*VIEW_NAME*?descending=false&startkey=["123",999999999]&endkey=["123",0]
对于这个冗长的解释,我的问题是:给定键是一个数组,是否有任何查询数组值1的方法,例如
My question to this very long winded explanation is: given that the key is an array, is there any way to query against 1 of the array's value, e.g.
*DB_NAME*/_design/*DDOC_NAME*/_view/*VIEW_NAME*?descending=false&key[0]="123"
,如果没有,任何人都可以推荐解决方法?
and if not, would anyone be able to recommend a work around?
推荐答案
使用此视图
function (doc) {
if (doc.beaconUUID) {
emit([doc.beaconUUID,doc.timestamp], doc);
}
}
我能够通过这些查询获得正确的响应:
i was able to get the correct responses with these queries:
按时间顺序排列的时间戳顺序:
* DB_NAME * / _ design / * DDOC_NAME * / _ view / * VIEW_NAME *?startkey = [ 123,0]& endkey = [ 123,{}]
chronological timestamp order:*DB_NAME*/_design/*DDOC_NAME*/_view/*VIEW_NAME*?startkey=["123",0]&endkey=["123",{}]
反向时间时间戳顺序:
* DB_NAME * / _ design / * DDOC_NAME * / _ view / * VIEW_NAME *?startkey = [ 123,{}]& endkey = [ 123,0]&降序= true
reverse chronological timestamp order:*DB_NAME*/_design/*DDOC_NAME*/_view/*VIEW_NAME*?startkey=["123",{}]&endkey=["123",0]&descending=true
这篇关于在ouchDB视图中查询数组键的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!