Firestore-嵌套查询 [英] Firestore - Nested query

查看:45
本文介绍了Firestore-嵌套查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆