Use the Mongotemplate template provided by spring data MongoDB for aggregation operation practices
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.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 + "]";
}
}
Use the Mongotemplate template provided by spring data MongoDB for aggregation operational practices
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.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 + "]";
}
}