2016-11-22 32 views
1

我有我的MongoDB的文檔結構GROUPBY一個字段值和Sortby另一個字段,如下圖所示:如何在MongoDB中

{ 
     "_id" : NumberLong(366), 
     "_class" : "com.cts.adpart.domain.DBData", 
     "file" : "xyz", 
     "meta" : { 
         "owner" : "user123", 
     }, 
     "curFlag" : true 
} 
  1. 我要檢查它有curFlag真的
  2. 一個特殊的文件所有者可以處理n沒有文件,所以我必須GroupBy所有者並顯示特定用戶正在處理的文件的數量。
  3. 最後根據每個用戶正在處理的文件數量,我必須顯示處理大號的用戶。的文件。

對於上述說明,我寫了下面的代碼。

Map<String, Object> dbObjIdMap = new HashMap<String, Object>(); 
dbObjIdMap.put("owner", "$meta.owner"); 
dbObjIdMap.put("curFlag", "$curFlag"); 
DBObject groupFields = new BasicDBObject("_id", new BasicDBObject(dbObjIdMap)); 

groupFields.put("count", new BasicDBObject("$sum", 1)); 
DBObject group = new BasicDBObject("$group", groupFields); 

而且它給我像下面的輸出:

{ "_id" : { "owner" : "[email protected]", "curFlag" : true }, "count" : 1 } 
{ "_id" : { "owner" : "[email protected]", "curFlag" : false }, "count": 1 } 

但我希望有老闆對他們來說,curFlag是真正的唯一的方式,我一定要顯示主人處理多個文件的數量。

任何人都可以幫我解決這個問題嗎?

回答

1

翻譯您的需求轉化爲代碼,你會得到下面的管道(蒙戈外殼版本):

// 1. Check for documents which have curFlag true 
var match = { "$match": { "curFlag": true } }; 

// 2. GroupBy owner and display the number of files that particular user is handling 
var group = { 
    "$group": { 
     "_id": "$meta.owner", 
     "count": { "$sum": 1 } 
    } 
}; 

// 3. Display the user who is handling large no. of files 
var sort = { "$sort": { "count": -1 } }, 
    limit = { "$limit": 1 }; 

// Run pipeline 
db.mycollection.aggregate([match, group, sort, limit]) 

Java實現:

public class JavaAggregation { 
    public static void main(String args[]) throws UnknownHostException { 

     MongoClient mongo = new MongoClient(); 
     DB db = mongo.getDB("test"); 

     DBCollection coll = db.getCollection("mycollection"); 

     // 1. Check for documents which have curFlag true 
     DBObject match = new BasicDBObject("$match", 
          new BasicDBObject("curFlag", true) 
         ); 

     // 2. GroupBy owner and display the number of files per user 
     DBObject groupFields = new BasicDBObject("_id", "$meta.owner"); 
     groupFields.put("count", new BasicDBObject("$sum", 1)); 
     DBObject group = new BasicDBObject("$group", groupFields); 

     // 3. Display the user who is handling large no. of files 
     DBObject sort = new BasicDBObject("$sort", 
          new BasicDBObject("count", -1) 
         ); 
     DbObject limit = new BasicDBObject("$limit", 1); 

     List<DBObject> pipeline = Arrays.asList(match, group, sort, limit); 

     AggregationOutput output = coll.aggregate(pipeline); 

     for (DBObject result : output.results()) { 
      System.out.println(result); 
     } 
    } 
} 
+0

或者與v3的驅動程序:AggregateIterable RES =文檔.aggregate(Arrays.asList(Aggregates.match(Filters.eq(「curFlag」,true)),Aggregates.sort(Sorts.orderBy(Sorts.descending(「count」))),Aggregates.group(「$ meta。所有者「,Accumulators.sum(」count「,」1「)),Aggregates.limit(1))); //格式化爲讀者,因爲這是一條評論,而不是一個答案;-) – mtj

+0

不知道集合中的數據,我無法確切知道爲什麼它只返回一個用戶。你可以編輯你的問題,以顯示更多的示例文件與不同的用戶? – chridam