英文:
Error while saving date in mongodb using MongoOperations Spring Java
问题
我需要在MongoDB中保存带有日期的JSON对象,这里是示例JSON:
{
"modifiedon": {
"$dateFromString": {
"dateString": "2017-02-08T12:10:40.787"
}
},
"modifiedby": "ramsha-GRM1 Ambreen",
"id": 537
}
我期望 'modifiedOn' 的值应该保存为:
"modifiedon": ISODate("2017-02-08T12:10:40.78")
以下是使用Java将JSON插入MongoDB的代码示例:
String json = {<JSONVALUE>};
mongoOperations.insert(json, "risk");
但是MongoOperations在保存JSON时没有处理 $dateFromString,该值在MongoDB中格式化为日期。非常感谢您提供任何线索。
英文:
I need to save json object in mongodb having dates, here is sample json:
{
"modifiedon": {
"$dateFromString": {
"dateString": "2017-02-08T12:10:40.787"
}
},
"modifiedby": "ramsha-GRM1 Ambreen",
"id": 537
}
I expect 'modifiedOn' value should save as
"modifiedon": ISODate("2017-02-08T12:10:40.78")
Here is a code to insert json into mongodb using java
String json = {<JSONVALUE>};
mongoOperations.insert(json,"risk");
But MongoOperations save JSON without process $dateFromString which is a date formatted in MongoDB. Any leads are highly appreciated thanks
答案1
得分: 0
在尝试之后,上述问题已通过修改输入的 JSON 对象来解决:
{
"modifiedon": {
"$date": "2020-09-30T11:32:58.000Z"
},
"modifiedby": "ramsha-GRM1 Ambreen",
"id": 229
}
而且它以日期对象的形式保存在 MongoDB 中,我可以针对日期进行查询。
英文:
after hit an trial above issue has been resolved by modifying input JSON object as
{
"modifiedon": {
"$date": "2020-09-30T11:32:58.000Z"
},
"modifiedby": "ramsha-GRM1 Ambreen",
"id": 229
}
AND it is saved in MongoDB as date object and I am able to query against date
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论