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.
这是我的一个文档,其中一个文档包含照片
集
Here's my one of my document, one document contains photos
set
photos: {
id1: true
id2: true
}
我想大胆地检索照片对象中具有 id1 的所有项目,对此的查询是什么?
I want to be ale to retrieve all items where they have id1 in photos object, what would be the query for that?
推荐答案
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']
然后可以使用以下方法进行过滤:
You can then filter with:
photos_ref = db.collection('documents')
query = photos_ref.where(u'photos', u'array_contains', u'id1')
To add/remove items to this array-that-behaves-like-a-set, see the documentation on updating elements in an array.
这篇关于Firestore-嵌套查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!