Firestore – 使用Java查询数组字段中的数据

huangapple go评论59阅读模式
英文:

Firestore - Query by field in array with java

问题

我在Firestore中有这样的数据:

Firestore – 使用Java查询数组字段中的数据

我想从Java中查询它,以获取所有项目状态等于TEST的订单。这可能吗?

当我像这样从客户端获取所有订单时,它运行良好:

CollectionReference ordersRef = dbFirestore.collection("orders");    
Query query = ordersRef.whereEqualTo("client", "client1");

但我无法按照我需要的方式执行它。我尝试过这样:

CollectionReference ordersRef = dbFirestore.collection("orders");    
Query query = ordersRef.whereEqualTo("items.status", "TEST");

但它返回一个空数组


ALEX的回答之后更新


如果我将items转换为Map,像这样:
Firestore – 使用Java查询数组字段中的数据
那么这个查询可以正常工作:

Query query = ordersRef.whereEqualTo("orders.items.status", "TEST");

但这不能解决问题,因为items Map 需要有多个对象,像这样:

Firestore – 使用Java查询数组字段中的数据

英文:

I have data in Firestore like this:

Firestore – 使用Java查询数组字段中的数据

And I want to query it from Java to get all orders where any item status would be equal to TEST. Is it possible?

It works fine when I get all orders from client like:

CollectionReference ordersRef = dbFirestore.collection("orders");    
Query query = ordersRef.whereEqualTo("client", "client1");

But I couldn't do it as I need. I try this

CollectionReference ordersRef = dbFirestore.collection("orders");    
Query query = ordersRef.whereEqualTo("items.status", "TEST");

but it returns an empty array


UPDATE AFTER ALEX'S ANSWER


If I convert items in a Map like:
Firestore – 使用Java查询数组字段中的数据
then this query works fine

Query query = ordersRef.whereEqualTo("orders.items.status", "TEST");

but this doesn´t resolve the problem because the items Map need to have several objects like
Firestore – 使用Java查询数组字段中的数据

--

答案1

得分: 2

这是您要翻译的内容:

And I want to query it from Java to get all orders where any item status would be equal to TEST. Is it possible?

That's currently not possible. You cannot query a collection based on a value that exists within objects that exist in an array field. You could, however, query using an entire object and call Query#whereArrayContains(). So instead of trying to query based only on the value of status, you have to query based on an object that contains all three fields. Besides that, the following query would have worked:

Query query = ordersRef.whereEqualTo("client.items.status", "TEST");

If that data would have been added as a map (object) and not as an array.

However, to solve such a problem, you should consider creating a sub-collection called items inside each document. In this way, a query like this should do the trick:

Query queryByStatus = dbFirestore.collectionGroup("orders").whereEqualTo("status", "TEST");

When using such a query, please also don't forget to create an index.

Why would you use this solution? Simply because there are no limitations. When you store lots of objects inside an array, you might hit the maximum limit of 1 MiB per document.

英文:

> And I want to query it from Java to get all orders where any item status would be equal to TEST. Is it possible?

That's currently not possible. You cannot query a collection based on a value that exists within objects that exist in an array field. You could however query using an entire object and call Query#whereArrayContains(). So instead of trying to query based only on the value of status you have queried based on an object that contains all three fields. Besides that, the following query would have worked:

Query query = ordersRef.whereEqualTo("client.items.status", "TEST");

If that data would have been added as a map (object) and not as an array.

However, to solve such a problem you should consider creating a sub-collection called items inside each document. In this way, a query like this should do the trick:

Query queryByStatus = dbFirestore.collectionGroup("orders").whereEqualTo("status", "TEST");

When using such a query, please also don't forget to create an index.

Why would you use this solution? Simply because there are no limitations. When you store lots of objects inside an array, you might hit the maximum limit of 1 Mib per document.

huangapple
  • 本文由 发表于 2023年7月11日 00:40:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655731.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定