FireStore日期查询无法按预期工作 [英] FireStore date query not working as expected

查看:58
本文介绍了FireStore日期查询无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有日期对象的文档.

I have doc which has a date object.

初始化Firestore的代码:

Code to init Firestore:

 FirebaseFirestore fireStore = FirebaseFirestore.getInstance();
        FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
                .setTimestampsInSnapshotsEnabled(true)
                .build();
        fireStore.setFirestoreSettings(settings);
        firestore = fireStore;

要查询的代码:

FirebaseFirestore db = FireStoreUtils.getInstance();
Query query= db.collection(viewType.collectionName());
        query.whereLessThan("endDate", new Date()); 
        return query.orderBy(viewType.sortProperty()).limit(PAGE_SIZE);

我总是在获取所有记录,并且好像没有应用where子句.在Firebase控制台上,我看到endDate被存储为时间戳.

I am always getting all the records and looks like the where clause is not getting applied. On Firebase console I see that the endDate is stored as timestamp.

Firebase控制台中的文档:

Doc from Firebase console:

createdDate: null (null)
description: "desc" (string)
endDate: February 3, 2019 at 11:18:58 PM UTC-8 (timestamp)
id: "-7475596197450085332" (string)
title: "title" 

推荐答案

Cloud Firestore查询是不可变的,这意味着您无法更改现有查询的属性.如果通过调用whereLessThan()方法更改值,则它将成为一个新查询.因此,要解决此问题,请链接所有方法调用,并将其存储在新的Query对象中,如下所示:

Cloud Firestore queries are immutable, which means that you cannot change the properties of an existing query. If you change the value by calling whereLessThan() method, it becomes a new query. So to solve this, please chain all method calls and store them in a new Query object like this:

Query query = db.collection(viewType.collectionName())
    .whereLessThan("endDate", new Date());
    .orderBy(viewType.sortProperty()).limit(PAGE_SIZE);
    return query;

这篇关于FireStore日期查询无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆