英文:
Firestore - Query by field in array with java
问题
我在Firestore中有这样的数据:
我想从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,像这样:
那么这个查询可以正常工作:
Query query = ordersRef.whereEqualTo("orders.items.status", "TEST");
但这不能解决问题,因为items Map 需要有多个对象,像这样:
英文:
I have data in Firestore like this:
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:
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
--
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论