MongoDB deposits non-homogeneous objects into the same collection

Source: Internet
Author: User
Tags findone json mongodb mongodb collection

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;
}

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.