通过时间戳查询Firestore中的数据

京东

我正在按时间戳查询文档,并且它返回一个空数组。但是,当我使用“ ==” ex:.where('date','==',timestamp)时,它可以工作,当我使用'> =''<='时,返回空数组

我也尝试过将时间戳转换为日期对象和字符串,但没有成功。

注意firestore中日期字段的类型为Timestamp

我正在查询集合中日期大于'2018-08-03'的文档。

下图是交易的集合(左)和文档(右)的图片,它们应该是返回的文档数组的一部分,因为日期大于“ 2018-08-03”

在此处输入图片说明

下面是我的代码。

  const firstDay = new Date('2018-08-03');
  const timestamp1 = admin.firestore.Timestamp.fromDate(firstDay);
  const trans = [];
  const docRef = db.collection('Users').doc(uid).collection('transactions').where('item_id', '==', item_id)
    .where('date', '>=', timestamp1);
  await docRef.get()
    .then((snapshot) => {
      snapshot.forEach((doc) => {
        trans.push({ transaction_id: doc.id, transaction: doc.data() });
      });
    })
    .catch(err => new Error('cannot get the documents', err));

预期结果:应该是交易日期大于上面指定的数组。

实际结果:空数组。

由于其对等式==起作用,因此我认为> =<=将起作用。我在这里想念什么吗?

Logs after trying ">" (equal to timestamp1)

Firestore (2.3.0) 2019-10-03T14:01:25.013Z AObRG [ClientPool.acquire]: Re-using existing client with 100 remaining operations
Firestore (2.3.0) 2019-10-03T14:01:25.015Z AObRG [Firestore.readStream]: Sending request: {"parent":"projects/valuemo-000/databases/(default)/documents/Users/Xmr3vKT19OSST02DSTDMt0jSq692","structuredQuery":{"from":[{"collectionId":"transactions"}],"where":{"compositeFilter":{"op":"AND","filters":[{"fieldFilter":{"field":{"fieldPath":"item_id"},"op":"EQUAL","value":{"stringValue":"zGRkpP3QkzI89zKyDjZ7FPDrXd5G3Bco5ENlR"}}},{"fieldFilter":{"field":{"fieldPath":"date"},"op":"EQUAL","value":{"timestampValue":{"seconds":1533254400}}}}]}}}}
Firestore (2.3.0) 2019-10-03T14:01:25.232Z AObRG [Firestore._initializeStream]: Received stream error: { Error: The query requires a COLLECTION_ASC index 
for collection transactions and field date. That index is not ready yet. See its status here: https://console.firebase.google.com/project/valuemo-000/database/firestore/indexes/single_field?create_exemption=ClJwcm9qZWN0cy92YWx1ZW1vLTAwMC9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvdHJhbnNhY3Rpb25zL2ZpZWxkcy9kYXRlEAEaCAoEZGF0ZRAB
    at Http2CallStream.call.on (D:\Projects\Valuemo-firebase\node_modules\@grpc\grpc-js\build\src\call.js:68:41)
    at Http2CallStream.emit (events.js:194:15)
    at process.nextTick (D:\Projects\Valuemo-firebase\node_modules\@grpc\grpc-js\build\src\call-stream.js:71:22)
    at process.internalTickCallback (internal/process/next_tick.js:70:11)
  code: 9,
  details:
   'The query requires a COLLECTION_ASC index for collection transactions and field date. That index is not ready yet. See its status here: https://console.firebase.google.com/project/valuemo-000/database/firestore/indexes/single_field?create_exemption=ClJwcm9qZWN0cy92YWx1ZW1vLTAwMC9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvdHJhbnNhY3Rpb25zL2ZpZWxkcy9kYXRlEAEaCAoEZGF0ZRAB',
  metadata: Metadata { options: undefined, internalRepr: Map {} } }
Firestore (2.3.0) 2019-10-03T14:01:25.256Z AObRG [Firestore._initializeStream]: Received initial error: { Error: The query requires a COLLECTION_ASC index for collection transactions and field date. That index is not ready yet. See its status here: https://console.firebase.google.com/project/valuemo-000/database/firestore/indexes/single_field?create_exemption=ClJwcm9qZWN0cy92YWx1ZW1vLTAwMC9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvdHJhbnNhY3Rpb25zL2ZpZWxkcy9kYXRlEAEaCAoEZGF0ZRAB
    at Http2CallStream.call.on (D:\Projects\Valuemo-firebase\node_modules\@grpc\grpc-js\build\src\call.js:68:41)
    at Http2CallStream.emit (events.js:194:15)
    at process.nextTick (D:\Projects\Valuemo-firebase\node_modules\@grpc\grpc-js\build\src\call-stream.js:71:22)
    at process.internalTickCallback (internal/process/next_tick.js:70:11)
  code: 9,
  details:
   'The query requires a COLLECTION_ASC index for collection transactions and field date. That index is not ready yet. See its status here: https://console.firebase.google.com/project/valuemo-000/database/firestore/indexes/single_field?create_exemption=ClJwcm9qZWN0cy92YWx1ZW1vLTAwMC9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvdHJhbnNhY3Rpb25zL2ZpZWxkcy9kYXRlEAEaCAoEZGF0ZRAB',
  metadata: Metadata { options: undefined, internalRepr: Map {} } }
Firestore (2.3.0) 2019-10-03T14:01:25.260Z AObRG [Firestore._retry]: Request failed with unrecoverable error: { Error: The query requires a COLLECTION_ASC index for collection transactions and field date. That index is not ready yet. See its status here: https://console.firebase.google.com/project/valuemo-000/database/firestore/indexes/single_field?create_exemption=ClJwcm9qZWN0cy92YWx1ZW1vLTAwMC9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvdHJhbnNhY3Rpb25zL2ZpZWxkcy9kYXRlEAEaCAoEZGF0ZRAB
    at Http2CallStream.call.on (D:\Projects\Valuemo-firebase\node_modules\@grpc\grpc-js\build\src\call.js:68:41)
    at Http2CallStream.emit (events.js:194:15)
    at process.nextTick (D:\Projects\Valuemo-firebase\node_modules\@grpc\grpc-js\build\src\call-stream.js:71:22)
    at process.internalTickCallback (internal/process/next_tick.js:70:11)
  code: 9,
  details:
   'The query requires a COLLECTION_ASC index for collection transactions and field date. That index is not ready yet. See its status here: https://console.firebase.google.com/project/valuemo-000/database/firestore/indexes/single_field?create_exemption=ClJwcm9qZWN0cy92YWx1ZW1vLTAwMC9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvdHJhbnNhY3Rpb25zL2ZpZWxkcy9kYXRlEAEaCAoEZGF0ZRAB',
  metadata: Metadata { options: undefined, internalRepr: Map {} } }
佩索拉托

因此,我进行了测试,认为与您介绍的设置非常相似,实际上我能够重现相同的行为。

在测试了各种可能性之后,我发现对我而言,这个问题与字段的数据类型无关,而与组合索引配置有关

使此查询起作用所需的复合索引配置如下:

在此处输入图片说明

根据文档的“复合索引支持查询”部分,此类复合查询的索引应首先具有相等过滤字段,虽然没有明确说明,但这是示例中文档所采用的方式。

让我知道这是否为您解决了问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Firestore - 通过时间戳查询不返回结果

通过时间戳分钟在postgres中查询

Firestore orderBy通过时间戳对象

Firestore订单通过时间戳DESC

SNS中的时间戳消息通过时

无法使用Table API在CosmosDB中通过时间戳查询

SQLite通过时间戳和值查询

R:通过时间戳查询MongoDB(使用mongolite)

通过时间戳对数据进行子集和绘图

Excel:通过时间戳平均传感器数据

Elasticsearch:通过时间戳过滤聚合

mysql通过时间戳获取最新记录而无需子查询

Firebase实时数据库:无法通过时间戳订购数据

RethinkDB-通过时间戳检索数据:不是TIME伪类型?

我正在尝试在 Firestore 中查询时间戳

在 Cloud Functions 中按时间戳从 Cloud Firestore 查询

如何根据ionic 4中的时间戳从firestore订购查询

通过时间戳在行和列名称中查找值的有效算法

熊猫通过时间戳的接近来分组

MS Access 2010 - 通过时间戳过滤时出错

通过时间戳将Pandas Dataframe分为多个块

通过时间戳和日期列联接表?

基础JSON文件中的时间戳格式已更改时,在Athena中查询时间戳数据

Firebase / firestore中的时间戳

通过时间戳日期的数据库 json 列表设置日期选择器的禁用日期

Oracle递归查询(比较分组数据中的时间戳)

Oracle中的时间戳数据类型查询

如何通过FCM发送Firestore时间戳?

首先通过时间戳值对熊猫中的行进行排序,然后对列的类别值进行特定排序