Firestore - 嵌套查询 [英] Firestore - Nested query
问题描述
我对 firebase 真的很陌生,老实说,我发现查询很难写.我正在使用 firebase_admin
在 python 中编写脚本,我希望查询/答案使用 python 代码,但使用任何其他编程语言都可以.
I'm really new to firebase and to be honest I find queries hard to write. I'm working on a script in python using firebase_admin
and i'd like the query/answer to be in python code, but in any other programming language is fine.
这是我的一个文档,一个文档包含photos
set
Here's my one of my document, one document contains photos
set
photos: {
id1: true
id2: true
}
我想成为 ale 来检索照片对象中具有 id1 的所有项目,对此的查询是什么?
I want to be ale to retrieve all items where they have id1 in photos object, what would be the query for that?
推荐答案
作为 有关简单查询的 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)
对于 .
表示法,请参阅关于 嵌套对象中的字段.
For the .
notation, see the documentation on fields in nested objects.
请注意,您可能希望更改数据模型以使用该数据的数组,如 数组现在可用于建模数学集.在您的文档中使用这样的数组:
Note that you'll probably want to change your data model to use an array for this data, as arrays can now be used to model mathematical sets. With a an array like this in your document:
user: ['id1', 'id2']
然后你可以过滤:
photos_ref = db.collection('documents')
query = photos_ref.where(u'photos', u'array_contains', u'id1')
要向此数组添加/删除项目-that-behaves-like-a-set,请参阅 更新数组中的元素.
To add/remove items to this array-that-behaves-like-a-set, see the documentation on updating elements in an array.
这篇关于Firestore - 嵌套查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!