gpt4 book ai didi

javascript - 按时间戳查询 firestore 中的数据

转载 作者:行者123 更新时间:2023-11-29 10:55:39 25 4
gpt4 key购买 nike

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

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

注意:firestore 中的date 字段是Timestamp 类型。

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

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

enter image description here

下面是我的代码。

  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 {} } }

最佳答案

所以我正在测试它,我认为它与您展示的设置非常相似,而且我实际上能够重现相同的行为。

在测试了各种可能性后,我发现我的问题与字段的数据类型无关,而是与复合索引的配置有关。

执行此查询所需的复合索引配置如下:

enter image description here

根据Queries supported by composite indexes在文档的部分,这些类型的复合查询的索引应该首先具有相等过滤字段,这没有明确说明,但文档在示例中就是这样呈现的。

如果这能为您解决问题,请告诉我。

关于javascript - 按时间戳查询 firestore 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58106160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com