firestore 检查 id 是否存在于 ids 数组中

MJ *_*nim 3 javascript firebase angularfire2 google-cloud-firestore

我使用 firebase firestore 为我的网站开发一个简单的聊天应用程序。每个聊天会话都有一个 id

假设我有一个 id 数组

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

我想使用下面的代码获取 id 等于 chat_sessions 对象中任何 id 的所有文档。

return this.afs
    .collection('chats', ref => ref.where('uid','in_array',chat_sessions)).snapshotChanges...
Run Code Online (Sandbox Code Playgroud)

但我没有得到任何结果。

我来自 PHP/MYSQL 背景,PHP 相当于我想要实现的目标,在 PHP 中是这样的

if(in_array(uid,chat_sessions)){
      get(doc/uid)
      }
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我们进行正确的查询,根据数组中的 id 列表检查文档 id 吗?谢谢你!

MJ *_*nim 10

谢谢@frank van Puffelen。你几乎是对的。我应该用in而不是in_array

ref.where(firebase.firestore.FieldPath.documentId(),'in_array',chat_sessions)
Run Code Online (Sandbox Code Playgroud)

不工作。相反,我替换in_arrayin

ref.where(firebase.firestore.FieldPath.documentId(),'in',chat_sessions)
Run Code Online (Sandbox Code Playgroud)

这有效!谢谢


Fra*_*len 7

您的查询是:

ref.where('uid','in_array',chat_sessions)
Run Code Online (Sandbox Code Playgroud)

uid这会根据 .txt 文件的值检查每个文档中调用的字段chat_sessions

相反,您似乎想根据数组检查每个文档的 ID,您可以这样做:

ref.where(firebase.firestore.FieldPath.documentId(),'in_array',chat_sessions)
Run Code Online (Sandbox Code Playgroud)

  • 找到它:`从“firebase/firestore”导入{documentId};...where(documentId(), 'in', chat_sessions)`https://firebase.google.com/docs/reference/js/firestore_#documentid (2认同)