簡體   English   中英

Mongodb Aggregate(Java / Spring)查詢以獲取最后一個子元素

[英]Mongodb Aggregate (Java/Spring) query to fetch last subelement

我有以下結構名稱“討論”。 我想獲取用戶討論中的最后一條消息。 我嘗試遵循不完整的Spring MongoDB查詢,能否讓我知道如何在每個討論中僅獲取一條消息(按lastmodifieddate排序),或在用戶是對話中最后一條消息的接收者的位置找到討論。

Aggregation aggr = newAggregation(
            match(Criteria.where("participants").regex(Pattern.compile(userid))),
                unwind("messages"),
                match(new Criteria().orOperator(Criteria.where("messages.touserId").is(userid),Criteria.where("messages.fromuserId").is(userid))),
                sort(Direction.DESC, "messages.lastModifiedDate"),
                group("_id").push("messages").as("messages"),
                project("_id","messages")
                );

{
  "_id": {
    "$oid": "57c2d7c8e4b0bcf181b7db0a"
  },
  "_class": "xxxxx",
  "participants": [
    "56893b22e4b0e8d1c6a25783",
    "56893bb6e4b0e8d1c6a25785",
    "577c2f6ee4b09ccb44d14415"
  ],
  "messages": [
    {
      "_id": {
        "$oid": "57c2d7c8e4b0bcf181b7db08"
      },
      "fromuserId": "577c2f6ee4b09ccb44d14415",
      "fromuser": "xxxx",
      "touserId": "56893b22e4b0e8d1c6a25783",
      "touser": "Bloreshop1",
      "message": "Check Product Price",
      "isMute": false,
      "index": 1,
      "createDate": {
        "$date": "2016-08-28T12:23:36.037Z"
      },
      "lastModifiedDate": {
        "$date": "2016-08-28T12:23:36.037Z"
      },
      "createdBy": "xxxx",
      "lastModifiedBy": "xxxxx"
    },
    {
      "_id": {
        "$oid": "57c2d7c8e4b0bcf181b7db09"
      },
      "fromuserId": "577c2f6ee4b09ccb44d14415",
      "fromuser": "xxxxxx",
      "touserId": "56893bb6e4b0e8d1c6a25785",
      "touser": "Bloreshop2",
      "message": "Check Product Price",
      "isMute": false,
      "index": 2,
      "createDate": {
        "$date": "2016-08-28T12:23:36.302Z"
      },
      "lastModifiedDate": {
        "$date": "2016-08-28T12:23:36.302Z"
      },
      "createdBy": "xxxxx",
      "lastModifiedBy": "xxx"
    }
  ],
  "discussionTopic": "Check Product Price",
  "messageCount": 2,
  "createDate": {
    "$date": "2016-08-28T12:23:36.318Z"
  },
  "lastModifiedDate": {
    "$date": "2016-08-28T12:23:36.318Z"
  },
  "createdBy": "xxxx",
  "lastModifiedBy": "xxxxx"
}

您正在尋找$ slice(aggregation) 當前版本1.9.5不支持slice。

Aggregation aggr = newAggregation(
        match(Criteria.where("participants").regex(Pattern.compile(userid))),
        unwind("messages"),
        match(new Criteria().orOperator(Criteria.where("messages.touserId").is(userid), Criteria.where("messages.fromuserId").is(userid))),
        sort(Direction.DESC, "messages.lastModifiedDate"),
        group("_id").push("messages").as("messages"),
        project("_id").and("messages").project("slice", 1));

未發行版本(2.x)支持切片

Aggregation aggr = newAggregation(
        match(Criteria.where("participants").regex(Pattern.compile(userid))),
        unwind("messages"),
        match(new Criteria().orOperator(Criteria.where("messages.touserId").is(userid), Criteria.where("messages.fromuserId").is(userid))),
        sort(Direction.DESC, "messages.lastModifiedDate"),
        group("_id").push("messages").as("messages"),
        project("_id").and("messages").slice(1));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM