英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论