我的节点后端已连接到Firestore。我需要执行此MySQL等效查询:
SELECT * FROM offer WHERE zip = '..' AND category = '...' AND status != 1 ORDER BY ..
到目前为止,我在Firebase查询下面进行了尝试:
const docRef = db.collection('ads');
await docRef.where('status', '<', 4).where('status', '>', 5).orderBy('status')
.where('zip', '==', searchTerm).where('subCategory', '==', subCategory).orderBy('createdAt')
此查询返回空数组。我刚接触Firebase。
答案 0 :(得分:0)
首先,CollectionReference
(来自db.collection(...)
)应命名为colRef
或类似名称。尽管它们类似于DocumentReference
,但它们的工作方式不同,含义也不同。
您在此行定义的查询中说:“查找状态必须小于4且大于5的文档,并按状态值对它们进行排序”。
colRef.where('status', '<', 4).where('status', '>', 5).orderBy('status')
由于status
的值不能同时大于5和小于4,所以没有结果。
如果要翻译SQL查询,则可以使用:
colRef.where('status', '>', 1).orderBy('status')
.where('zip', '==', searchTerm).where('subCategory', '==', subCategory).orderBy('createdAt');
这假定状态永远不会小于1。如果是,则还需要第二个单独的查询:
colRef.where('status', '<', 1).orderBy('status')
.where('zip', '==', searchTerm).where('subCategory', '==', subCategory).orderBy('createdAt')