Samar Rizvi Samar Rizvi - 3 months ago 8
Javascript Question

Firebase startAt query gives unexpected results

I perform the following query:

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


And I get the following output. It's wierd that output also contains those objects which I have not queried for in the path field:

[
{
"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
}
]


The output also includes other results where
path
is "
54a8924f8c417b2a281f4e3a4c639785/Test Folder/
" but I queried for
path
starting with "
54a8924f8c417b2a281f4e3a4c639785/Test Folder 2/
"

Answer

Queries in Firebase use lexicographic ordering when sorting/filtering strings. Consequently it means that Test Folder/ is actually greater than Test Folder 2/, because / is greater than (space).

What you have to do is limiting the query with an endAt clause. Using a high Unicode code point should allow any text, as long as the prefix is the same.

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

The old Firebase documentation had a nice example for that here under Range queries, but it seems to have disappeared in the transition.