按日期字符串查询Firebase

编程入门 行业动态 更新时间:2024-10-27 03:34:30
本文介绍了按日期字符串查询Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道可以按照上一个问题中的时间戳查询,不过,如果日期格式如下:

{lambeosaurus:{ date:2012-20-03,length:12.5,weight:5000 },stegosaurus:{ date:2015-25-13,length:9,weight:2500 } }

解决方案

/ 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

本文发布于:2023-10-22 08:42:03,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   按日   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!