我每分钟存储用户的lat、lng、timestamp
。
我只想读取基于时间戳
的部分数据。我尝试过以下查询,但没有成功。另请检查我的示例数据库的附件。
function queryLocations(){
var ref = firebase.database()
.ref('user-locations/' + currentUID)
.orderByChild('timestamp')
.startAt('1501061958000')
.endAt('1501062154000')
.on("child_added",function(data){
c = c+1;
locationCountElement.textContent = '' + c;
});
firebaseLocRef = ref;
}
因此,我将 startTimestamp
和 endTimestamp
作为输入。我只需要在 startTimestamp
和 endTimestamp
之间包含 timestamp
的行。
我的 firebase 规则如下所示
{
"rules": {
".read": "auth != null",
".write": "auth != null",
"user-locations":{
"$uid": {
".indexOn": "timestamp"
}
}
}
}
最佳答案
您需要startAt()
和endAt()
:
var query = firebase.database()
.ref('user-locations/' + currentUID)
.orderByChild('timestamp')
.startAt(startTimestamp)
.endAt(endTimestamp)
.on("child_added", function(data) {
更新:在更新的代码中,您将时间戳作为字符串传递。但在数据库中它们存储为数字,因此您还必须在查询中使用数字:
.startAt(1501061958000)
.endAt(1501062154000)
关于javascript - Firebase 请求行范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45349392/