Swift和Firestore:查询具有今天日期的字段
我天真地尝试执行以下操作。
让ref = Firestore.firestore()。collection(“ posts”)
让今天= Date()
让queryRef = ref.whereField(“ created”,isEqualTo:今天)
不幸的是,它没有返回任何结果,我想它正在为我们的日期字段寻找完全匹配的结果。
注意:希望实际上是这样。我并没有过多地关注它是否应该返回非空结果。 😅
经过几次Google搜索,我想到了这个:
让日历= Calendar.current
让组件= calendar.dateComponents([。year,.month,.day],来自:Date())
让开始= calendar.date(来自:组件)!
let end = calendar.date(byAdding:.day,value:1,to:start)!let queryRef = ref
.whereField(“ created”,isGreaterThan:start)
.whereField(“ created”,isLessThan:end)
现在,我得到了预期的结果。 ☺️
我们可以快速将其重构为CollectionReference
上的方法……
然后在呼叫站点:
让queryRef = queryRef.whereField(“ created”,isDateInToday:date)