我可以使用 Firebase Firestore Python SDK 对子项进行排序吗?

huangapple go评论113阅读模式
英文:

Can I sort with Firebase Firestore child with the Python SDK?

问题

我有一个 Firebase Firestore 数据,结构如下,但是在尝试按子项排序时出现了问题。

  1. {
  2. 'agreement': 'ALR87HJLKJJ78954',
  3. 'agreementDetails': {
  4. 'name': 'johnny5isalive',
  5. 'country': 'Spain'
  6. }
  7. }

我的查询如下所示:

  1. query = db.collection('rentalAgreements').where('agreement', '==', agreement_number).order_by('/agreementDetails/name', direction=firestore.Query.ASCENDING).limit(10)
  2. results = query.get()

我收到以下错误:

  1. ValueError: Path /agreementDetails/name not consumed, residue: /agreementDetails/name

我确实认为我在冒险尝试,通过谷歌找到了一些关于 order_by_child 的引用,我也尝试过,但返回了错误:

  1. AttributeError: 'Query' object has no attribute 'order_by_child'

这是可能的吗?

英文:

I have a Firebase Firestore with data structured as per below but am having issues being able to sort by a child.

  1. {
  2. 'agreement' : 'ALR87HJLKJJ78954',
  3. 'agreementDetails' : {
  4. 'name' : 'johnny5isalive',
  5. 'country' : 'Spain'
  6. }
  7. }

My query looks like the following:

  1. query = db.collection('rentalAgreements').where('agreement', '==', agreement_number).order_by('/agreementDetails/name', direction=firestore.Query.ASCENDING).limit(10)
  2. results = query.get()

I get the following error:

> ValueError: Path /agreementDetails/name not consumed, residue:
> /agreementDetails/name

I did think I was chancing it a bit and found through Google a number of references to order_by_child which I also tried but it came back with an error:

> AttributeError: 'Query' object has no attribute 'order_by_child'

Is this possible?

答案1

得分: 3

引用对象属性时,您需要在字段路径中使用点表示法:

  1. query = db.collection('rentalAgreements')
  2. .where('agreement', '==', agreement_number)
  3. .order_by('agreementDetails.name', direction=firestore.Query.ASCENDING)
  4. .limit(10)
英文:

When referencing an object property, you will need to use dot notation in the field path:

  1. query = db.collection('rentalAgreements')
  2. .where('agreement', '==', agreement_number)
  3. .order_by('agreementDetails.name', direction=firestore.Query.ASCENDING)
  4. .limit(10)

huangapple
  • 本文由 发表于 2020年1月4日 00:35:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定