Firestore-嵌套查询

时间:2019-02-24 23:28:34

标签: firebase google-cloud-firestore firebase-admin

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

这是我的一个文档,其中一个文档包含photos

photos: {
    id1: true
    id2: true
}

我想大胆地检索照片对象中具有 id1 的所有项目,对此的查询是什么?

1 个答案:

答案 0 :(得分:1)

Firebase documentation on simple queries显示:

# 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)

有关.表示法,请参见fields in nested objects上的文档。

请注意,您可能需要更改数据模型以将数组用于此数据,例如arrays can now be used to model mathematical sets。在文档中使用这样的数组:

user: ['id1', 'id2']

您可以使用以下方法进行过滤:

photos_ref = db.collection('documents')

query = photos_ref.where(u'photos', u'array_contains', u'id1')

要向一组行为类似的数组中添加/删除项目,请参见updating elements in an array上的文档。