MONGO database additions and deletions

Source: Internet
Author: User
Tags object object

First establish a connection pool

Applicationcontext.xml Configuration

1. Table Header Additions

xmlns:mongo= "Http://www.springframework.org/schema/data/mongo"

2. Add a configuration

<!--Mongo DataSource--

1 <Mongo:mongoID= "MONGO"Host= "${mongo.host}"Port= "${mongo.port}">2 <mongo:options/>3 </Mongo:mongo>4 5 <mongo:db-factoryID= "Mongodbfactory"dbname= "${mongo.dbname}"Mongo-ref= "MONGO"/>6 7 <BeanID= "Mongotemplate"class= "Org.springframework.data.mongodb.core.MongoTemplate">8 <Constructor-argref= "Mongodbfactory"/>9 </Bean>

3. Mongoaddkeywordservice Add

 PackageCom.honghailt.dataextracter.service;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.data.mongodb.core.MongoTemplate;ImportOrg.springframework.stereotype.Service;Importorg.springframework.util.StringUtils;ImportCom.honghailt.dataextracter.model.MongoKeyword;ImportCom.mongodb.BasicDBObject;Importcom.mongodb.DBCollection;ImportCom.mongodb.DBCursor;Importcom.mongodb.DBObject; @Service Public classMongoaddkeywordservice {@AutowiredPrivatemongotemplate mongoDb; //Public void CreateTable () {//dbcollection collection = Mongodb.getcollection (//"Tb_keyword_operation_test_log");//dbobject dbo = new Basicdbobject ();//Dbo.put ("Nick", "Test");//dbo.put ("Col1", "col1");//dbo.put ("col2", "col2");//dbo.put ("Col3", "col3");//dbo.put ("Col4", "Col4");//Collection.insert (dbo);//     }     //Public void FindData () {//dbcollection collection = Mongodb.getcollection (//"Cat_brand");//dbobject dbo = new Basicdbobject ();//dbo.put ("CatId", "110202");//dbo.put ("brand", "AData");//long Timebegin =system.currenttimemillis ();//dbcursor sor=collection.find (dbo);//System.out.println ("Count:\t" +sor.count ());//long Timeend =system.currenttimemillis ();//System.out.println (timeend-timebegin);//     }          PublicMap<string,integer>GetCount (Mongokeyword keyWord) {dbcollection collection=Mongodb.getcollection ("Cat_brand"); DBObject dbo=NewBasicdbobject (); if(keyword!=NULL&&!Stringutils.isempty (Keyword.getkeywordstr ())) {Dbo.put ("Brand", Keyword.getkeywordstr ()); }            if(keyword!=NULL&&!Stringutils.isempty (Keyword.getcatid ())) {Dbo.put ("CatId", Keyword.getcatid (). toString ()); } dbcursor Sor=collection.find (dbo); intCount =Sor.count (); Map<String,Integer> map =NewHashmap<string,integer>(); Map.put ("Count", Count); returnmap; }           PublicList<mongokeyword>getkeywordlist (Mongokeyword keyWord) {List<MongoKeyword> keywordlist =NewArraylist<mongokeyword>(); Dbcollection Collection=Mongodb.getcollection ("Cat_brand"); DBObject dbo=NewBasicdbobject (); if(keyword!=NULL&&!Stringutils.isempty (Keyword.getkeywordstr ())) {Dbo.put ("Brand", Keyword.getkeywordstr ()); }        if(keyword!=NULL&&!Stringutils.isempty (Keyword.getcatid ())) {Dbo.put ("CatId", Keyword.getcatid (). toString ()); } dbcursor Sor=collection.find (dbo). Skip (0). Limit (20); List<DBObject> list=Sor.toarray ();  for(DBObject dbobject:list) {mongokeyword keyword=NewMongokeyword (); if(Dbobject.get ("catId")! =NULL) Keyword.setcatid ("" +dbobject.get ("CatId")); if(Dbobject.get ("brand")! =NULL) Keyword.setkeywordstr ("" + Dbobject.get ("brand"))); if(Dbobject.get ("Allavgprice")! =NULL) Keyword.setallavgprice ("" +dbobject.get ("Allavgprice")); if(Dbobject.get ("Allclicks")! =NULL) Keyword.setallclicks ("" +dbobject.get ("Allclicks")); if(Dbobject.get ("allcompition")! =NULL) keyword.setallcompition ("" +dbobject.get ("Allcompition")); if(Dbobject.get ("allctr")! =NULL) keyword.setallctr ("" + Dbobject.get ("allctr")); if(Dbobject.get ("ALLPV")! =NULL) KEYWORD.SETALLPV ("" + Dbobject.get ("ALLPV"));        Keywordlist.add (keyword); }         returnkeywordlist; }               Public voidSavemongokeyword (string[] keywordstr, string[] catId) {dbcollection collection=Mongodb.getcollection ("Cat_brand"); List<DBObject> d =NewArraylist<dbobject>(); List<String> list =getuniquelist (Keywordstr,catid);  for(String string:list) {string[] str= String.Split (","); DBObject dbo=NewBasicdbobject (); if(! Stringutils.isempty (str[0]) {dbo.put ("Brand", str[0]); }                if(! Stringutils.isempty (str[1]) {dbo.put ("CatId", str[1]); }                 intCount=collection.find (dbo). Count (); if(count<=0) {Dbo.put ("Brand", str[0]); Dbo.put ("CatId", str[1]);                D.add (dbo); }Else{System.out.println ("Already exists Keywordstr:" + str[0] + "CatId:" +str[1]);        }} collection.insert (d); //TODO auto-generated Method Stub    }         Public StaticList<string>getuniquelist (string[] keywordstr, string[] catId) {List<String> list=NewArraylist<string>(); if(keywordstr!=NULL&& keywordstr.length>=1){             for(inti = 0; i < keywordstr.length; i++) {                if(! Stringutils.isempty (Keywordstr[i]) &&!Stringutils.isempty (Catid[i])) {String str=keywordstr[i]+ "," +Catid[i]; if(!list.contains (str))                    {List.add (str); }                }            }        }        returnlist; }    //Public static void Main (string[] args) {//string[] strs1={"1", "2", "3", "4", "1", "2"};//string[] strs2={"1", "2", "3", "4", "1", "3"};//list<string> List =getuniquelist (strs1,strs2);//For (Object object:list) {//System.out.println (object);//        }//    }    }

Update action

 PublicList<map<string,string>>getmongolist () {dbcollection col=Basedao.getdb (). GetCollection (CONSTANT.TABLE_CUSTOMER_ORDER_MONTN); Dbcursor Sor=Col.find (); List<DBObject> list=Sor.toarray (); Map<String,String> map= This. Getuserlist ();//                            for(String s:map.keyset ()) {System.out.println (s); Basicdbobject Query=NewBasicdbobject (); Query.put ("Creator", s); DBObject Update=NewBasicdbobject (); Update.put ("Creator_deptid", Map.get (s)); Col.update (Query,NewBasicdbobject (). Append ("$set", update),false,true); }//For (DBObject dbobject:list) {//String name = (string) dbobject.get ("creator");//if (Stringutils.isnotempty (Map.get (name))) {////System.out.println (Dbobject.get ("Creator_deptid") + "" + (Map.get (name));//if (!dbobject.get ("Creator_deptid"). Equals (Map.get (name))) {//System.out.println (Dbobject.get ("Creator_deptid") + "|0000==*==0000|" + (Map.get (name)) + "AAAA" +name);//                }//            }//        }        return NULL; }

MONGO database additions and deletions

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.