mongo数据库的增删改查
首先建立连接池
applicationContext.xml 配置
1.表头 添加
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
2.添加配置
<!-- Mongo DataSource -->
1 <mongo:mongo id="mongo" host="${mongo.host}" port="${mongo.port}"> 2 <mongo:options/> 3 </mongo:mongo> 4 5 <mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbName}" mongo-ref="mongo"/> 6 7 <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 8 <constructor-arg ref="mongoDbFactory"/> 9 </bean>
3. MongoAddKeywordService 添加
package com.honghailt.dataextracter.service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import com.honghailt.dataextracter.model.MongoKeyword; import com.mongodb.BasicDBObject; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; @Service public class MongoAddKeywordService { @Autowired private MongoTemplate 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); // } public Map<String,Integer> getCount(MongoKeyword keyWord){ DBCollection collection = mongoDb.getCollection( "cat_brand"); DBObject dbo = new BasicDBObject(); 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); int count = sor.count(); Map<String,Integer> map = new HashMap<String,Integer>(); map.put("count", count); return map; } public List<MongoKeyword> getKeywordList(MongoKeyword keyWord){ List<MongoKeyword> keyWordList = new ArrayList<MongoKeyword>(); DBCollection collection = mongoDb.getCollection( "cat_brand"); DBObject dbo = new BasicDBObject(); 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 = new MongoKeyword(); 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); } return keyWordList; } public void saveMongoKeyWord(String[] keywordStr, String[] catId) { DBCollection collection = mongoDb.getCollection( "cat_brand"); List<DBObject> d = new ArrayList<DBObject>(); List<String> list =getUniqueList(keywordStr,catId); for (String string : list) { String[] str = string.split(","); DBObject dbo = new BasicDBObject(); if(!StringUtils.isEmpty(str[0])){ dbo.put("brand", str[0]); } if(!StringUtils.isEmpty(str[1])){ dbo.put("catId", str[1]); } int count= collection.find(dbo).count(); if(count<=0){ dbo.put("brand", str[0]); dbo.put("catId",str[1]); d.add(dbo); }else{ System.out.println("已存在keywordStr:" + str[0] +"catId:" +str[1]); } } collection.insert(d); // TODO Auto-generated method stub } public static List<String> getUniqueList(String[] keywordStr, String[] catId){ List<String> list= new ArrayList<String>(); if(keywordStr!=null && keywordStr.length>=1){ for (int i = 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); } } } } return list; } // 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); // } // } }
更新操作
public List<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 = new BasicDBObject(); query.put("creator",s ); DBObject update=new BasicDBObject(); update.put("creator_deptid", map.get(s)); col.update(query, new BasicDBObject().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; }