1

大家好我在查詢我的數據時遇到很大問題。我有這樣的文件:mongo和spring-data-mongo中的聚合查詢

{ 
    "_id" : NumberLong(999789748357864), 
    "text" : "#asd #weila #asd2 welcome in my house", 
    "date" : ISODate("2016-12-13T21:44:37.000Z"), 
    "dateString" : "2016-12-13", 
    "hashtags" : [ 
     "asd", 
     "weila", 
     "asd2" 
    ] 
} 

,我想建立兩個查詢:

1)計算的每一天井號標籤的數量,並得到了例如這樣的事情:

{_id:"2016-12-13", 
hashtags:[ 
{hashtag:"asd",count:20}, 
{hashtag:"weila",count:18}, 
{hashtag:"asd2",count:10}, 
.... 
] 
} 

{_id:"2016-12-14", 
hashtags:[ 
{hashtag:"asd",count:18}, 
{hashtag:"asd2",count:14}, 
{hashtag:"weila",count:10}, 
.... 
] 
} 

2)另一個是相同的,但我想設置從2016-12-13到2016-12-17期間。

對於第一個我寫這個查詢,我得到了我搜索的內容,但在Spring Data Mongo中我不知道該怎麼寫。

db.comment.aggregate([ 
{$unwind:"$hashtags"}, 
{"$group":{ 
    "_id":{ 
     "date" : "$dateString", 
     "hashtag": "$hashtags" 
    }, 
    "count":{"$sum":1} 
    } 
}, 
{"$group":{ 
    "_id": "$_id.date", 
    "hashtags": { 
     "$push": { 
     "hashtag": "$_id.hashtag", 
     "count": "$count" 
    }}, 
    "count": { "$sum": "$count" } 
}}, 
{"$sort": { count: -1}}, 
{"$unwind": "$hashtags"}, 
{"$sort": { "count": -1, "hashtags.count": -1}}, 
{"$group": { 
     "_id": "$_id", 
     "hashtags": { "$push": "$hashtags" }, 
     "count": { "$first": "$count" } 
    }}, 
{$project:{name:1,hashtags: { $slice: ["$hashtags", 2 ]}}} 
]); 

回答

0

,您仍然可以使用相同的聚合操作減去第二階段小組賽之後,但在過濾方面的流水線步驟的一小部分,你不得不在初始$match管道步推出日期範圍查詢。

以下蒙戈殼例子 展示如何過濾聚集體用於特定日期範圍:

1)設置的期間從2016年12月13日2016年12月14日到:

var startDate = new Date("2016-12-13"); 
startDate.setHours(0,0,0,0); 

var endDate = new Date("2016-12-14"); 
endDate.setHours(23,59,59,999); 
var pipeline = [ 
    { 
     "$match": { 
      "date": { "$gte": startDate, "$lte": endDate } 
     } 
    } 
    { "$unwind": "$hashtags" }, 
    { 
     "$group": { 
      "_id": { 
       "date": "$dateString", 
       "hashtag": "$hashtags" 
      }, 
      "count": { "$sum": 1 } 
     } 
    }, 
    { 
     "$group": { 
      "_id": "$_id.date", 
      "hashtags": { 
       "$push": { 
        "hashtag": "$_id.hashtag", 
        "count": "$count" 
       } 
      } 
     } 
    } 
] 
db.comment.aggregate(pipeline) 

2)設置的期間從2016年12月13日到二○一六年十二月一十七日:

var startDate = new Date("2016-12-13"); 
startDate.setHours(0,0,0,0); 

var endDate = new Date("2016-12-17"); 
endDate.setHours(23,59,59,999); 
// run the same pipeline as above but with the date range query set as required 

彈簧數據等效(未測試):

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; 

Aggregation agg = newAggregation(
    match(Criteria.where("date").gte(startDate).lte(endDate)), 
    unwind("hashtags"), 
    group("dateString", "hashtags").count().as("count"), 
    group("_id.dateString") 
     .push(new BasicDBObject 
      ("hashtag", "$_id.hashtags").append 
      ("count", "$count") 
     ).as("hashtags") 
); 
AggregationResults<Comment> results = mongoTemplate.aggregate(agg, Comment.class); 
List<Comment> comments = results.getMappedResults(); 
+0

謝謝您的回答@chridam,我意識到同樣的事情,工作得很好,但是這種解決方案我必須做的每一天一個查詢。情況2是完美的。你知道如何在Spring Data中分割哈希標籤數組來獲得前十個元素嗎?在mongo中,我對count進行排序,並在做了一個投影之後進行排序[{$ project:{name:1,hashtags:{$ slice:[「$ hashtags」,2]}}}'但是我沒有使用$ slice 。我需要對內部結構進行排序並限制它,但在Spring Data mongo中,我不知道該怎麼做,任何建議? – dantavo

+0

在聚集查詢 – Veeram

+0

中加入這個'project(「name」)。和(「hashtags」).project(「slice」,2))'謝謝@SagarReddy,我以一種奇怪的方式解決了這個問題,但結果是正確的。我在聚合函數中的第二組步驟之前放了一個限制(2),但我會改變這個指令,使其與你一樣;) – dantavo