java Mongondb aggregate function & deduplication

1  deduplication:
 2  
. 3  we can go to a given field by re-distinct methods, but also in the mysql keyword
 . 4  
. 5 DBObject Query = new new BasicDBObject ();
 . 6 mongoTemplate.getCollection ( "collectionName") distinct ( "de-duplication field. name " , Query)
 . 7  
. 8  
. 9  packets of polymerization:
 10  
. 11  using a number of different values for the field aggregation statistics, for example, as follows:
 12 is  
13 is  statistics field of Content, alias value
 14  
15 aggregation aggregation = Aggregation.newAggregation (Aggregation.group ( "Content") COUNT () AS ( "value".. ));
 16  // query 
17 Criteria Criteria = new new . Criteria () the WHERE ( "a condition").is ( "a condition");
18 criteria.and("条件二").is("条件二");
19 aggregation.match(criteria);List<Map> maps = mongoTemplate.getCollection("collectionName").aggregate(aggregation);
20 
21 
22 另一种写法
23 
24 Criteria criteria = new Criteria().where("条件一").is("条件一");
25 criteria.and("条件二").is("条件二");
26 Aggregation.sort(sort), Aggregation.limit(10)
27 Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria),Aggregation.group("Content").count().as("value"),Aggregation.sort(sort), Aggregation.limit(10));
28List <DBObject> PVS = mongoTemplate.aggregate (aggregation, "collectionName", DBObject. Class ) .getMappedResults ();
 29  
30  
31 is  paging query mongo of:
 32  
33 is  directly Skip () plus limit () can achieve tab, However, in the case of large volumes of data, this method is defective performance
 34  
35 All my optimization as follows: on the record once the last record id, the next page of the query as a query, the query can be greater than this id , sort sorting setting value - 1 represents a descending order, an ascending order.
36  
37 [ DBObject Query = new new BasicDBObject ();
 38 is query.put ( "a condition", "a condition" );
 39  the dbcursor the dbcursor;
 40  IF (Page. 1 == == 0 || || Page StringUtil.isStrEmpty (lastId )) {
 41     dbCursor = mongoTemplate.getCollection(appid).find(query).sort(new BasicDBObject("name", -1)).skip(size * (page - 1)).limit(size);
42 }else{
43     BasicDBObject decoment = new BasicDBObject("$gt",new ObjectId(lastId));
44     query.put("_id",decoment);
45     dbCursor = mongoTemplate.getCollection(appid).find(query).sort(new BasicDBObject("name", -1)).skip(size * (page - 1)).limit(size);
46 }
47 return dbCursor.toArray();
View Code

 

Guess you like

Origin www.cnblogs.com/lljboke/p/11367408.html