【问题标题】:Need help resolving firebase query需要帮助解决 firebase 查询
【发布时间】:2019-12-20 07:33:31
【问题描述】:

我的节点后端连接到 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 的新手。

【问题讨论】:

  • 您的查询正在检查小于 4 和大于 5 的状态。根据您的 MySQL 查询,我认为您想要 where('status', '!=', 1)
  • @jmalenfant Firebase 没有不等式('&lt;&gt;''!='operator,可用于 where(...)
  • @samthecodingman 奇怪。他可以只使用.query() 然后使用SQL,对吗?
  • 为了保持 Firestore 查询的性能,它们只是不受支持。看看this answer

标签: node.js firebase google-cloud-firestore


【解决方案1】:

首先,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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    相关资源
    最近更新 更多