spring data mongodb 提供的MongoTemplate 模板,进行Aggregation操作实践
2016-11-21 15:29
537 查看
使用spring data mongodb 提供的MongoTemplate 模板,进行Aggregation操作实践
public class FlowSizeAggregateTest
{
public static void main(String[] args) throws Exception
{
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath*:META-INF/spring/*.xml");
MongoTemplate mongoTemplate = (org.springframework.data.mongodb.core.MongoTemplate) context
.getBean(MongoTemplate.class);
// Criteria criteria =
// Criteria.where("userName").is("123123@123.com");
Aggregation aggregation = Aggregation.newAggregation(Aggregation
.group("userName").sum("flowSize").as("tatoalFlowSize").sum("amount").as("totalAmount"));
AggregationResults<User> aggRes = mongoTemplate.aggregate(aggregation,
"flow_2016_10", User.class);
List<User> listRes = aggRes.getMappedResults();
for (User user : listRes)
{
System.out.println(user);
}
System.out.println(listRes.size());
}
}
class User implements Serializable
{
/**
*
*/
private static final long serialVersionUID = -6420217644123680385L;
private String _id;
private String tatoalFlowSize;
private Double totalAmount;
public String get_id()
{
return _id;
}
public void set_id(String _id)
{
this._id = _id;
}
public String getTatoalFlowSize()
{
return tatoalFlowSize;
}
public void setTatoalFlowSize(String tatoalFlowSize)
{
this.tatoalFlowSize = tatoalFlowSize;
}
public Double getTotalAmount()
{
return totalAmount;
}
public void setTotalAmount(Double totalAmount)
{
this.totalAmount = totalAmount;
}
@Override
public String toString()
{
return "User [_id=" + _id + ", tatoalFlowSize=" + tatoalFlowSize
+ ", totalAmount=" + totalAmount + "]";
}
}
使用spring data mongodb 提供的MongoTemplate 模板,进行Aggregation操作实践
public class FlowSizeAggregateTest
{
public static void main(String[] args) throws Exception
{
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath*:META-INF/spring/*.xml");
MongoTemplate mongoTemplate = (org.springframework.data.mongodb.core.MongoTemplate) context
.getBean(MongoTemplate.class);
// Criteria criteria =
// Criteria.where("userName").is("123123@123.com");
Aggregation aggregation = Aggregation.newAggregation(Aggregation
.group("userName").sum("flowSize").as("tatoalFlowSize").sum("amount").as("totalAmount"));
AggregationResults<User> aggRes = mongoTemplate.aggregate(aggregation,
"flow_2016_10", User.class);
List<User> listRes = aggRes.getMappedResults();
for (User user : listRes)
{
System.out.println(user);
}
System.out.println(listRes.size());
}
}
class User implements Serializable
{
/**
*
*/
private static final long serialVersionUID = -6420217644123680385L;
private String _id;
private String tatoalFlowSize;
private Double totalAmount;
public String get_id()
{
return _id;
}
public void set_id(String _id)
{
this._id = _id;
}
public String getTatoalFlowSize()
{
return tatoalFlowSize;
}
public void setTatoalFlowSize(String tatoalFlowSize)
{
this.tatoalFlowSize = tatoalFlowSize;
}
public Double getTotalAmount()
{
return totalAmount;
}
public void setTotalAmount(Double totalAmount)
{
this.totalAmount = totalAmount;
}
@Override
public String toString()
{
return "User [_id=" + _id + ", tatoalFlowSize=" + tatoalFlowSize
+ ", totalAmount=" + totalAmount + "]";
}
}
public class FlowSizeAggregateTest
{
public static void main(String[] args) throws Exception
{
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath*:META-INF/spring/*.xml");
MongoTemplate mongoTemplate = (org.springframework.data.mongodb.core.MongoTemplate) context
.getBean(MongoTemplate.class);
// Criteria criteria =
// Criteria.where("userName").is("123123@123.com");
Aggregation aggregation = Aggregation.newAggregation(Aggregation
.group("userName").sum("flowSize").as("tatoalFlowSize").sum("amount").as("totalAmount"));
AggregationResults<User> aggRes = mongoTemplate.aggregate(aggregation,
"flow_2016_10", User.class);
List<User> listRes = aggRes.getMappedResults();
for (User user : listRes)
{
System.out.println(user);
}
System.out.println(listRes.size());
}
}
class User implements Serializable
{
/**
*
*/
private static final long serialVersionUID = -6420217644123680385L;
private String _id;
private String tatoalFlowSize;
private Double totalAmount;
public String get_id()
{
return _id;
}
public void set_id(String _id)
{
this._id = _id;
}
public String getTatoalFlowSize()
{
return tatoalFlowSize;
}
public void setTatoalFlowSize(String tatoalFlowSize)
{
this.tatoalFlowSize = tatoalFlowSize;
}
public Double getTotalAmount()
{
return totalAmount;
}
public void setTotalAmount(Double totalAmount)
{
this.totalAmount = totalAmount;
}
@Override
public String toString()
{
return "User [_id=" + _id + ", tatoalFlowSize=" + tatoalFlowSize
+ ", totalAmount=" + totalAmount + "]";
}
}
使用spring data mongodb 提供的MongoTemplate 模板,进行Aggregation操作实践
public class FlowSizeAggregateTest
{
public static void main(String[] args) throws Exception
{
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath*:META-INF/spring/*.xml");
MongoTemplate mongoTemplate = (org.springframework.data.mongodb.core.MongoTemplate) context
.getBean(MongoTemplate.class);
// Criteria criteria =
// Criteria.where("userName").is("123123@123.com");
Aggregation aggregation = Aggregation.newAggregation(Aggregation
.group("userName").sum("flowSize").as("tatoalFlowSize").sum("amount").as("totalAmount"));
AggregationResults<User> aggRes = mongoTemplate.aggregate(aggregation,
"flow_2016_10", User.class);
List<User> listRes = aggRes.getMappedResults();
for (User user : listRes)
{
System.out.println(user);
}
System.out.println(listRes.size());
}
}
class User implements Serializable
{
/**
*
*/
private static final long serialVersionUID = -6420217644123680385L;
private String _id;
private String tatoalFlowSize;
private Double totalAmount;
public String get_id()
{
return _id;
}
public void set_id(String _id)
{
this._id = _id;
}
public String getTatoalFlowSize()
{
return tatoalFlowSize;
}
public void setTatoalFlowSize(String tatoalFlowSize)
{
this.tatoalFlowSize = tatoalFlowSize;
}
public Double getTotalAmount()
{
return totalAmount;
}
public void setTotalAmount(Double totalAmount)
{
this.totalAmount = totalAmount;
}
@Override
public String toString()
{
return "User [_id=" + _id + ", tatoalFlowSize=" + tatoalFlowSize
+ ", totalAmount=" + totalAmount + "]";
}
}
相关文章推荐
- Spring Data MongoDB 六:进阶Aggregation操作(上)
- [Spring Data MongoDB]学习笔记--MongoTemplate插入修改操作
- Spring Data MongoDB 六:进阶Aggregation操作(上)
- 通过使用spring-data-mongodb操作mongodb
- 那位帮忙提供一个java mongodb多个collection进行mapreduce的操作。
- java用spring-data-mongodb操作mongodb的时间问题
- 使用spring-data-mongodb操作mongodb
- Spring Data MongoDB 级联操作
- Spring Data MongoDB 一:入门篇(环境搭建、简单的CRUD操作)
- Spring Data MongoDB 一:入门篇(环境搭建、简单的CRUD操作)
- java操作Mongodb,Spring-data-mongo简单配置
- Spring Data MongoDB 级联操作
- Spring Data MongoDB 级联操作
- 大数据之mongodb --> (2)java集成 MongoDB 3.2,使用Spring-data-mongodb进行集成
- Spring Data MongoDB 二:添加、删除操作
- spring-data-mongodb-1.9.x中Aggregation关于Conditional Aggregation Operators的坑
- Spring Data MongoDB 一:入门篇(环境搭建、简单的CRUD操作)
- Spring Data MongoDB 二:添加、删除操作
- 使用springdata操作mongodb的例子
- 使用Spring-data进行Redis操作