Firestore - 嵌套查询

5 python firebase firebase-admin google-cloud-firestore

我对 firebase 真的很陌生,老实说,我发现查询很难写。我正在使用 python 编写一个脚本,firebase_admin我希望查询/答案在 python 代码中,但在任何其他编程语言中都可以。

这是我的一份文件,一份文件包含photos

photos: {
    id1: true
    id2: true
}
Run Code Online (Sandbox Code Playgroud)

我想成为 ale 来检索它们在照片对象中具有id1 的所有项目,对此的查询是什么?

Fra*_*len 7

正如关于简单查询Firebase 文档所示:

# Create a reference to the photos collection
ref = db.collection('documents')

# Create a query against the collection
query_ref = ref.where(u'photos.id1', u'==', True)
Run Code Online (Sandbox Code Playgroud)

有关.表示法,请参阅有关嵌套对象中的字段的文档。

请注意,您可能希望更改数据模型以将数组用于此数据,因为数组现在可用于对数学集进行建模。在您的文档中使用这样的数组:

user: ['id1', 'id2']
Run Code Online (Sandbox Code Playgroud)

然后你可以过滤:

photos_ref = db.collection('documents')

query = photos_ref.where(u'photos', u'array_contains', u'id1')
Run Code Online (Sandbox Code Playgroud)

要向此数组中添加/删除项目,请参阅有关更新数组中元素的文档。

  • “u”代表什么?不确定我是否见过这个 (2认同)