我知道可以按上一个问题中的时间戳进行查询,但是,如果日期采用以下格式会怎样:
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 } }我将如何按日期查询?
推荐答案您已将日期存储为字符串.当 Firebase 订购字符串时,它使用字典顺序.按照这个顺序,"2012-20-03" 在 "2012-25-01" 之前.
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".
这就是为什么您链接到的问题(以及 Firebase 文档)通常将日期存储为时间戳.那些有关于日期的所有信息,在一个单一的数字,保证正确订购.
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.
或者,您可以将日期存储为字符串.但在这种情况下,您必须确保日期的格式也能按字典顺序正确排序.对于您的示例:"2012-01-25" 和 "2012-03-20".
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 按日期字符串查询
发布评论