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

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

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

问题

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

{
    'agreement': 'ALR87HJLKJJ78954',
    'agreementDetails': {
        'name': 'johnny5isalive',
        'country': 'Spain'
    }
}

我的查询如下所示:

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

我收到以下错误:

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

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

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.

{
    'agreement' : 'ALR87HJLKJJ78954',
    'agreementDetails' : {
        'name' : 'johnny5isalive',
        'country' : 'Spain'
    }
}

My query looks like the following:

query = db.collection('rentalAgreements').where('agreement', '==', agreement_number).order_by('/agreementDetails/name', direction=firestore.Query.ASCENDING).limit(10)
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

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

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

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

query = db.collection('rentalAgreements')
    .where('agreement', '==', agreement_number)
    .order_by('agreementDetails.name', direction=firestore.Query.ASCENDING)
    .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:

确定