Sky*_*kye 19 android google-cloud-firestore
如何通过一个字段的多个值获取数据?例如,我有包含帖子的数据库,我想查询blogId为1或2的所有帖子,按时间戳排序.
collection("posts").whereEqualTo("blogId", "1")
.whereEqualTo("blogId", 2).orderBy("timestamp", Query.Direction.DESCENDING).limit(50)
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用:(
怎么做到这一点?问候 :)
Alb*_*haw 30
查询将如下所示:
database.collection("collectionName").where("fieldName", "in", ["fieldValue1", "fieldValue2"]);
Run Code Online (Sandbox Code Playgroud)
您最多可以有 10 个值 (fieldValueX) 来检查“IN”。
OP所需的代码如下:
database.collection("posts").where("blogId", "in", ["1", "2"]);
Run Code Online (Sandbox Code Playgroud)
phi*_*con 14
您可以组合 Observables 并作为一个返回
orQuery(){
const $one = this.afs.collection("posts", ref => ref.where("blogId","==","1")).valueChanges();
const $two = this.afs.collection("posts", ref => ref.where("blogId","==","2")).valueChanges();
return combineLatest($one,$two).pipe(
map(([one, two]) => [...one, ...two])
)
}
getOr(){
this.orQuery().subscribe(data => console.log(data))
}
Run Code Online (Sandbox Code Playgroud)
小智 6
ORfirebase firestore 不接受运算符:
Cloud Firestore 对逻辑 OR 查询提供有限支持。in 和 array-contains-any 运算符支持单个字段上最多 10 个相等 (==) 或数组包含条件的逻辑 OR。对于其他情况,请为每个 OR 条件创建单独的查询,并将查询结果合并到您的应用程序中。
通常使用 firebase 语法你可以调用两个集合:
const res1 = async collection("posts", ref => ref.where('blogId', '==', 1).get();
const res2 = async collection("posts", ref => ref.where('blogId', '==', 2).get();
Run Code Online (Sandbox Code Playgroud)
您可以在提供给视图之前合并结果。
但在这种情况下,如果您有 blogIds,则可以使用以下语法:
collection("posts").orderBy('blogId').startAt(1).endAt(2);
编辑
Firebase 已侦听我们的请求,并且它们已包含IN来自7 Nov, 2019. 这是一种OR查询,您可以在其中使用多达10 OR过滤器。
对于安卓:
collection("posts").whereIn("blogId", Arrays.asList("1", "2"))
.orderBy("timestamp", Query.Direction.DESCENDING).limit(50);
Run Code Online (Sandbox Code Playgroud)
我找不到任何有关将 where 条件进行或运算的能力的文档。但您可以将您的要求重新表述如下blogId:
WHERE blogId > 0 AND blogId < 3
Run Code Online (Sandbox Code Playgroud)
试试这个代码:
collection("posts")
.where("blogId", ">", "0")
.where("blogId", "<", "3")
.orderBy("timestamp", Query.Direction.DESCENDING)
.limit(50)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6935 次 |
| 最近记录: |