博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mongo数据库的增删改查
阅读量:4993 次
发布时间:2019-06-12

本文共 6381 字,大约阅读时间需要 21 分钟。

首先建立连接池

applicationContext.xml 配置  

1.表头 添加  

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

 

2.添加配置

<!-- Mongo DataSource -->

1 
2
3
4 5
6 7
8
9

 

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;@Servicepublic 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
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
map = new HashMap
(); map.put("count", count); return map; } public List
getKeywordList(MongoKeyword keyWord){ List
keyWordList = new ArrayList
(); 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
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
d = new ArrayList
(); List
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
getUniqueList(String[] keywordStr, String[] catId){ List
list= new ArrayList
(); 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
list =getUniqueList(strs1,strs2);// for (Object object : list) {// System.out.println(object);// }// } }

更新操作

public List
> getMongoList(){ DBCollection col = baseDao.getDB().getCollection(Constant.TABLE_CUSTOMER_ORDER_MONTN); DBCursor sor =col.find(); List
list=sor.toArray(); Map
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; }

 

转载于:https://www.cnblogs.com/w8104254/p/4112067.html

你可能感兴趣的文章
linux下给php安装curl、gd(ubuntu)
查看>>
Java自带的Logger使用-代码摘要
查看>>
Java设计模式系列 — 构造器模式
查看>>
MySQL执行计划explain的key_len解析
查看>>
Windows Phone开发(9):关于页面状态 转:http://blog.csdn.net/tcjiaan/article/details/7292160...
查看>>
android 通过数组,流播放声音的方法
查看>>
Spring入门篇
查看>>
JAVA遇见HTML——JSP篇(JSP状态管理)
查看>>
启动eclipse出现错误Java was started but returned exit =一个数字
查看>>
myBatis模糊查找
查看>>
数据结构与算法之五 链接列表
查看>>
java 对象数组
查看>>
设计模式读书笔记-单件模式(创建型模式)
查看>>
Oracle——热备份
查看>>
Vue路由history模式踩坑记录:nginx配置解决404问题
查看>>
c# 多张图片合成一张图片
查看>>
使用SQL Server 2008的事务日志传送功能备份数据库(logshiping)
查看>>
AngularJS多个ng-app只解析第一个的问题
查看>>
强制修改常量的值
查看>>
Grunt 初体验
查看>>