简体   繁体   English

在 Firebase 控制台中按数组或 Map 字段值查询 Firestore 文档

[英]Querying Firestore documents by Array or Map fields values in Firebase console

Here, I want to query by the value "ministoreid1" in Firebase console.在这里,我想通过 Firebase 控制台中的值“ministoreid1”进行查询。 But I am not able to figure out.但我无法弄清楚。 Here, I am trying to do the query in console not using codes.在这里,我试图在不使用代码的控制台中进行查询。

Here, I am trying to do the query in console not using codes. 在这里,我试图在控制台中不使用代码进行查询。

Unfortunately, there is currently no way you can filter your documents in the Firebase console according to a value that exist in an array. 不幸的是,当前您无法根据数组中存在的值在Firebase控制台中过滤文档。 As you can see, there are only the following operators: 如您所见, 只有以下运算符:

== is equal to ==等于

> is after >之后

>= is after and includes >=在之后,包括

< is before <在之前

<= is before and includes <=在之前,包括

But an whereArrayContains option it is not present yet. 但是whereArrayContains选项尚不存在。 I recommend you file a feature request for that. 我建议您为此提交功能请求 It might be also useful for other developers. 这对于其他开发人员也可能有用。

The query that you perform in the console does't return any results because you are checking if the mini_stores_assigned is equal to ministoreid1 , which obviously is not since the mini_stores_assigned property is an array and not a String so you can compare them. 您在控制台执行查询简化版,因为要检查是否返回任何结果mini_stores_assigned等于ministoreid1 ,这显然是不能,因为mini_stores_assigned属性是一个数组,而不是一个字符串,因此您可以对它们进行比较。

I have filed the feature request at Alex' suggestion. 我已根据亚历克斯的建议提出了功能要求。 And the reply I received from Firebase Support: 以及我从Firebase支持部门收到的答复:

Currently, there is no query similar to array-contains available in the Firestore Console. 当前,没有任何类似于Firestore控制台中可用的包含数组的查询。 I can file a feature request ticket on your behalf. 我可以代表您提交功能请求通知单。 However, I can't guarantee anything at the moment, so please watch out for any updates on our blog or release notes for now. 但是,我目前不能保证任何事情,因此请注意我们博客上的任何更新或发行说明。 For the map field, you can try to filter on the console using the format: 'mapFieldName.keyName' in the field text box 对于地图字段,您可以尝试在控制台上使用字段文本框中的格式“ mapFieldName.keyName”进行过滤

So we can query for map values by 'mapFieldName.keyName'. 因此,我们可以通过“ mapFieldName.keyName”查询地图值。 I didn't know this before. 我以前不知道

For future use, Firebase has added the feature request by Ssuburat.为了将来使用,Firebase 添加了 Ssuburat 的功能请求。 You can now can filter your documents in the Firebase console according to a value that exist in an array.您现在可以根据数组中存在的值在 Firebase 控制台中过滤您的文档。 图片在这里

###FILTER BLOGS BY USER. ###按用户过滤博客。

for example if you have two collections (one to many)例如,如果您有两个 collections(一对多)

/users
/blogs

blog and user has these schemes:博客和用户有以下方案:

blog: { name,date,user:{myusername:true}}

//notice that user is a map or object and document blog has id itself wich you can use in user document and viceversa. //请注意,用户是 map 或 object 并且文档博客本身具有 ID,您可以在用户文档中使用,反之亦然。

user:{name,lastname,blogs:{idblog1:true,idblog2:true}} //blogs is a map or object

if you want to filter by map object you can do this:如果你想通过 map object 过滤你可以这样做:

import firebase from "firebase/compat/app";
import { getFirestore } from "firebase/firestore";

const appFirebase = firebase.initializeApp(firebaseConfig);
export const dbFirebase = getFirestore(appFirebase);

const myuser= "myusername"
const q = query(collection(dbFirebase, "blogs"), where(`user.${myuser}`, "==", true));
const blogsSnapshot = await getDocs(q);

blogsSnapshot.forEach((doc) => {

      // doc.data() is never undefined for query doc snapshots
      console.log(doc.id, " => ", doc.data());
    });

console.log({blogsSnapshot}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase Firestore - 如何从特定文档中检索数组字段并将这些数组值用于 map 文档 - Firebase Firestore - how to retrieve an array field from a specific document and use those array values to map documents 查询 flutter 中的 Firestore map 字段 - Querying Firestore map fields in flutter 查询 Firebase Firestore 数据 - Querying Firebase Firestore Data Firebase Firestore:有没有办法为集合中的所有文档强制执行必填字段? - Firebase Firestore: Is there a way to enforce required fields for all documents in a collection? Firebase Firestore:查询并获取其中一个字段中包含查询值的文档 - Firebase Firestore: Query and get documents that contain the queried value in one of the fields Firestore 界面默认折叠字段(console.firebase.google) - Firestore interface collapse fields by default (console.firebase.google) 删除 Firebase 的“is”前缀 Boolean 值的 Firestore 字段 - Drop "is" prefix for Firebase Firestore fields for Boolean Values Firebase firestore 多个字段的唯一值 - Firebase firestore unique values in multiple fields Firestore - 批量查询 REST 中的嵌套文档 API - Firestore - Batch querying nested documents in REST API Node JS / Firebase Admin SDK / Firestore - 使用快照接收 firebase 文档后获取空数组 - Node JS / Firebase Admin SDK / Firestore - Getting empty array after receiving firebase documents using snapshots
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM