Mongotemplate templates provided by spring data MongoDB for aggregation operation practices

Source: Internet
Author: User
Tags mongodb tostring

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 + "]";
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.