Firebase 按日期字符串查询

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

我知道可以按上一个问题中的时间戳进行查询,但是,如果日期采用以下格式会怎样:

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 按日期字符串查询

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

发布评论

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

>www.elefans.com

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