Couchbase查询无法按预期工作

时间:2016-07-17 03:50:49

标签: java sql couchbase n1ql

使用Couchbase 4.1我有以下文档结构

"bucketName": {
                "UserId": "user1",
                "skillSet": {
                    "punchPower": 1,
                    "kickPower": 1,
                    "specialPower": "Extreme programmer"
                },
                "id": "GameUser_XXX1D",
                "timestamp": 1464935606358
            }

我想要一个查询,它会返回最新的时间戳和所有匹配的总数。这就是我所拥有的。

select count(timestamp) as users, timestamp from bucketName where id like 'GameUser__%' AND 
skillSet.specialPower = 'Extreme Programmer' AND 
skillSet.kickPower = 1 AND 
skillSet.punchPower = 1 AND 
timestamp BETWEEN $sometimeIPassHere AND $sometimeIPassHere 
ORDER BY timestamp desc limit 1;

这会返回正常数量的用户,但是返回的时间戳不正确,返回的是最早的时间戳,就好像我的查询中没有考虑desc一样。我尝试删除,并应用asc也返回了相同的结果。

如何解决此错误查询结果

1 个答案:

答案 0 :(得分:2)

我能够通过应用max()方法来实现这一点。

select max(timestamp), count(timestamp) as users, timestamp from bucketName where id like 'GameUser__%' AND 
skillSet.specialPower = 'Extreme Programmer' AND 
skillSet.kickPower = 1 AND 
skillSet.punchPower = 1 AND 
timestamp BETWEEN $sometimeIPassHere AND $sometimeIPassHere 
ORDER BY timestamp desc limit 1;