Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用'in'子句从firestore集合中获取具有特定ID(复数)的文档?_Javascript_Firebase_React Native_Google Cloud Firestore - Fatal编程技术网

Javascript 如何使用'in'子句从firestore集合中获取具有特定ID(复数)的文档?

Javascript 如何使用'in'子句从firestore集合中获取具有特定ID(复数)的文档?,javascript,firebase,react-native,google-cloud-firestore,Javascript,Firebase,React Native,Google Cloud Firestore,我已经读了一些关于这个主题的帖子,但由于某种原因,我无法获取我需要的文档。我有一个自动生成id的集合用户,每个文档都包含名称和电子邮件。这是我的收藏: 请注意,ID是自动生成的 然后,我尝试在代码中执行以下操作: firebase.firestore() .collection("users") .where( "id", "in", ids

我已经读了一些关于这个主题的帖子,但由于某种原因,我无法获取我需要的文档。我有一个自动生成id的集合
用户
,每个文档都包含名称和电子邮件。这是我的收藏:

请注意,ID是自动生成的

然后,我尝试在代码中执行以下操作:

firebase.firestore()
        .collection("users")
        .where(
          "id",
          "in",
          ids
        )
        .get()
        .then((querySnapshot) => {
            const people = [];
            querySnapshot.forEach(documentSnapshot => {
              people.push({
                  ...documentSnapshot.data(),
                  key: documentSnapshot.id
                  });
                });
              console.log("people: ", people);
          });
我的
人员
数组为空。我非常确定我的
ids
数组具有正确的id。我不确定这部分是否正确:

firebase.firestore()
        .collection("users")
        .where(
          "id",
          "in",
          ids
        )
        .get()

“id”
是自动生成列的正确名称吗?

在这种情况下,我假设
ids
是客户端代码中的一个数组,您只想在firestore中查询这些id

不幸的是,在firestore中使用
.where()
功能时,它不会查询文档ID,而是查询文档的内容

如果要使用firestore的
.where()
索引功能,则需要在实际文档数据中添加
uid
字段。一旦你这样做了,你应该能够只查询你想要的ID


若要按文档ID查询文档,应使用它返回一个特殊值,可与
where()
过滤器一起使用,以按文档ID进行搜索


以下代码已从(Typescript/JavaScript)中调整:


注意:我不想在Firestore中使用术语“key”,而是使用“id”。如果您在文档中使用的是“id”,那么您可以始终使用“\u id”来代替。

我明白了。这不是一个坏习惯吗?我的意思是,我有一次身份证,然后我把它们放在第二次。还是这是一种标准做法?Tbh,除了for循环和逐个查询之外,我找不到任何“简单”的方法来获取数组中的文档?此外,我是否正确地将列命名为“id”?数据的非规范化(重复)在NoSQL数据结构(如Firestore)中是一种不错的做法。不可以,除了
firestore.collection('users').doc()
@cpboyce之外,您不能查询文档ID,这在中没有提到,但从firestore SDK(/)的v0.1.1开始就存在了。我之所以看到它,是因为它最终被添加到了去年6月。这也不是指南中唯一没有更新的东西。该指南尚未更新,说明批量写入不算数。您需要查看firebase.firestore.FieldPath.documentId()创建的值的一些限制-它返回文档的完整路径,而不仅仅是特定的documentId@LeadDreamer,我不确定何时,但是这个问题已经解决了,因为这个答案已经发布了,因为这里包含的代码按预期工作。我会看看是否能找到一些关于何时/如何修复的信息,这些信息应该在2020年1月至6月之间。@LeadDreamer根据我之前的评论,有两种模式的
firebase.firestore.FieldPath.documentId()
。在
集合参考上使用时,只需指定文档ID(例如
“C90QMZGBPSJSJA1QIQ95F6”
)。但是,在集合组查询中使用时,可以指定有效的文档路径(例如,
“users/Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3/todos/c90QMzGbPSJA1qiQ95f6”
)。这似乎是有意的。谢谢你提到它,我会确保在这个功能的文档不可用时用这些信息更新要点。
function chunkArr(arr, n) {
  if (n <= 0) throw new Error("n must be greater than 0");
  return Array
    .from({length: Math.ceil(arr.length/n)})
    .map((_, i) => arr.slice(n*i, n*(i+1)))
}

async function fetchDocumentsWithId(collectionQueryRef, arrayOfIds) {
  // in batches of 10, fetch the documents with the given ID from the collection
  const fetchDocsPromises = chunkArr(arrayOfIds, 10)
    .map((idsInChunk) => (
      collectionQueryRef
        .where(firebase.firestore.FieldPath.documentId(), "in", idsInChunk)
        .get()
    ))

  return Promise.all(fetchDocsPromises)
    .then((querySnapshotArray) => {
      const allDocumentsArray = [];
      for (let querySnapshot of querySnapshotArray) {
        querySnapshot.forEach(doc => allDocumentSnapshotsArray.push({...doc.data(), key: doc.id}))
      }
      return allDocumentsArray;
    });
}

const usersColRef = firebase.firestore()
        .collection("users");
const ids = [ /* ... */ ];

const docDataArray = fetchDocumentsWithId(usersColRef, ids);

const usersColRef = firebase.firestore()
        .collection("users");
const ids = [ /* ... */ ];
const docDataArray = [];

await fetchDocumentsWithId(usersColRef, ids, (doc) => docDataArray.push({ ...doc.data(), key: doc.id }))

console.log(docDataArray)