使用Spring/Java的Mongodb按日期字段查询

使用Spring/Java的Mongodb按日期字段查询,java,mongodb,spring-data-mongodb,Java,Mongodb,Spring Data Mongodb,我有以下Mongodb文档。要获取参与者='xxx'和message.lastmodifiedDate>dt以及(message.touserid='xxx'或message.fromuserid='xxx')的文档 我使用以下java代码获取数据,并将其转换为查询 { "aggregate" : "discussion" , "pipeline" : [ { "$match" : { "participants" : { "$regex" : "56d314a8e4b04d7f98cfd0c6

我有以下Mongodb文档。要获取参与者='xxx'和message.lastmodifiedDate>dt以及(message.touserid='xxx'或message.fromuserid='xxx')的文档

我使用以下java代码获取数据,并将其转换为查询

{ "aggregate" : "discussion" , "pipeline" : [ { "$match" : { "participants" : { "$regex" : "56d314a8e4b04d7f98cfd0c6"}}} , { "$unwind" : "$messages"} , { "$match" : { "$and" : [ { "messages.lastModifiedDate" : { "$gte" : { "$date" : "2016-02-28T16:06:11.960Z"}}} , { "$or" : [ { "messages.touserId" : "56d314a8e4b04d7f98cfd0c6"} , { "messages.formuserId" : "56d314a8e4b04d7f98cfd0c6"}]}]}} , { "$sort" : { "messages.lastModifiedDate" : -1}} , { "$skip" : 0} , { "$limit" : 10} , { "$group" : { "_id" : { "_id" : "$_id" , "productId" : "$productId"} , "data" : { "$push" : "$messages"}}} , { "$project" : { "productId" : "$_id.productId" , "data" : 1}}]}
但是这个查询没有任何记录。如果我将日期更改为ISODate,它将获取预期结果

db.discussion.aggregate( [{ "$match" : { "participants" : { "$regex" : "56841002eceefce22f455c7f"}}} , { "$unwind" : "$messages"}, { "$match" : { "$and" : [ { "messages.lastModifiedDate" : { "$gte" : ISODate("2016-02-28T16:38:48.632Z")}} , { "$or" : [ { "messages.touserId" : "56841002eceefce22f455c7f"} , { "messages.formuserId" : "56841002eceefce22f455c7f"}]}]}}]);
您能告诉我需要做哪些更改,以便它使用ISODate获取文档,或者在插入文档时使用普通的java.util.Date对象吗?我也尝试了注释代码,但没有成功

public List<Discussion> findInbox(String userid,Date lastloginDate,int skip, int limit){

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");


        Aggregation aggr = newAggregation(
                match(Criteria.where("participants").regex(Pattern.compile(userid))),
                unwind("messages"),
                match(new Criteria().andOperator(Criteria.where("messages.lastModifiedDate").gte(lastloginDate),new Criteria().orOperator(Criteria.where("messages.touserId").is(userid),Criteria.where("messages.formuserId").is(userid)))),
                //match(new Criteria().andOperator(Criteria.where("messages.lastModifiedDate").is(new BasicDBObject("$gte","ISODate("+format.format(lastloginDate)+")")),new Criteria().orOperator(Criteria.where("messages.touserId").is(userid),Criteria.where("messages.formuserId").is(userid)))),
                sort(Direction.DESC, "messages.lastModifiedDate"), 
                skip(skip),
                limit(limit),
                group("_id","productId").push("messages").as("data"),

                project("_id","productId","data")
                //project("product","participants","messages")
                );

        AggregationResults<Discussion> results = mongoTemplate.aggregate(aggr, "discussion", Discussion.class);
        List<Discussion> discussions = results.getMappedResults();

        return discussions;
    }
public List findInbox(字符串userid、Date lastloginandate、int skip、int limit){
SimpleDataFormat=新的SimpleDataFormat(“yyyy-MM-dd'T'HH:MM:ss.SSS'Z'”);
聚合aggr=newAggregation(
匹配(Criteria.where(“参与者”).regex(Pattern.compile(userid)),
释放(“消息”),
匹配(新条件().andOperator(条件.where(“messages.lastModifiedDate”).gte(lastloginDate)、新条件().orOperator(条件.where(“messages.touserId”).is(userid)、条件.where(“messages.formuserId”).is(userid)),
//匹配(new Criteria().andOperator(Criteria.where(“messages.lastModifiedDate”).is(new BasicDBObject($gte)、“ISODate”(+format.format(lastloginDate)+”)、新Criteria().orOperator(Criteria.where(“messages.touserId”).is(userid)、Criteria.where(“messages.formuserId”).is(userid)),
排序(Direction.DESC,“messages.lastModifiedDate”),
跳过(跳过),
限制(limit),,
组(“_id”,“productId”).push(“消息”).as(“数据”),
项目(“\u id”、“productId”、“data”)
//项目(“产品”、“参与者”、“信息”)
);
AggregationResults=mongoTemplate.aggregate(aggr,“讨论”,discussion.class);
列表讨论=结果。getMappedResults();
返回讨论;
}

我使用了另一个文档中的
日期
字段。将其更改为日历解决了问题

// Calling method
Calendar cal = Calendar.getInstance();
cal.setTime(loginInfo.getCreateDate());

return customDiscussionRepository.findInbox(activeUser.getId(), cal.getTime(), pageNumber > 0?pageSize*(pageNumber-1):0, pageSize);



// Repository (lastloginDate is the method argument passed from calling method).
Criteria.where("messages.lastModifiedDate").gte(lastloginDate)

我正在使用另一个文档中的
日期
字段。将其更改为日历解决了问题

// Calling method
Calendar cal = Calendar.getInstance();
cal.setTime(loginInfo.getCreateDate());

return customDiscussionRepository.findInbox(activeUser.getId(), cal.getTime(), pageNumber > 0?pageSize*(pageNumber-1):0, pageSize);



// Repository (lastloginDate is the method argument passed from calling method).
Criteria.where("messages.lastModifiedDate").gte(lastloginDate)