Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.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 如何在firebase-firestore上进行正确的搜索查询以查找数组内的对象_Javascript_Database_Firebase_Google Cloud Firestore - Fatal编程技术网

Javascript 如何在firebase-firestore上进行正确的搜索查询以查找数组内的对象

Javascript 如何在firebase-firestore上进行正确的搜索查询以查找数组内的对象,javascript,database,firebase,google-cloud-firestore,Javascript,Database,Firebase,Google Cloud Firestore,我正在尝试通过这样做来学习firebase,所以我可能错误地建模了我的DB。 无论如何,我想知道是否有一种方法可以搜索所有具有特定电子邮件价值的评论。让我向您展示我的数据库的结构 我试着阅读文档,并尝试了一些不起作用的事情,例如: const fetchComments = async () => { const commentRef = await db.collection("comments") return commentRef.where("email", '

我正在尝试通过这样做来学习firebase,所以我可能错误地建模了我的DB。 无论如何,我想知道是否有一种方法可以搜索所有具有特定电子邮件价值的评论。让我向您展示我的数据库的结构

我试着阅读文档,并尝试了一些不起作用的事情,例如:

   const fetchComments = async () => {
   const commentRef = await db.collection("comments")

   return commentRef.where("email", '==', "123@123.com").get()
}


   fetchComments().then(function (querySnapshot) {
    querySnapshot.forEach(function (doc) {

        console.log(doc.id, ' => ', doc.data());
    });})
但是日志记录不会返回任何信息,如果您记录querySnapshot,我当然会得到一个包含大量内容的对象,但它似乎没有数据

所以我想知道,首先,有没有一种方法可以得到所有有特定电子邮件的评论? 或者,如果基本上我以错误的方式构建了所有数据,并且不可能获取它们


谢谢

需要注意的重要一点是,firestore将始终从查询中返回整个文档

从今天起,您可以在where子句中添加以下条件:
=
数组包含
中,或
数组包含任何

最接近您的用例的一个条件是

数组包含: 如果文档中存储了数组,例如:

liked_by: ["user1", "user2", "user3"]
您可以使用where子句
运行查询。where('liked_by','array contains','user1')
将返回所有文档,其中包含字符串“user1”的数组名为
liked_by
。如果在数组中存储了对象,如

comments: [
  {
    text: "comment",
    user_id: "user1",
    date: "dd/mm/yyyy"
  }, ...
]
您必须将整个对象放在where子句中,例如:
.where('comments','array contains',{text:'comment',user_id:'user1',date:'dd/mm/yyyyy'})
将返回所有文档,这些文档包含一个名为comments的数组,并且具有与where子句中相同的对象

在您的例子中,每个文档中都有一个注释数组,因此,除非您碰巧知道整个对象,否则无法查询此类文档

如果这些文档ID(1,2,3…)对您很重要,我建议您使用以下结构:

comments:{ // collection name
  "random_document_id_created_by_firestore": { 
// doc ids are assigned automatically by firestore when you use the add method. 
// refer: https://firebase.google.com/docs/firestore/manage-data/add-data#add_a_document
    "comment": "comment text",
    "datePosted": "dd/mm/yyyy",
    "email": "123@123.com",
    "id": 9993,
    "docId": 1 // add the key name (docId) as per your choice, e.g. postId
  }, ...
}
然后,您可以在查询中链接多个where子句以根据需要筛选数据:
.where('docId','=',1)。where('email','=','123@123.com“).get()

是的!文档ID 1、2、3是我的博客帖子的ID,因此它们很重要。如果理解正确,您建议我将帖子添加到我的评论对象中,以及链接
。where('docId','==',1)。where('email','==','123@123.com)。get()
应该有效吗?是的。您可以链接多个where子句。