查询引用数组

时间:2019-03-20 15:50:08

标签: python google-cloud-firestore

我有一个包含一堆用户引用的数组。我正在尝试查找用户所在的所有游戏。这样做似乎很简单。 Game集合包含一个数组Players以及对用户的引用。

def list_games(user):
    return db.collection(u'Game').where("Players","array_contains",user)

for row in db.collection(u'User').get():
    user=row
list_games(user)

当我尝试这个简单的代码时,我在下面包含的list_games的一行中遇到错误。 (已删除了一些路径信息,但其他方面则保持不变)

Traceback (most recent call last):
  File "listGames.py", line 29, in <module>
    for game in list_games(user).get():
  File "listGames.py", line 14, in list_games
    return db.collection(u'Game').where("Players","array_contains",user)
  File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\google\cloud\firestore_v1beta1\collection.py", line 222, in where
    return query.where(field_path, op_string, value)
  File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\google\cloud\firestore_v1beta1\query.py", line 265, in where
    value=_helpers.encode_value(value),
  File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-pakages\google\cloud\firestore_v1beta1\_helpers.py", line 200, in encode_value
    "Cannot convert to a Firestore Value", value, "Invalid type", type(value)
TypeError: ('Cannot convert to a Firestore Value', <google.cloud.firestore_v1beta1.document.DocumentSnapshot object at 0x000000000454D668>, 'Invalid type', <class 'google.cloud.firestore_v1beta1.document.DocumentSnapshot'>)

在我看来,我应该为用户做一些不同的事情,例如user.id,但我无法弄清楚。我需要怎么做才能在数组中搜索引用?谢谢!

2 个答案:

答案 0 :(得分:0)

userDocumentSnapshot,因此您应该这样处理。也许您想与to_dict()一起convert it to a dict

答案 1 :(得分:0)

正确的做法是比较.reference,如下所示。

def list_games(user):
    return db.collection(u'Game').where("Players","array_contains",user.reference)
相关问题