[英]Querying Firestore documents by Array or Map fields values in Firebase console
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.
我以前不知道
###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.