类型MongoCollection 中的方法聚合(List <?extends Bson>)不适用于参数(BasicDBObject)
问题描述:
我是MongoDB的新手。我在loginCollection.aggregate
得到一个错误,指出:类型MongoCollection <Document>中的方法聚合(List <?extends Bson>)不适用于参数(BasicDBObject)
在类型MongoCollection的方法汇总(表)不适用的参数(BasicDBObject)
以下是我的代码片段。提前致谢。
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;
}
答
这是一个有点棘手回答这个不知道,但是你正在使用MongoDB的Java驱动程序是什么版本?
由于在某个时候在2.x训练aggregate()
方法已经接受了List
。例如:
// 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")))
));