查询以迭代 Firestore 中的地图数组(Web)

Lui*_*oga 1 web firebase google-cloud-platform react-native google-cloud-firestore

我在我的 React Native 项目中使用 Firestore,我想通过使用 .where() 实时获取 X 文档,我想我正在寻找的字段位于地图数组内,我怎样才能执行查询以根据字段“r_user_uid”获取文档

就像是:

const db = firebase.firestore();
db.collection("f_invitations").where("invitations.r_user_uid", "==", 12) 
                              .onSnapshot((querySnapshot) => {...}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Ren*_*nec 5

下面,使用array-contains,应该可以解决问题。

  db.collection('f_invitations')
    .where('invitations', 'array-contains', { r_user_uid: '12' })
    .onSnapshot(function (querySnapshot) {
      querySnapshot.forEach(function (doc) {
        console.log(doc.data());
      });
    });
Run Code Online (Sandbox Code Playgroud)

但请注意,查询中的整个对象应与数组中的整个对象匹配。

换句话说,以下内容不起作用:

  db.collection('f_invitations')
    .where('invitations', 'array-contains', {
      r_user_uid: '12',
      r_user_name: 'John',
    })
Run Code Online (Sandbox Code Playgroud)