Springboot~mongo an inline collection operation

Source: Internet
Author: User

For the various operations of the embedded object of MongoDB uncle in the. NET platform has said, at the same time the uncle also encapsulated MONGO storage, the use is also very convenient, and in the Java Springboot Framework of course there are corresponding methods, the following main say, Hope that just contact with MongoDB classmate Help!

A test data structure of the demo

/*** address.*/@Data @noargsconstructor@allargsconstructor Public classAddress {/*** number. */@IdPrivateString ID; /*** Province. */  PrivateString Province; /*** City. */  PrivateString City; /*** District. */  PrivateString District; /*** status. */  PrivateStatus status; /*** Extended. */  PrivateList<addressextension>addressextension;}

Where it has an inline collection object, addressextension, we've added some test data,

Here are a few common data operations:

 /*** Get data. *   * @paramProvince Province *@paramCity *@return   */@Override PublicAddress findbyprovinceandcity (String province, String city) {query Query=NewQuery (Criteria.where ("province"). Is (province). and (' City '). is (city)); returnMongotemplate.findone (query, Address.class, "Address"); }  /*** Update fields. *   * @paramaddress. */@Override Public voidupdatecity (address) {query Query=NewQuery (Criteria.where ("_id"). Is (Address.getid ())); Update Update= Update.update ("City", Address.getcity ()); Mongotemplate.upsert (query, update, Address. )class); }  /*** Add data from an inline document. *   * @paramID. * @paraminfo. */@Override Public voidaddaddressinfo (string ID, string info) {Query Query= Query.query (Criteria.where ("_id"). is (ID)); Addressextension ext=Newaddressextension ();    Ext.setinfo (info); Update Update=NewUpdate ();//Update.push ("Students", student); //Addtoset If the data already exists, no action is taken, and push inserts an identical piece of dataUpdate.addtoset ("Addressextension", ext); Mongotemplate.upsert (query, update, Address. )class); }  /*** Update an element in the embedded document * *@paramOldinfo *@paramNewinfo*/@Override Public voidupdateaddressinfo (String oldinfo, String newinfo) {query Query=NewQuery (Criteria.where ("Addressextension.info"). is (Oldinfo)); Update Update=NewUpdate (); Update.set ("Addressextension.$.info", Newinfo); Mongotemplate.upsert (query, update, Address. )class); }  /*** Lambda filter. * @paramlist *@parampredicate *@return   */   PublicList<addressextension> Conditionfilter (list<addressextension> List, predicate<addressextension>predicate) {    returnList.stream (). filter (predicate). Collect (Collectors.tolist ()); }  /*** Delete the data in the inline document. *   * @paramID. * @paramaddressextension. */@Override Public voidDeladdressinfo (String ID, addressextension addressextension) {query Query= Query.query (Criteria.where ("_id"). is (ID)); Update Update=NewUpdate (); Update.pull ("Addressextension", addressextension); Mongotemplate.updatefirst (query, update, Address. )class); }  /*** Delete document. *   * @paramID. */@Override Public voiddeladdress (String ID) {query query= Query.query (Criteria.where ("_id"). is (ID)); Mongotemplate.remove (query, Address.class); }

Where the update inline collection is special, it is updated with the subscript of the collection element, and $ is the subscript for the element that is currently being updated!

Thanks for reading!

Springboot~mongo an inline collection operation

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.