首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Google Firestore -如何在一次往返中通过多个in获取多个文档?

Google Firestore -如何在一次往返中通过多个in获取多个文档?
EN

Stack Overflow用户
提问于 2017-10-13 10:18:43
回答 11查看 67.3K关注 0票数 152

我想知道是否有可能在Firestore数据库的一次往返(网络调用)中通过I列表获取多个文档。

EN

回答 11

Stack Overflow用户

发布于 2019-11-09 22:46:38

他们刚刚宣布了这项功能,https://firebase.googleblog.com/2019/11/cloud-firestore-now-supports-in-queries.html

现在您可以使用这样的查询,但要记住输入大小不能大于10。

userCollection.where('uid', 'in', ["1231","222","2131"])

票数 39
EN

Stack Overflow用户

发布于 2018-11-28 06:07:19

在实践中,您可以像这样使用firestore.getAll

代码语言:javascript
复制
async getUsers({userIds}) {
    const refs = userIds.map(id => this.firestore.doc(`users/${id}`))
    const users = await this.firestore.getAll(...refs)
    console.log(users.map(doc => doc.data()))
}

或者使用promise语法

代码语言:javascript
复制
getUsers({userIds}) {
    const refs = userIds.map(id => this.firestore.doc(`users/${id}`))
    this.firestore.getAll(...refs).then(users => console.log(users.map(doc => doc.data())))
}
票数 33
EN

Stack Overflow用户

发布于 2018-05-17 23:14:00

您可以使用如下函数:

代码语言:javascript
复制
function getById (path, ids) {
  return firestore.getAll(
    [].concat(ids).map(id => firestore.doc(`${path}/${id}`))
  )
}

可以使用单个ID进行调用:

代码语言:javascript
复制
getById('collection', 'some_id')

或ID数组:

代码语言:javascript
复制
getById('collection', ['some_id', 'some_other_id'])
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46721517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档