使用Spring / Java

时间:2016-06-03 14:49:33

标签: java mongodb spring-data-mongodb

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

{
  "_id": {
    "$oid": "575161ea02758f067057b8a8"
  },
  "_class": "com.idearealty.product.shopchat.persistence.model.Discussion",
  "participants": "56d314a8e4b04d7f98cfd0c6,56d5d48ee4b0cc330f512a47,56d9d599e4b0cc330f512aaa,57130299e4b08c554c1092c7,56841002eceefce22f455c7f",
  "messages": [
    {
      "_id": {
        "$oid": "575161e802758f067057b8a4"
      },
      "formuserId": "56841002eceefce22f455c7f",
      "fromuser": "9674642375",
      "touserId": "56d314a8e4b04d7f98cfd0c6",
      "touser": "debopam_r",
      "message": "User Creating Discussion",
      "isMute": false,
      "index": 1,
      "createDate": {
        "$date": "2016-06-03T10:54:32.428Z"
      },
      "lastModifiedDate": {
        "$date": "2016-06-03T10:54:32.428Z"
      },
      "createdBy": "9674642375",
      "lastModifiedBy": "9674642375"
    },
    {
      "_id": {
        "$oid": "575161e902758f067057b8a5"
      },
      "formuserId": "56841002eceefce22f455c7f",
      "fromuser": "9674642375",
      "touserId": "56d5d48ee4b0cc330f512a47",
      "touser": "Raushan",
      "message": "User Creating Discussion",
      "isMute": false,
      "index": 2,
      "createDate": {
        "$date": "2016-06-03T10:54:33.006Z"
      },
      "lastModifiedDate": {
        "$date": "2016-06-03T10:54:33.006Z"
      },
      "createdBy": "9674642375",
      "lastModifiedBy": "9674642375"
    },
    {
      "_id": {
        "$oid": "575161e902758f067057b8a6"
      },
      "formuserId": "56841002eceefce22f455c7f",
      "fromuser": "9674642375",
      "touserId": "56d9d599e4b0cc330f512aaa",
      "touser": "anirbanshop1",
      "message": "User Creating Discussion",
      "isMute": false,
      "index": 3,
      "createDate": {
        "$date": "2016-06-03T10:54:33.572Z"
      },
      "lastModifiedDate": {
        "$date": "2016-06-03T10:54:33.572Z"
      },
      "createdBy": "9674642375",
      "lastModifiedBy": "9674642375"
    },
    {
      "_id": {
        "$oid": "575161ea02758f067057b8a7"
      },
      "formuserId": "56841002eceefce22f455c7f",
      "fromuser": "9674642375",
      "touserId": "57130299e4b08c554c1092c7",
      "touser": "dummyshop",
      "message": "User Creating Discussion",
      "isMute": false,
      "index": 4,
      "createDate": {
        "$date": "2016-06-03T10:54:34.208Z"
      },
      "lastModifiedDate": {
        "$date": "2016-06-03T10:54:34.208Z"
      },
      "createdBy": "9674642375",
      "lastModifiedBy": "9674642375"
    }
  ],
  "productId": "56841004eceefce22f455c9b",
  "product": {
    "_id": {
      "$oid": "56841004eceefce22f455c9b"
    },
    "category": "Services",
    "productName": "Driving School",
    "imageurl": "service_icon.png",
    "createDate": {
      "$date": "2015-12-30T17:10:28.644Z"
    },
    "lastModifiedDate": {
      "$date": "2015-12-30T17:10:28.644Z"
    },
    "createdBy": "UnAuntenticatedUser",
    "lastModifiedBy": "UnAuntenticatedUser"
  },
  "userToRetailer": {
    "57130299e4b08c554c1092c7": {
      "_id": {
        "$oid": "5713029ae4b08c554c1092c8"
      },
      "shopName": "dummyshop",
      "user": {
        "$ref": "IdeaRealtyUser",
        "$id": {
          "$oid": "57130299e4b08c554c1092c7"
        }
      }
    },
    "56d314a8e4b04d7f98cfd0c6": {
      "_id": {
        "$oid": "56d314a9e4b04d7f98cfd0c7"
      },
      "shopName": "Test Shop",
      "user": {
        "$ref": "IdeaRealtyUser",
        "$id": {
          "$oid": "56d314a8e4b04d7f98cfd0c6"
        }
      }
    },
    "56d9d599e4b0cc330f512aaa": {
      "_id": {
        "$oid": "56d9d59ae4b0cc330f512aab"
      },
      "shopName": "anirbanshop1",
      "user": {
        "$ref": "IdeaRealtyUser",
        "$id": {
          "$oid": "56d9d599e4b0cc330f512aaa"
        }
      }
    },
    "56d5d48ee4b0cc330f512a47": {
      "_id": {
        "$oid": "56d5d48fe4b0cc330f512a48"
      },
      "shopName": "Kolkata Shop1",
      "user": {
        "$ref": "IdeaRealtyUser",
        "$id": {
          "$oid": "56d5d48ee4b0cc330f512a47"
        }
      }
    }
  },
  "messageCount": 4,
  "createDate": {
    "$date": "2016-06-03T10:54:34.215Z"
  },
  "lastModifiedDate": {
    "$date": "2016-06-03T10:54:34.215Z"
  },
  "createdBy": "9674642375",
  "lastModifiedBy": "9674642375"
}

我正在使用以下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;
    }

1 个答案:

答案 0 :(得分:0)

我正在使用其他文档中的Date字段。将其更改为Calendar解决了问题。

// 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)