Google cloud firestore Firestore查询日期范围

Google cloud firestore Firestore查询日期范围,google-cloud-firestore,querying,Google Cloud Firestore,Querying,我的文档上有一个日期范围(使用日期对象),类似这样 { start: March 5 end: April 7 } 我正在试图弄清楚如何构建一个查询来返回某一特定日期的所有事件,例如3月27日,比如 类似于的查询表示获取开始日期在3月27日之前、结束日期在3月27日之后的所有文档,但由于Firestore查询的限制,这似乎是不可能的 所以我花了好几个小时来挠头,要么为此构建一个查询,要么以某种方式构造我的数据,这样就可以做到这一点 这可能吗?只需使用小于或大于该查询即可。例如,在我的a

我的文档上有一个日期范围(使用日期对象),类似这样

{
  start: March 5
  end: April 7
}
我正在试图弄清楚如何构建一个查询来返回某一特定日期的所有事件,例如
3月27日
,比如

类似于
的查询表示获取开始日期在3月27日之前、结束日期在3月27日之后的所有文档,但由于Firestore查询的限制,这似乎是不可能的

所以我花了好几个小时来挠头,要么为此构建一个查询,要么以某种方式构造我的数据,这样就可以做到这一点


这可能吗?

只需使用小于或大于该查询即可。例如,在我的android For snapshot listener(监听更新)应用程序中,我做了如下操作:

    ListenerRegistration registration;
    String lessDate = "09/04/2019 23:59:59";
    String greaterDate = "11/04/2019 23:59:59";
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss", Locale.getDefault());
    try {
        Date lDate = dateFormat.parse(lessDate);
        Date gDate = dateFormat.parse(greaterDate);
        Query q = db.collection("collection").whereGreaterThan("createdAt", new Timestamp(lDate))
                .whereLessThan("createdAt", new Timestamp(gDate));
        registration = q.addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot value,
                                @Nullable FirebaseFirestoreException e) {
                if (e != null) {
                    Log.i("FIRESTORE ERROR", "Listen failed.", e);
                    return;
                }
                for (DocumentChange doc : value.getDocumentChanges()) {
                    // What ever you want to do.
                }
            }
        });
    } catch (ParseException e) {
        e.printStackTrace();
    }
监听器注册;
字符串lessDate=“09/04/2019 23:59:59”;
字符串greaterDate=“11/04/2019 23:59:59”;
SimpleDataFormat dateFormat=新的SimpleDataFormat(“dd/MM/yyyy hh:MM:ss”,Locale.getDefault());
试一试{
Date lDate=dateFormat.parse(lessDate);
dategdate=dateFormat.parse(greaterDate);
查询q=db.collection(“collection”)。其中大于(“createdAt”,新时间戳(lDate))
.whereLessThan(“createdAt”,新时间戳(gDate));
注册=q.addSnapshotListener(新的EventListener(){
@凌驾
public void onEvent(@Nullable QuerySnapshot value,
@可为空的FireBaseFireStore异常(e){
如果(e!=null){
Log.i(“FIRESTORE错误”,“侦听失败”,e);
回来
}
对于(DocumentChange文档:value.getDocumentChanges()){
//无论你想做什么。
}
}
});
}捕获(解析异常){
e、 printStackTrace();
}

对于日期,您实际上可以从系统中获取日期,或者从用户处获取输入,并根据需要进行自定义。如果您需要帮助,请告诉我。

以上代码节省了我的时间。我知道这种代码结构很奇怪,但它是有效的。 这可以用来搜索日期范围,希望对大家有所帮助

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("Asia/Manila"));
    Date newDate = null;
    Date newDate1 = null;
    try {
        newDate = sdf.parse(YOUR_EDIT_TEXT + " 00:00:00");
        newDate1 = sdf.parse(YOUR_EDIT_TEXT + " 23:59:59");
    } catch (ParseException e) {
        e.printStackTrace();
    }


    db.collection("sales")
            .whereGreaterThanOrEqualTo("created_at", newDate)
            .whereLessThan("created_at", newDate1)
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    for(final DocumentSnapshot sales: task.getResult()) {
                        // Do what ever you want with the data.
                    }
                }
            });
SimpleDataFormat sdf=新的SimpleDataFormat(“MM/dd/yyyy hh:MM:ss”);
sdf.setTimeZone(TimeZone.getTimeZone(“亚洲/马尼拉”);
Date newDate=null;
Date newDate1=null;
试一试{
newDate=sdf.parse(您的编辑文本+“00:00:00”);
newDate1=sdf.parse(您的编辑文本+“23:59:59”);
}捕获(解析异常){
e、 printStackTrace();
}
db.收款(“销售”)
。其中,大于或等于“创建于”,新日期)
.whereLessThan(“创建于”,newDate1)
.get()
.addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
对于(最终文档快照销售:task.getResult()){
//对数据做任何你想做的事情。
}
}
});

在Firestore中也可以,但请添加真实数据库的屏幕截图。欢迎使用SO!当你回答一个问题时,请稍加评论。在这种情况下,有另一个答案,所以你应该暴露你的答复的优点。