Firebase 于 2023 年引入了 OR 子句 。
Web 客户端的模块化 Web 示例
import { or, collection, query, where } from "firebase/firestore";
const q = query(
collection(db, "collection_name"),
or(
where('status', '==', 'open'),
where('createdAt', '<=', Timestamp.fromDate(new Date()))
)
);
可以 AND
与 OR
子句结合:
import { and, or, collection, query, where } from "firebase/firestore";
const q = query(
collection(db, "cities"),
and(
where('state', '==', 'CA'),
or(
where('capital', '==', true),
where('population', '>=', 1000000)
)
));
Firebase 函数示例
import { initializeApp } from 'firebase-admin/app';
import { Filter, getFirestore } from "firebase-admin/firestore";
const firebaseApp = initializeApp({
// your paramateres
});
const firestore = getFirestore(firebaseApp);
const result = await firestore.collection("cities")
.where(
Filter.or(
Filter.where('capital', '==', true),
Filter.where('population', '>=', 1000000)
)
)
.get();