SPRINGMVC Operation MongoDB Additions and deletions change

Source: Internet
Author: User

Below is MongoDB simple additions and deletions (news category)

Attached: Query.addcriteria (Criteria.where ("ModelID"). NE (""). NE (null)); Non-empty
Query.with (New Sort (New Order (direction.desc, "CT"))); Flashbacks

Sort multiple fields: first sort by sort descending, second sort by ct descending
Query.with (New Order (Direction.desc, "sort")). and (New sort (direction.desc, "CT"));

 Public classNewshelpservice {@ResourcePrivatemongotemplate mongotemplate; //Add     Public voidInsert (Newshelp newshelp) {Mongotemplate.insert (NEWSHELP); }//Select     PublicPagination<newshelp>Findnewshelpall (String type, string title, String ID, Integer pageno, integer pageSize) {//querypage page = Querypage.build (PageNo, pageSize);Mandoassert.notnull (Type, "cannot be empty"); Criteria Criteria= Criteria.where ("type"). is (type); if(title!=NULL) {pattern pattern=Pattern.compile (title, pattern.case_insensitive);//Fuzzy Query Criteria.andoperator (criteria.where ("Title"). Regex (pattern)); }        if(id!=NULL) {criteria.andoperator (Criteria.where ("id"). is (ID)); } Query Query=Basicquery.query (criteria); LongTotal = Mongotemplate.count (query, Newshelp.class); Pagination<NewsHelp> Newshelps =NewPagination<newshelp>(PageNo, pageSize); intSkip = (pageNo-1) *pageSize; if(pagesize<1) {Query.skip (skip);//Skip is the equivalent of starting from that record .Query.limit (pageSize);//how many records to take from Skip} List<NewsHelp> datas = mongotemplate.find (query, Newshelp.class); Newshelps.setdata (datas);//Get DataNewshelps.settotal ((int) total); returnNewshelps; }//SelectOne     Publicnewshelp findnewshelp (String ID) {query query= Basicquery.query (Criteria.where ("id"). is (ID)); Newshelp Newshelp= Mongotemplate.findone (query, Newshelp.class); returnNewshelp; }    //Update News category     Public voidupdatenewshelp (Newshelp help) {Mandoassert.notnull (Help.getid (),"ID cannot be empty"); Update Update=NewUpdate (); if(NULL!=Help.gettops ()) {Update.set ("Tops", Help.gettops ()); }        if(Stringutils.isnotempty (Help.gettitle ())) {Update.set ("Title", Help.gettitle ()); }        if(Stringutils.isnotempty (Help.getcontent ())) {Update.set ("Content", Help.getcontent ()); } update.set ("Modifydate",NewDate ()); Mongotemplate.updatemulti (Query.query (Criteria.where ("id"). is (Help.getid ())), Update,newshelp.class); } //Delete     Public voidDelnewshelp (list<string>IDs) {Ids.stream (). ForEach (ID-{query Query= Basicquery.query (Criteria.where ("id"). is (ID)); Mongotemplate.findandremove (query, Newshelp.class);    }); }}

Encounter Dawn Original

The SPRINGMVC+MONGODB configuration is explained below

SPRINGMVC Operation MongoDB Additions and deletions change

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.