2016-11-23 81 views
1

我有以下結構名稱討論。我想在用戶討論中獲取最後一條消息。我嘗試了以下不完整的Spring MongoDB查詢,請允許我告訴我如何僅根據討論獲取一條消息(按lastmodifieddate排序),或者找到用戶是對話中最後一條消息的接收者的討論。Mongodb Aggregate(Java/Spring)查詢獲取最後一個子元素

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" 
} 
+0

。這是一個討論的結構。來自用戶和用戶的消息表示該用戶是分別發送還是接收消息。我正在使用OR查詢來查找用戶發送或接收消息的消息。我對這個用戶收到最後一條消息的消息感興趣。 – Debopam

+0

雖然Veerams回答下面可能從技術角度解決您的問題,但針對您的問題的最佳解決方案寧願將最後一條消息冗餘保存在文檔中,並在每次添加新消息時通過$ set進行更新。如果可以,避免聚集。 – mtj

回答

2

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

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