简体   繁体   English

如何在 db firestore 上查询数组?

[英]How to query an array on a db firestore?

I don't think I'm very far from the answer, I tried to query an id inside an array on Firestore.我不认为我离答案很远,我试图在 Firestore 的数组中查询一个 id。

I wanna compare the id of the participants.我想比较参与者的身份。

Also my DB in Firestore:还有我在 Firestore 中的数据库:

在此处输入图像描述

And also my function, for the moment my function return size =0 so they can't catch the query.还有我的 function,目前我的 function 返回大小 =0,因此他们无法捕获查询。

const deleteAbuse = (type) => {
    const participants = channel?.otherParticipants;
    if (!participants || participants.length != 1) {
      return;
    }
    const myID = currentUser.id;
    const otherUserID = participants[0].id;
    console.log('id user', otherUserID);
    channelDB
      .where('participants.id', '==', otherUserID)
      .get()
      .then((querySnapshot) => {
        console.log(querySnapshot.size);
        querySnapshot.forEach((doc) => {
          console.log(doc.ref);
          //doc.ref.delete();
        });
      });
  };

There is no way you can query filter your "channels" collection to get documents based only on a single field of an object that exists inside the participants array.您无法查询过滤“频道”集合以仅基于participants数组中存在的 object 的单个字段来获取文档。 In other words, you cannot create that filtering based only on that ID.换句话说,您不能仅基于该 ID 创建该过滤。 To be able to filter the data, you should pass to the where() function, the entire object, and not only some partial data.为了能够过滤数据,您应该传递给where() function,整个object,而不仅仅是部分数据。 The object should contain values for all the fields. object 应包含所有字段的值。 Only the ID is not enough.只有身份证是不够的。

And also my function, for the moment my function return size =0还有我的 function,目前我的 function 返回大小 =0

That's the expected behavior since in your array there isn't any object that holds only a single field called id .这是预期的行为,因为在您的数组中没有任何 object 只包含一个名为id的字段。 All objects hold 7 fields, or even more than that, as I cannot see all fields in your screenshot.所有对象都包含 7 个字段,甚至更多,因为我无法在您的屏幕截图中看到所有字段。

I wrote an article called:我写了一篇文章叫:

Where you can find in the first part, the simplest way to get a custom object from an array of custom objects.在第一部分中,您可以找到从自定义对象数组中获取自定义 object 的最简单方法。

Alternatively, you can create an array that can hold only the IDs of the participants, filter the documents and create another database call to get their corresponding data.或者,您可以创建一个包含参与者 ID 的数组,过滤文档并创建另一个数据库调用以获取其相应的数据。

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

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