简体   繁体   English

firestore 针对 id 数组检查 id 是否存在

[英]firestore Check if id exists against an array of ids

I developing a simple chat applicaiton for my website using firebase firestore.我使用 firebase firestore 为我的网站开发了一个简单的聊天应用程序。 where every chat session has an id每个聊天 session 都有一个 id

provided i have an array of ids假设我有一个 id 数组

chat_sessions = ["q93XhadA9QQLu6X8yfZB", "YXEYISEI8b2iixCcP3VO", "GXrSZbtrmN5NcrtvjFYp"]

I want to get all document whose id is equal to any of the id's in the chat_sessions object using the code below.我想使用下面的代码获取所有 id 等于 chat_sessions object 中任何 id 的文档。

return this.afs
    .collection('chats', ref => ref.where('uid','in_array',chat_sessions)).snapshotChanges...

but I am not getting any results.但我没有得到任何结果。

I come from a PHP/MYSQL background the PHP equivalent of what i am trying to achieve will be sth like this in PHP我来自 PHP/MYSQL 背景,PHP 相当于我试图实现的目标将在 PHP 中像这样

if(in_array(uid,chat_sessions)){
      get(doc/uid)
      }

can anyone help with the right query where we check for document id against a list of ids in an array?任何人都可以帮助我们根据数组中的 id 列表检查文档 id 的正确查询吗? Thank You!谢谢你!

Thank you @frank van Puffelen.谢谢@frank van Puffelen。 You were almost right.你几乎是对的。 I should have used in instead of in_array我应该使用in而不是in_array

ref.where(firebase.firestore.FieldPath.documentId(),'in_array',chat_sessions)

did not work.不工作。 Instead I replaced in_array with in :相反,我用in替换了in_array

ref.where(firebase.firestore.FieldPath.documentId(),'in',chat_sessions)

This worked!这行得通! Thank you谢谢

Your query is:您的查询是:

ref.where('uid','in_array',chat_sessions)

This checks a field called uid in each document against the values of the chat_sessions .这会根据chat_sessions的值检查每个文档中名为uid的字段。

It seems that instead you want to the check the ID of each document against the array, which you can do with:相反,您似乎想根据数组检查每个文档的 ID,您可以这样做:

ref.where(firebase.firestore.FieldPath.documentId(),'in_array',chat_sessions)

I found something else on firestore ie "array-contains-any" for this case.我在 Firestore 上找到了其他东西,即这种情况下"array-contains-any"

Maybe it's updated now.也许现在更新了。

火库条件

UPDATE更新

Hi, firebase did some update recently, so for do it I found out this method嗨,firebase 最近做了一些更新,所以我发现了这个方法

` `

const [products, setProduct] = useState([]);
const ids = ['H11LlJsh3sObwORZhA0b','om9m0lU9HYWyOJZKvEdi','1AoHyHuSFcF01zoyXyTD','6xoBlxsRXUoyzBUcWl0F',
'GJqthlmBGZaFAJqtC2jK','QNT3PxMfhNGg1RZnuqcq','RZgGoFZHyDAYaVZJWxGk','g4UO5P0EgtEqJnawwhXX','gyrZm8p0cEgJdDvTuB1g','mrscldfeYlkaSF151MpI',]

useEffect(() => {
    const saveFirebaseTodos = [];      
    ids.forEach((element) => {
    fetchMyAPI()
    async function fetchMyAPI() {
        const q = query(collection(db, "a"), where('__name__', '==', element));
        const querySnapshot = await getDocs(q); 
        querySnapshot.forEach((doc) => {
        saveFirebaseTodos.push(({id: doc.id, ...doc.data()}));
        /*console.log(doc.id, " => ", doc.data());*/
        if (ids.length == saveFirebaseTodos.length) {
            setProduct(saveFirebaseTodos) 
            }
        });
    }})

}, [])`

In this way, you can check how many element in arrays you want (bypassing 10 elements limit of firebase).通过这种方式,您可以检查您想要的 arrays 中有多少元素(绕过 firebase 的 10 个元素限制)。

Hope can help anyone:D希望可以帮助任何人:D

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM