我开发了一个将 JSON 保存在 MongoDb 中的 Spring 应用程序。 客户端能够通过输入时间间隔开始和结束来搜索数据。 为了能够使用正则表达式(lte 和 gte)从数据库中获取数据,我将时间保存为 UnixTimeStamp:
"TimeStamp": {
"StartTime": 1549371540000,
"EndTime" : 1549371540000,
"StartTimeZone":"Europe/Berlin",
"EndTimeZone":"Europe/Berlin"
}
过滤有效,但结果如下:
"TimeStamp": {
"StartTimeZone": "Europe/Berlin",
"EndTimeZone": "Europe/Berlin",
"EndTime": {
"$numberLong": "1549371540000"
},
"StartTime": {
"$numberLong": "1549371540000"
}
}
是否可以仅获取键值对而不是使用 $numberLong 键获取 StartTime 对象? 我也在考虑使用 yyyy_mm_ddTHH:MM:SS+-Offset 节省时间 aso Iso 格式
但据我所知,我只能使用正则表达式来过滤这种格式的时间。 处理时间是项目的重要组成部分,我想确保我以最佳实践方式处理这部分。
编辑: 在我的代码中我要
for (Document doc : collection.find(query).projection(Projections.include(projectionList))) {
//Save the query result as a List of JSON-Objects
dataList.add(doc.toJson());
}
将结果保存为 json 列表。我知道 toJson 方法已被弃用,但我找不到将文档转换为 JSON 表示法的方法。
最佳答案
Is it possible to just get the key-value pair instead of the StartTime Object with the $numberLong key?
JsonWriterSettings settings = JsonWriterSettings.builder()
.int64Converter((value, writer) -> writer.writeNumber(value.toString())).build();
document.toJson(settings); // The output would contain simple long value
I know that the toJson method is deprecated
@medTech toJson 方法未弃用,请引用文档 here 的官方文档.此外,对于 json writer settings点击链接。
关于java - 当我想以毫秒为单位获取 unix 时间时,Mongodb 时间戳输出格式返回 "$numberLong"对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57266367/