collection("posts").whereEqualTo("blogId", "1")
.whereEqualTo("blogId", 2).orderBy("timestamp", Query.Direction.DESCENDING).limit(50)
上面的代码不起作用 :(
如何实现呢? 谢谢 :)
collection("posts").whereEqualTo("blogId", "1")
.whereEqualTo("blogId", 2).orderBy("timestamp", Query.Direction.DESCENDING).limit(50)
上面的代码不起作用 :(
如何实现呢? 谢谢 :)
Firestore现在支持“IN”查询,以实现此目的。
查询将如下所示:
database.collection("collectionName").where("fieldName", "in", ["fieldValue1", "fieldValue2"]);
您最多可以有10个值(fieldValueX)来检查是否为“IN”。
OP想要的代码如下:
database.collection("posts").where("blogId", "in", ["1", "2"]);
你可以将这些 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))
}
IN
查询。这是一种OR
查询,您可以使用多达10个OR
过滤器。collection("posts").whereIn("blogId", Arrays.asList("1", "2"))
.orderBy("timestamp", Query.Direction.DESCENDING).limit(50);
OR
操作符在Firebase Firestore中不被接受:
Cloud Firestore对逻辑或查询的支持有限。in和array-contains-any 操作符支持单个字段上最多10个相等(==)或包含数组(array-contains)条件的逻辑或运算。对于其他情况,请为每个OR条件创建单独的查询,并在应用程序中合并查询结果。
通常使用Firebase语法可以调用两个集合:
const res1 = async collection("posts", ref => ref.where('blogId', '==', 1).get();
const res2 = async collection("posts", ref => ref.where('blogId', '==', 2).get();
您可以在呈现视图之前合并结果。
但如果您有blogIds,可以使用以下语法:collection("posts").orderBy('blogId').startAt(1).endAt(2);
例如:
我找不到关于将where条件进行OR操作的文档。但是您可以通过以下方式重新表达对blogId
的要求:
WHERE blogId > 0 AND blogId < 3
试试这段代码:
collection("posts")
.where("blogId", ">", "0")
.where("blogId", "<", "3")
.orderBy("timestamp", Query.Direction.DESCENDING)
.limit(50)
对于Android用户,请注意Firestore现在支持逻辑OR查询。因此,您现在可以在多个字段上执行OR查询。该选项从Cloud Firestore版本24.4.5开始提供。
在代码中,它将非常简单:
val db = Firebase.firestore
val citiesRef = db.collection("cities")
val query = citiesRef.where(Filter.or(
Filter.equalTo("capital", true),
Filter.greaterThanOrEqualTo("population", 1000000)
))
将返回 capital
设置为 true
的文档,或者人口大于或等于 1_000_000
的文档。
or
查询:https://firebase.google.com/docs/firestore/query-data/queries#or_queries
他们给出的一个例子是:const q = query(citiesRef,
or(where('capital', '==', true),
where('population', '>=', 1000000)
)
);
Firestore没有OR
查询。您可以通过运行两个查询并将它们拼接起来来完成这项工作。
例如,您可以:
const search = ( key, value ) => collection( 'users' ).where( key, '==', value ).get().then( doc => doc.data() )
Promise.all( [ search( 'name', 'John' ), search( 'name', 'Johnny' ) ] )
.then( ( [ one, two ] ) => one.concat( two ) )
.then( allResults => dothings() )
对于Android,我正在使用这样的OR查询。
List<String> values = new ArrayList<>();
values.add("Value 1");
values.add("Value 2");
db.collection("collectionName")
.whereIn("fieldName", values) // where values is the list we created
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
// Do your stuff
}
}
});