首页 > 解决方案 > Flutter firestore,如何获取包含数组内值的文档

问题描述

我有一个 json 数据存储在 firestore 中,如下所示,文档的 id 是 ddmmyyyy 格式的日期

29032019:{
   morning:[{
      person : 1,
      name:'abc',
      cancelled:false
     },
     {
      person : 2,
      name:'abc',
      cancelled:false
    }
  ],
  afternoon:[{
      person : 3,
      name:'abc',
      cancelled:false
   },
   {
      person : 4,
      name:'abc',
      cancelled:false
   }
 ]
}
30032019:{
   morning:[{
      person : 5,
      name:'abc',
      cancelled:false
     },
     {
      person : 2,
      name:'abc',
      cancelled:false
    }
  ],
  afternoon:[{
      person : 6,
      name:'abc',
      cancelled:false
   },
   {
      person : 7,
      name:'abc',
      cancelled:false
   }
 ]
}

这里 9032019 和 30032019 是文档 ID。如何获取包含上午和下午数组中的人的文档,到目前为止,我可以做的是通过使用接下来的 3 个即将到来的日期来获取文档并遍历上午和下午数组以检查 ID 为 6 的人是否存在于收藏与否。

标签: fluttergoogle-cloud-firestore

解决方案


使用 Firestore,您无法像现在那样在数组中搜索对象的字段。您只能按项目的确切内容搜索数组。

This means you will have to bring the values to search into a new field that is searchable. If you a searching by the person field, you could create a new array field in the document called people that contains only the integers of each person, then use an arrayContains query to find them.


推荐阅读