简体   繁体   English

CouchDB视图中的多个范围键

[英]Multiple range keys in couchdb views

I've been searching for a solution since few hours without success... 几个小时以来一直在寻找解决方案,但没有成功...

I just want to do this request in couchdb with a view: 我只想在带有视图的沙发床中执行此请求:

select * from database where (id >= 3000000 AND id <= 3999999) AND gyro_y >= 1000

I tried this: 我尝试了这个:

function(doc) {
    if(doc.id && doc.Gyro_y){
        emit([doc.id,doc.Gyro_y], null);
    }
}

Here is my document (record in couchdb): 这是我的文档(记录在couchdb中):

{
   "_id": "f97968bee9674259c75b89658b09f93c",
   "_rev": "3-4e2cce33e562ae502d6416e0796fcad1",
   "id": "30000002",
   "DateHeure": "2016-06-16T02:08:00Z",
   "Latitude": 1000,
   "Longitude": 1000,
   "Gyro_x": -242,
   "Gyro_y": 183,
   "Gyro_z": -156,
   "Accel_x": -404,
   "Accel_y": -2424,
   "Accel_z": -14588
}

I then do an HTTP request like so: 然后,我像这样执行HTTP请求:

http://localhost:5984/arduino/_design/filter/_view/bygyroy?startkey=["3000000",1000]&endkey=["3999999",9999999]&include_docs=true

I get this as an answer: 我得到这个答案:

{
    total_rows: 10,
    offset: 8,
    rows: [{
    id: "f97968bee9674259c75b89658b09f93c",
    key: [
    "01000002",
    183
    ],
    value: null,
    doc: {
        _id: "f97968bee9674259c75b89658b09f93c",
        _rev: "3-4e2cce33e562ae502d6416e0796fcad1",
        id: "30000002",
        DateHeure: "2016-06-16T02:08:00Z",
        Latitude: 1000,
        Longitude: 1000,
        Gyro_x: -242,
        Gyro_y: 183,
        Gyro_z: -156,
        Accel_x: -404,
        Accel_y: -2424,
        Accel_z: -14588
    }
}
]
}

So it's working for the id but it's not working for the second key gyro_y . 因此,它适用于id,但不适用于第二个键gyro_y

Thanks for your help. 谢谢你的帮助。

When you specify arrays as your start/end keys, the results are filtered in a "cascade". 当您将数组指定为开始/结束键时,结果将以“级联”过滤。 In other words, it moves from left to right, and only if something was matched by the previous key, will it be matched by the next key. 换句话说,它从左到右移动,并且仅当某项与上一个键匹配时,才会与下一个键匹配。

In this case, you'll only find Gyro_y >= 1000 when that document also matches the first condition of 3000000 <= id <= 3999999 . 在这种情况下,当该文档还与第一个条件3000000 <= id <= 3999999匹配时,您只会找到Gyro_y >= 1000

Your SQL example does not translate exactly to what you are doing in CouchDB. 您的SQL示例未完全转换为您在CouchDB中所做的事情。 In SQL, it'll find both conditions and then find the intersection amongst your resulting rows. 在SQL中,它将找到两个条件,然后找到结果行之间的交集。 I would read up on view collation to understand these inner-workings of CouchDB. 我将阅读有关视图整理的文章,以了解CouchDB的这些内部工作原理。

To solve your problem right now, I would simply switch the order you are emitting your keys. 为了立即解决您的问题,我只需要切换发射钥匙的顺序即可。 By putting the Gyro_y value first, you should get the results you've described. 通过将Gyro_y值放在第一位,您应该获得描述的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM