Firebase startAt查询会产生意外结果

时间:2016-09-06 08:38:11

标签: javascript firebase firebase-realtime-database

我执行以下查询:

firebase.database().ref().child('files').child('54a8924f8c417b2a281f4e3a4c639785/').orderByChild('path').startAt('54a8924f8c417b2a281f4e3a4c639785/Test Folder 2/')

我得到以下输出。很奇怪输出还包含我在路径字段中未查询的对象:

[
    {
        "fullPath": "54a8924f8c417b2a281f4e3a4c639785/Test Folder 2/Test Sub Folder/.Test Sub Folder",
        "name": "Test Sub Folder",
        "path": "54a8924f8c417b2a281f4e3a4c639785/Test Folder 2/",
        "timestamp": 1473150053359,
        "type": "dir",
        "uid": "ItvFuIIVriaHPPzeMgggwPqSlmq1",
        "$id": "-KQy_TcuUb3FsQz-CEyY",
        "$priority": null
    },
    {
        "extension": "png",
        "fullPath": "54a8924f8c417b2a281f4e3a4c639785/Test Folder 2/download.png",
        "mimeType": "image/png",
        "name": "download.png",
        "path": "54a8924f8c417b2a281f4e3a4c639785/Test Folder 2/",
        "size": 124631,
        "timestamp": 1473150091860,
        "type": "image",
        "uid": "ItvFuIIVriaHPPzeMgggwPqSlmq1",
        "$id": "-KQy_c1SLjHjTHCS1Mox",
        "$priority": null
    },
    {

        "extension": "jpg",
        "fullPath": "54a8924f8c417b2a281f4e3a4c639785/Test Folder 2/SampleImage.jpg",
        "mimeType": "image/jpeg",
        "name": "SampleImage.jpg",
        "path": "54a8924f8c417b2a281f4e3a4c639785/Test Folder 2/",
        "size": 62678,
        "timestamp": 1473150091920,
        "type": "image",
        "uid": "ItvFuIIVriaHPPzeMgggwPqSlmq1",
        "$id": "-KQy_c1WiRELh2GiZoFm",
        "$priority": null
    },
    {

        "extension": "jpg",
        "fullPath": "54a8924f8c417b2a281f4e3a4c639785/Test Folder 2/goodtimes.jpg",
        "mimeType": "image/jpeg",
        "name": "goodtimes.jpg",
        "path": "54a8924f8c417b2a281f4e3a4c639785/Test Folder 2/",
        "size": 128982,
        "timestamp": 1473150091920,
        "type": "image",
        "uid": "ItvFuIIVriaHPPzeMgggwPqSlmq1",
        "$id": "-KQy_c1_fNhcg3RKXkTj",
        "$priority": null
    },
    {

        "extension": "jpg",
        "fullPath": "54a8924f8c417b2a281f4e3a4c639785/Test Folder 2/Test Sub Folder/antibiotics2515.jpg",
        "mimeType": "image/jpeg",
        "name": "antibiotics2515.jpg",
        "path": "54a8924f8c417b2a281f4e3a4c639785/Test Folder 2/Test Sub Folder/",
        "size": 430885,
        "timestamp": 1473150091921,
        "type": "image",
        "uid": "ItvFuIIVriaHPPzeMgggwPqSlmq1",
        "$id": "-KQy_c1bzYINWTYiyFkC",
        "$priority": null
    },
    {

        "extension": "png",
        "fullPath": "54a8924f8c417b2a281f4e3a4c639785/Test Folder/download.png",
        "mimeType": "image/png",
        "name": "download.png",
        "path": "54a8924f8c417b2a281f4e3a4c639785/Test Folder/",
        "size": 124631,
        "timestamp": 1473149727313,
        "type": "image",
        "uid": "ItvFuIIVriaHPPzeMgggwPqSlmq1",
        "$id": "-KQyZDwJIbSExhaln5l-",
        "$priority": null
    },
    {

        "extension": "jpg",
        "fullPath": "54a8924f8c417b2a281f4e3a4c639785/Test Folder/SampleImage.jpg",
        "mimeType": "image/jpeg",
        "name": "SampleImage.jpg",
        "path": "54a8924f8c417b2a281f4e3a4c639785/Test Folder/",
        "size": 62678,
        "timestamp": 1473149727363,
        "type": "image",
        "uid": "ItvFuIIVriaHPPzeMgggwPqSlmq1",
        "$id": "-KQyZDwLBEIBKDx6Glq2",
        "$priority": null
    },
    {

        "extension": "jpg",
        "fullPath": "54a8924f8c417b2a281f4e3a4c639785/Test Folder/goodtimes.jpg",
        "mimeType": "image/jpeg",
        "name": "goodtimes.jpg",
        "path": "54a8924f8c417b2a281f4e3a4c639785/Test Folder/",
        "size": 128982,
        "timestamp": 1473149728493,
        "type": "image",
        "uid": "ItvFuIIVriaHPPzeMgggwPqSlmq1",
        "$id": "-KQyZDwNTejmqwN6X7ZR",
        "$priority": null
    },
    {

        "name": "Test Sub Folder",
        "path": "54a8924f8c417b2a281f4e3a4c639785/Test Folder/",
        "timestamp": 1473149760565,
        "type": "dir",
        "uid": "ItvFuIIVriaHPPzeMgggwPqSlmq1",
        "$id": "-KQyZLKsDkxlTJHDDwDX",
        "$priority": null
    },
    {

        "extension": "jpg",
        "fullPath": "54a8924f8c417b2a281f4e3a4c639785/Test Folder/Test Sub Folder/antibiotics2515.jpg",
        "mimeType": "image/jpeg",
        "name": "antibiotics2515.jpg",
        "path": "54a8924f8c417b2a281f4e3a4c639785/Test Folder/Test Sub Folder/",
        "size": 430885,
        "timestamp": 1473149889432,
        "type": "image",
        "uid": "ItvFuIIVriaHPPzeMgggwPqSlmq1",
        "$id": "-KQyZqbRe3V1BBWImBxq",
        "$priority": null
    }
]

输出还包括path为" 54a8924f8c417b2a281f4e3a4c639785/Test Folder/"但我查询path以" 54a8924f8c417b2a281f4e3a4c639785/Test Folder 2/"

开头

1 个答案:

答案 0 :(得分:3)

Firebase中的查询在排序/过滤字符串时使用字典顺序。因此,这意味着Test Folder/实际上大于Test Folder 2/,因为/大于(space)

您需要做的是使用endAt子句限制查询。只要前缀相同,使用高Unicode代码点就应该允许任何文本。

.orderByChild('path')
.startAt('54a8924f8c417b2a281f4e3a4c639785/Test Folder 2/')
.endAt('54a8924f8c417b2a281f4e3a4c639785/Test Folder 2/\uf8ff')

旧的Firebase文档在范围查询下为here提供了一个很好的示例,但它似乎在转换中消失了。