In MongoDB, the concept of ' table ' in the corresponding relational database is ' set ', the data structure in the table is consistent, MongoDB is stored in JSON format, the collection data is flexible, and the same set of MongoDB collection can hold different structures. The following are examples of testing in project development.
1. Save
public void Testsave () {
Bizentpris biz = new Bizentpris ();
Biz.setname ("biztest01");
Country Country = new country ();
Country.setname ("Chinaname");
Mongotemplate.save (Biz, "testmuticollection");
Mongotemplate.save (Country, "testmuticollection");
Bizentpris biz2 = new Bizentpris ();
Biz2.setname ("biztest02");
Country Country2 = new country ();
Country2.setname ("ChinaName02");
Mongotemplate.save (BIZ2, "testmuticollection");
Mongotemplate.save (Country2, "testmuticollection");
}
2. Enquiry
Public map<string,object> testQuery01 () {
Map<string,object> objmap = new hashmap<string, object> ();
Query query = new query ();
Query.addcriteria (Criteria.where ("name"). Is ("biztest01"));
Bizentpris ent = mongotemplate.findone (query, Bizentpris.class, "testmuticollection");
Objmap.put ("Entpris", ENT);
Query q = new query ();
Q.addcriteria (Criteria.where ("name"). Is ("chinaName01"));
Country Country = Mongotemplate.findone (q, Country.class, "testmuticollection");
Objmap.put ("Country", country);
list<country> Countrylistall = Mongotemplate.findall (Country.class, "testmuticollection");
Objmap.put ("Countrylistall", countrylistall);//When FindAll queries the collection testmuticollection all object JSON data, including Bizentpris and country
list<bizentpris> Entlistall = Mongotemplate.findall (Bizentpris.class, "testmuticollection");
Objmap.put ("Entprislistall", entlistall);//Query collection testmuticollection All object JSON data, including Bizentpris and country
Query q1 = new query ();
System.out.println (Country.class.getSimpleName ());
System.out.println (Country.class.getName ());
Q1.addcriteria (Criteria.where ("_class"). Is (Country.class.getSimpleName ()));
list<country> countrylist = Mongotemplate.find (Q1,country.class, "testmuticollection");
Objmap.put ("Countrylistwithclass", countrylist);
Query q2 = new query ();
Q2.addcriteria (Criteria.where ("_class"). Is (BizEntpris.class.getName ()));
list<bizentpris> entlist = Mongotemplate.find (Q2,bizentpris.class, "testmuticollection");
Objmap.put ("Entprislistwithclass", entlist);
return objmap;
}