我正在开始使用 Mongo 中的 Changestream。在我当前的设置中,缝合函数将更改日志事件插入修订集合中。但是,当我从集合中读取数据时,无法转换时间戳字段。我尝试过以下 2 次尝试:
1)管道
[
{
$match: {
'documentKey._id': _id,
},
},
{
$sort: { _id: -1 },
},
{
$addFields: {
convertedDate: { $toDate: 'clusterTime' },
},
},
]
Run Code Online (Sandbox Code Playgroud)
但它给出了错误:Error parsing date string 'clusterTime'; 0: passing a time zone identifier as part of the string is not allowed 'c'; 6: Double timezone specification 'r'
2)bson时间戳类
import { Timestamp } from 'bson';
const asTimestampInstance = new Timestamp(v.clusterTime);
Run Code Online (Sandbox Code Playgroud)
但这里打字稿给了我错误:Expected 2 arguments, but got 1.ts(2554)
index.d.ts(210, 30): An argument for 'high' was not provided.
在 Altas 中,集群时间正确地看起来像时间戳:
我希望我只是错过了一些简单的东西:)
不幸的是$toDate不能直接使用时间戳。至少在 v4.0 中不是。
参数应该是数字、字符串或 ObjectId。
您需要首先将时间戳转换为字符串:
$addFields: {
convertedDate: { $toDate: {$dateToString:{date:"$clusterTime"}} },
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3386 次 |
| 最近记录: |