首页 > 解决方案 > MongoDB 日期比较不适用于 $date

问题描述

我最近开始使用 MongoDB,所以我想我只是没有注意到一些简单的事情,所以,请礼貌:)

我在 mongo 中有一个名为“issueDoc”的集合。这就是它的实际样子

db.issueDoc.find({}).limit(1).pretty()
{
     "_id" : ObjectId("604b25906e26367c031f7520"),
     "createdAt" : ISODate("2020-06-14T00:00:00Z"),
     "content" : "some Issue",
     "companyId" : "12345",
     "issueStatus" : "APPROVED",
     "_class" : "com.example.mongodbaggregations.domain.Issue"
}

我正在尝试获取所有“issueDoc”元素,这些元素是某个特定公司在某个特定日期之后创建的。这似乎很简单。我在 Spring 框架中使用聚合(我打算进一步分组)。Spring 生成查询,如下所示:

db.issueDoc.aggregate([{
      "$match" : { 
             "companyId" : "12345",
             "createdAt" : { 
                 "$gte" : { 
                      "$date" : "2020-03-12T00:00:00Z"
                 }
             }
       }
}])

生成上述查询的代码本身是:

public void getAmountOfIssuesCreated(String companyId, Date leftBorderDate, String groupBy) {
        AggregationOperation matchAggregation = Aggregation.match(
                Criteria.where(IssueMongoFields.COMPANY_ID).is(companyId)
                        .and(IssueMongoFields.CREATED_AT).gte(leftBorderDate)
        );

        final Aggregation aggregation = Aggregation.newAggregation(
                matchAggregation
        );

        mongoTemplate.aggregate(aggregation, "issueDoc", ResponseForm.class);
    }

上面的这个查询(对我来说)似乎是有效的,尽管它总是不返回任何内容,无论传递的时间戳如何(我的意思是“createAt”字段的条件)。例如 - 假设我们在“issueDoc”集合中有以下元素:

> db.issueDoc.find({}).pretty()
{
    "_id" : ObjectId("604b25906e26367c031f7520"),
    "createdAt" : ISODate("2020-06-14T00:00:00Z"),
    "content" : "some Issue",
    "companyId" : "12345",
    "issueStatus" : "APPROVED",
    "_class" : "com.example.mongodbaggregations.domain.Issue"
}
{
    "_id" : ObjectId("604b25d96e26367c031f7521"),
    "createdAt" : ISODate("2020-05-25T00:00:00Z"),
    "content" : "some Issue",
    "companyId" : "12345",
    "issueStatus" : "DECLINED",
    "_class" : "com.example.mongodbaggregations.domain.Issue"
}
{
    "_id" : ObjectId("604b25fe6e26367c031f7522"),
    "createdAt" : ISODate("2020-04-25T00:00:00Z"),
    "content" : "some Issue",
    "companyId" : "12345",
    "issueStatus" : "PENDING",
    "_class" : "com.example.mongodbaggregations.domain.Issue"
}

正如我已经提到的,我们现在看到的查询根本不会返回任何内容。但是我自己尝试了另一个(我的个人查询位于下面)

> db.issueDoc.aggregate([{ "$match" : { "companyId" : "12345", "createdAt" : { "$gte" : ISODate("2020-04-26T00:00:00Z")}}}]).pretty()
{
    "_id" : ObjectId("604b25906e26367c031f7520"),
    "createdAt" : ISODate("2020-06-14T00:00:00Z"),
    "content" : "some Issue",
    "companyId" : "12345",
    "issueStatus" : "APPROVED",
    "_class" : "com.example.mongodbaggregations.domain.Issue"
}
{
    "_id" : ObjectId("604b25d96e26367c031f7521"),
    "createdAt" : ISODate("2020-05-25T00:00:00Z"),
    "content" : "some Issue",
    "companyId" : "12345",
    "issueStatus" : "DECLINED",
    "_class" : "com.example.mongodbaggregations.domain.Issue"
}
> 

如您所见,此查询运行良好。那么,第一个查询到底有什么问题?(这是由 Spring 生成的)。顺便说一句,这就是我的“issueDoc”域模型的样子:

@Document(collection = "issueDoc")
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Issue {

    @MongoId(FieldType.OBJECT_ID)
    private ObjectId id;

    @Indexed
    @Field(name = IssueMongoFields.CREATED_AT)
    private Date createdAt;

    @Field(name = IssueMongoFields.CONTENT)
    private String content;

    @Field(name = IssueMongoFields.COMPANY_ID)
    private String companyId;

    @Field(name = IssueMongoFields.ISSUE_STATUS)
    private IssueStatus issueStatus;

    @Field(name = IssueMongoFields.CREATOR)
    private User creator;
}

我想我只是不明白一些很简单的事情。感谢任何帮助

标签: mongodbaggregation-frameworkspring-mongodb

解决方案


推荐阅读