按日期字符串查询Firebase [英] Firebase query by date string
问题描述
我知道可以按照上一个问题中的时间戳查询,不过,如果日期格式如下:
{
lambeosaurus:{
date:2012-20-03,
length:12.5,
weight:5000
},
stegosaurus:{
date:2015-25-13,
length:9,
weight:2500
}
}
$ c $
解决方案 / div> 您已将日期存储为字符串。当Firebase订单字符串使用词典排序。按照该顺序,2012-20-03
出现在2012-25-01之前
。
这就是为什么您链接到的问题(以及Firebase文档)通常将日期存储为时间戳。那些拥有关于日期的所有信息,保证以正确的顺序排列。
或者,您可以将日期存储为字符串。但在这种情况下,您必须确保日期格式按照字典顺序排列。对于你的示例:2012-01-25
和2012-03-20
。
所以在这种情况下,唯一的选择是将数据结构更改为原始问题(和文档)或者按照您的顺序排序的字符串格式想要。
I know that it's possible to query by timestamp as seen in previous question, However, what if the date is in the following format:
{
"lambeosaurus": {
"date" : "2012-20-03",
"length" : 12.5,
"weight": 5000
},
"stegosaurus": {
"date" : "2015-25-13",
"length" : 9,
"weight" : 2500
}
}
How would I go about and query this by date?
解决方案 You've stored your dates as strings. When Firebase order strings it uses lexicographic ordering. In that order, "2012-20-03"
comes before "2012-25-01"
.
This is the reason why the question you linked to (and the Firebase documentation) usually store dates as timestamps. Those have all the information about the date, in a single number that is guaranteed to be ordered correctly.
Alternatively, you can store the date as a string. But in that case you have to make sure the date is in a format that will lexicographically order correctly too. For your sample that would be: "2012-01-25"
and "2012-03-20"
.
So in this case your only option is to change the data structure to either what was in the original question (and documentation) or to a string format that orders in the order you want.
这篇关于按日期字符串查询Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文