在使用MongoOperations Spring Java保存日期至mongodb时出现错误。

huangapple go评论67阅读模式
英文:

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:

{
 &quot;modifiedon&quot;: {
 &quot;$dateFromString&quot;: {
     &quot;dateString&quot;: &quot;2017-02-08T12:10:40.787&quot;
  }
},
&quot;modifiedby&quot;: &quot;ramsha-GRM1 Ambreen&quot;,
&quot;id&quot;: 537
}

I expect 'modifiedOn' value should save as

&quot;modifiedon&quot;: ISODate(&quot;2017-02-08T12:10:40.78&quot;)

Here is a code to insert json into mongodb using java

String json = {&lt;JSONVALUE&gt;};
mongoOperations.insert(json,&quot;risk&quot;);

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

{
&quot;modifiedon&quot;: {
&quot;$date&quot;: &quot;2020-09-30T11:32:58.000Z&quot;
},
&quot;modifiedby&quot;: &quot;ramsha-GRM1 Ambreen&quot;,
&quot;id&quot;: 229
}

AND it is saved in MongoDB as date object and I am able to query against date

huangapple
  • 本文由 发表于 2020年9月30日 14:08:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64131788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定