我有一组酒吧订单,在每个文档中我都有一个包含酒保引用的字段。
我正在尝试使用文档引用来确定位置。
像这样的东西:
orders = this.database.collection("orders",
(ref) => ref.where("barman.path", "==", "barmen/" + this.auth.auth.currentUser.uid),
);
return orders.valueChanges();
barman.path
因为当我得到一个带有字段引用的文档时,这是获取引用路径的方法。我已经尝试只使用
barman
而是 barman.path
.我已经尝试使用 docref (
firestore.googleapis.com/pro...
) 的完整“路径”来创建 where。有任何想法吗?
只放 id 而不是完整的引用将使系统的其他部分变得困难。
最佳答案
为了查询引用字段,您应该创建一个 DocumentReference
与您尝试查询的位置匹配的对象,并使用它。请注意,这需要是原生 Firestore 引用,如 AngularFirestoreCollection
和 AngularFirestoreDocument
只会作为“自定义对象类型”显示给 Firestore。您可以通过 ref
获取此信息AngularFirestore 类型的成员。
我不确定查询的“.path”部分来自何处,因为您要查询的实际字段是 barman
.我已经在示例中删除了它。
在您的情况下,这将类似于:
const queryReference = this.database
.collection('barmen')
.doc(this.auth.auth.currentUser.uid).ref;
orders = this.database.collection('orders',
(ref) => ref.where('barman', '==', queryReference),
);
return orders.valueChanges();
我显然无法完全测试它,因为没有足够的代码来显示
this.auth.auth.currentUser.uid
在这种情况下有效。但它应该为您指明正确的方向。
关于javascript - 如何在 Firestore 中查询 DocumentReference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47608254/