Heim > Fragen und Antworten > Hauptteil
@Test
public void testCount() throws Exception {
DynamicSqlParameter dsp = new DynamicSqlParameter();
long sT = System.currentTimeMillis();
MongoDatasource mongoDatasource = MongoDatasource.getInstance(mongoService.getDatasource());
DBCollection dbCollection = mongoDatasource.getDB().getCollection("dayFlow");
List arrayList = new ArrayList<>();
DBObject dbObject1 = new BasicDBObject();
dbObject1.put("usedDayFlow", 2);
DBObject dbObject2 = new BasicDBObject();
dbObject2.put("_id", null);
dbObject2.put("count", new BasicDBObject("$sum", 1));
arrayList.add(new BasicDBObject("$match", dbObject1));
arrayList.add(new BasicDBObject("$group", dbObject2));
System.out.println(JSON.serialize(arrayList));
AggregationOutput size = dbCollection.aggregate(arrayList);
System.out.println(size.results());
System.out.println("运行时间:" + ((System.currentTimeMillis() - sT) /1000) + "s");
}
[ { "$match" : { "usedDayFlow" : 2}} , { "$group" : { "_id" : null , "count" : { "$sum" : 1}}}]
[{ "_id" : null , "count" : 1002223}]
PHP中文网2017-04-18 10:53:35
public long getCount() {
String user = "用户名";
String database = "admin";
String password = "密码";
MongoCredential credential = MongoCredential.createCredential(user,database, password.toCharArray());
MongoClientOptions options = MongoClientOptions.builder()
.connectionsPerHost(10)
.threadsAllowedToBlockForConnectionMultiplier(10)
.socketTimeout(20000)
.connectTimeout(15000)
.maxWaitTime(50000)
.build();
MongoClient mongoClient = new MongoClient(new ServerAddress("IP地址", "端口"), Arrays.asList(credential), options);
MongoDatabase mongoDatabase = mongoClient.getDatabase("数据库");
MongoCollection<Document> collection = mongoDatabase.getCollection("数据表");
final long[] count = new long[1];
Block<Document> printBlock = new Block<Document>() {
@Override
public void apply(final Document document) {
count[0] = (long) document.get("count");
}
};
Bson bson = Filters.eq("categories", "Bakery");
collection.aggregate(
Arrays.asList(
Aggregates.match(bson),
Aggregates.group(null, Accumulators.sum
("count", 1L))
)
).forEach(printBlock);
return count[0];
}
阿神2017-04-18 10:53:35
能否补充一些评论中的信息。多谢!
将评论的内容转发在这里,方便查看:
1、count和aggregate的不同:在mongoDB中,count和aggregate是在两支不同的程序中实现的,aggregate的实现是考虑到了shard的环境的,所以官方文档是推荐使用aggregate来进行shard环境下的count。
2、MongoDB shell下使用aggregate和使用Java MongoDB驱动使用aggregate来进行count,结果应该是一样的,因为两者都是使用的aggregate。
您提到Issue大概是MongoDB shell和Java MongoDB驱动进行count的结果不一致。
这种不一致,我觉得可能是:
1)比较的过程有没有纰漏;
2)所用的Java MongoDB驱动是否有纰漏。
供参考。
Love MongoDB! Have Fun!
今晚8点,MongoDB中文社区大神在线讲座,请大家踊跃参与;此大神常驻本版哦!
请戳此链接。