import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Accumulators;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.util.Arrays;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-context.xml"})
@WebAppConfiguration
public class MongoAggTest {
/**
* count
* */
@Test
public void count(){
Bson match = Aggregates.match(Filters.gte("age", 30));
Bson group = Aggregates.group("$province", Accumulators.sum("cnt", 1));
Bson sort = Aggregates.sort(new BasicDBObject("cnt", -1));
MongoCollection<Document> coll = null;//get your coll
MongoCursor<Document> iterator = coll.aggregate(Arrays.asList(match, group,sort)).iterator();
while (iterator.hasNext()){
Document next = iterator.next();
System.out.println(next.toJson());
}
}
/**
* sum
* */
@Test
public void sum(){
Bson match = Aggregates.match(Filters.gte("age", 30));
Bson group = Aggregates.group("$province", Accumulators.sum("cnt", "$age"));
Bson sort = Aggregates.sort(new BasicDBObject("cnt", -1));
MongoCollection<Document> coll = null;//get your coll
MongoCursor<Document> iterator = coll.aggregate(Arrays.asList(match, group,sort)).iterator();标签:group,iterator,mongodb,Aggregates,next,Accumulators,coll,import From: https://www.cnblogs.com/wangdonghua/p/17536630.html
while (iterator.hasNext()){
Document next = iterator.next();
System.out.println(next.toJson());
}
}
/**
* first聚合
* */
@Test
public void first(){
Bson match = Aggregates.match(Filters.gte("age", 30));
Bson group = Aggregates.group("$province", Accumulators.first("first", "$$ROOT"));
Bson sort = Aggregates.sort(new BasicDBObject("cnt", -1));
MongoCollection<Document> coll = null;//get your coll
MongoCursor<Document> iterator = coll.aggregate(Arrays.asList(match, group,sort)).iterator();
while (iterator.hasNext()){
Document next = iterator.next();
System.out.println(next.toJson());
}
}
/**
* 多字段聚合
* */
@Test
public void group()throws Exception{
Bson group = Aggregates.group(new BasicDBObject("age", "$age").append("province", "$province"),
Accumulators.sum("cnt", 1));
MongoCollection<Document> coll = null;// do yourself
MongoCursor<Document> iterator = coll.aggregate(Arrays.asList(group)).iterator();
while (iterator.hasNext()){
Document next = iterator.next();
System.out.println(next.toJson());
}
}
@Test
public void add2Set()throws Exception{
Bson group = Aggregates.group("$province", Accumulators.addToSet("nameSet", "$name"));
MongoCollection<Document> coll = null; //do yourself
MongoCursor<Document> iterator = coll.aggregate(Arrays.asList(group)).iterator();
while (iterator.hasNext()){
Document next = iterator.next();
System.out.println(next.toJson());
}
}
}