1

MongoDBを初めて使用しています。私のコードスニペットが続きタイプMongoCollection <Document>の集計メソッド(List <?extends Bson>)は、引数(BasicDBObject)には適用されません

をタイプするMongoCollectionのメソッド集計(リスト)の引数には適用されません

を(BasicDBObjectを):私が述べloginCollection.aggregateでエラーを取得しています。前もって感謝します。

public MonthlyLoginCount monthlyLoginCount() { 

    MonthlyLoginCount monthlyLoginCount = new MonthlyLoginCount(); 
    Map<String, Long> map = new HashMap<String, Long>(); 
    MongoClient mongo = new MongoClient(dataSource, 27017); 
    MongoCollection<Document> loginCollection = mongo.getDatabase(mongoDataBase).getCollection(loginDetailsCollection); 

    AggregationOutput logincount = loginCollection.aggregate(new BasicDBObject("$group", 
      new BasicDBObject("_id", "$email_id").append("value", new BasicDBObject("$push", "$value")))); 
    Iterator<DBObject> results = logincount.results().iterator(); 

    while (results.hasNext()) { 
     try { 
      Object str = results.next().get("_id"); 

      long count = loginCollection.count(new BasicDBObject("email_id", str.toString())); 

      System.out.println("email id:: " + str.toString() + " count: " + count); 
      map.put(str.toString(), count); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    mongo.close(); 
    monthlyLoginCount.setMap(map); 
    return monthlyLoginCount; 
} 

答えて

0

いつか2.xの電車の中でaggregate()方法はListを受け入れたので、それは...あなたはしかし、使用しているMongoDBのJavaドライバのバージョンを知らなくても、これを答えるために

少しトリッキーです。例えば:

// in 2.14 
AggregationOutput aggregate(List<DBObject> pipeline) 

// in 3.x 
AggregateIterable<TDocument> aggregate(List<? extends Bson> pipeline); 

唯一の引数リストは、凝集パイプラインのステージを表し、Listあります。たとえば:

AggregateIterable<Document> documents = collection.aggregate(Arrays.asList(
     new Document("$match", theMatchDocument), 
     new Document("$project", theProjectionDocument) 
)); 

例外メッセージは、あなたの質問に含ま:

"タイプはMongoCollectionのメソッド集計(リスト)の引数(BasicDBObject)には適用されません"

... aggregate(List)を呼び出して、その番号をAggregationOutputに割り当てると、あなたはv2.1xを使用していると思われます(API docsを参照してください)。あなたの質問は次のように書き直すことができます:

AggregationOutput logincount = loginCollection.aggregate(Arrays.asList(
     new BasicDBObject("$group", new BasicDBObject("_id", "$email_id").append("value", new BasicDBObject("$push", "$value"))) 
)); 
関連する問題