仅使用Spring Boot和MongoDB比较日期的YYYY-MM部分。

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

Only compare YYYY-MM part of date using Springboot and MongoDB

问题

我遇到了关于MongoDB日期字段的问题。
在MongoDB集合中,我有一列是日期类型。它以ISO格式存储整个日期,就像下面这样。

2020-05-13T14:00:00.000

现在从我的API中,我传递了**2020-05,即YYYY-MM,并且我想在数据库中获取所有具有2020-05**值的记录。

我尝试过多种选项,如下所示。

  1. Date mydate = new Date();
  2. Criteria criteria = Criteria
  3. .where("date").is(mydate);

我在数据库中有以下数据。

|---------------------|------------------|------------------|

名称 ID 日期
A 1 2020-05-13T14:00:00
--------------------- ------------------ ------------------
B 2 2020-12-31T14:00:00
--------------------- ------------------ ------------------
C 3 2020-07-17T14:00:00
--------------------- ------------------ ------------------
D 4 2020-05-29T14:00:00
--------------------- ------------------ ------------------

如果我传递了**2020-05**,则应该返回

|---------------------|------------------|--------------------|

名称 ID 日期
A 1 2020-05-13T14:00:00
--------------------- ------------------ --------------------
D 4 2020-05-29T14:00:00
--------------------- ------------------ --------------------

在MongoDB中是否有类似于MySQL中的STR_TO_DATE()和Oracle中的TO_DATE()的功能?

我正在尝试在Spring Boot应用程序中实现此功能。

尝试上面的查询。

这将对您有所帮助。

英文:

I am facing issue with mongoDb date fields.
I have one column in MongoDB collect which is date type. Which store whole date in ISO format.
As like below.

2020-05-13T14:00:00.000

Now from my api i am passing 2020-05 means YYYY-MM and i want to fetch all records with 2020-05 value in db.

I had tried with multiple option as below.

  1. Date mydate=new Date();
  2. Criteria criterid = Criteria
  3. .where("date").is(mydate)

I have below data in DB

  1. |---------------------|------------------|------------------|
  2. | Name | ID | Date |
  3. |---------------------|------------------|------------------|
  4. | A | 1 |2020-05-13T14:00:00|
  5. |---------------------|------------------|------------------|
  6. | B | 2 |2020-12-31T14:00:00|
  7. |---------------------|------------------|------------------|
  8. | C | 3 |2020-07-17T14:00:00|
  9. |---------------------|------------------|------------------|
  10. | D | 4 |2020-05-29T14:00:00|
  11. |---------------------|------------------|------------------|
  12. enter code here

And if i will pass "2020-05" then it should returns

  1. |---------------------|------------------|--------------------|
  2. | Name | ID | Date |
  3. |---------------------|------------------|--------------------|
  4. | A | 1 |2020-05-13T14:00:00 |
  5. |---------------------|------------------|--------------------|
  6. | D | 4 |2020-05-29T14:00:00 |
  7. |---------------------|------------------|--------------------|

Is there anything in mongoDb like STR_TO_DATE() in MySQL and TO_DATE() in oracle`**.

I am trying to implement this in Spring boot application.

Try above query.

It will help you.

答案1

得分: 1

以下是翻译好的部分:

一种解决方案是这样的:

  1. db.collection.find({
  2. $expr: {
  3. $and: [
  4. { $eq: [{ $year: "$Date" }, 2020] },
  5. { $eq: [{ $month: "$Date" }, 5] }
  6. ]
  7. }
  8. })
英文:

One solution would be this one:

  1. db.collection.find({
  2. $expr: {
  3. $and: [
  4. { $eq: [{ $year: "$Date" }, 2020] },
  5. { $eq: [{ $month: "$Date" }, 5] }
  6. ]
  7. }
  8. })

huangapple
  • 本文由 发表于 2020年5月4日 19:41:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/61591421.html
匿名

发表评论

匿名网友

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

确定