近期项目做了次架构调整,原来是使用MySQL+GeoHash来存储LBS数据(地理位置信息),现在使用NOSQL数据库MongoDB来存储LBS数据(地理位置信息)。由于项目是基于spring MVC开发的,今天就Mongodb的使用做下总结。

Spring MVC 集成Mongodb

1.加载jar,maven配置

        <!-- 新加入的spring整合mongodb的包 开始 -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.13.0-rc0</version>
</dependency> <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-cross-store</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-log4j</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<!-- 新加入的spring整合mongodb的包 结束 -->
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

2.配置 
Mongodb分2种方法集成,分别是单机和集群方式。 
单机配置 
applicationContext.xml

<mongo:mongo id="mongo" host="${mongo.host}" port="${mongo.port}" >
<mongo:options connections-per-host="${mongo.connectionsPerHost}"
threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}"
auto-connect-retry="${mongo.autoConnectRetry}" socket-keep-alive="${mongo.socketKeepAlive}"
socket-timeout="${mongo.socketTimeout}" slave-ok="${mongo.slaveOk}" write-number="1"
write-timeout="0" write-fsync="true"/>
</mongo:mongo>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

配置文件common-config.properties

mongo.dbname = test_db #数据库名称
mongo.password = test_pwd #密码
mongo.username = test_user #用户名
mongo.host = 127.0.0.1 #主机
mongo.port= 27017 #端口号
mongo.connectionsPerHost= 8 #一个线程变为可用的最大阻塞数
mongo.threadsAllowedToBlockForConnectionMultiplier= 4 #线程队列数,它以上面connectionsPerHost值相乘的结果就是线程队列最大值
mongo.connectTimeout= 1500 #连接超时时间(毫秒)
mongo.maxWaitTime= 1500 #最大等待时间
mongo.autoConnectRetry= true #自动重连
mongo.socketKeepAlive= true #scoket保持活动
mongo.socketTimeout=1500 #scoket超时时间
mongo.slaveOk=true #读写分离
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

集群配置 
applicationContext.xml

<mongo:mongo  id="mongo"  replica-set="${mongo.replicaSet}">
<mongo:options connections-per-host="${mongo.connectionsPerHost}"
threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}" auto-connect-retry="${mongo.autoConnectRetry}"
socket-keep-alive="${mongo.socketKeepAlive}" socket-timeout="${mongo.socketTimeout}" slave-ok="${mongo.slaveOk}"
write-number="${mongo.writeNumber}" write-fsync="${mongodb.writeFsync}" />
</mongo:mongo>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

配置文件common-config.properties

mongo.dbname = test_db
mongo.username = test_user
mongo.password = test_pwd
mongo.replicaSet = 192.168.2.193:27017,192.168.2.192:27017
mongo.connectionsPerHost= 100
mongo.threadsAllowedToBlockForConnectionMultiplier=50
mongo.connectTimeout=3000
mongo.maxWaitTime=5000
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
mongo.socketTimeout=5000
mongo.slaveOk=true
mongo.writeNumber = 1
mongodb.writeFsync = true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

其他配置

<!-- 用户验证 -->
<bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">
<constructor-arg name="username" value="${mongo.username}" />
<constructor-arg name="password" value="${mongo.password}" />
</bean> <!-- mongo的工厂,通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建 -->
<bean id="mongoDbFactory"
class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
<constructor-arg ref="mongo" />
<constructor-arg value="${mongo.dbname}" />
<constructor-arg ref="userCredentials" />
</bean> <bean id="mappingContext"
class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" /> <bean id="defaultMongoTypeMapper"
class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
<constructor-arg name="typeKey">
<null />
</constructor-arg>
</bean> <!-- collection的映射 -->
<bean id="mappingMongoConverter"
class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mappingContext" ref="mappingContext" />
<property name="typeMapper" ref="defaultMongoTypeMapper" />
</bean> <!-- mongodb的主要操作对象,所有对mongodb的增删改查的操作都是通过它完成 -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
</bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

Mongodb增删改查

1.Mongodb对象模型 
Dynamic.Java


@Document(collection = "Dynamic")
public class Dynamic implements Serializable{ /**
*
*/
private static final long serialVersionUID = 179822189115264434L;
private String _id;
private String userId; //用户ID
private Date releaseDate;//发布日期
private double []loc = new double[2];//位置 public void setId(String _id) {
this._id = _id;
} public String getId() {
return this._id;
} public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Date getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(Date releaseDate) {
this.releaseDate = releaseDate;
}
public double[] getLoc() {
return loc;
}
public void setLoc(double[] loc) {
this.loc = loc;
} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

2.代码示例 
新增

Dynamic dynamic = New Dynamic();
dynamic.setUserId("10023");
// ...
// set属性,自己补全
// 转换为DBObject
DBObject dbObject = BeanUtil.bean2DBObject(dynamic);
db.removeField("serialVersionUID");
// 保存
mongoTemplate.getCollection(collection).save(dbObject)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

更新 
普通更新

DBObject aim = new BasicDBObject("_id", id);
DBObject setValue = new BasicDBObject();
setValue.put("loc", loc.getLoc());
// 更新的值,用$set
DBObject updateSetValue = new BasicDBObject("$set", setValue); //更新,aim:where条件 updateSetValue:更新值 true:是否没有记录时插入 false:是否多行更新
mongoTemplate.getCollection(collection).update(aim, updateSetValue , true,
false);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

数组更新

DBObject addAim = new BasicDBObject();
addAim.put("_id", id);
//数据中push值
DBObject updateSetValue = new BasicDBObject("$push", addedValue);
//增加
updateSetValue.put("$inc", new BasicDBObject("commentSize", 1));
mongoTemplate.getCollection(collection).update(addAim, updateSetValue);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

查询 
单条查询

DBObject query = new BasicDBObject("_id", id);
DBObject fields = new BasicDBObject();
mongoTemplate.getCollection(collection).findOne(query, fields)
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

分页查询

int pageSize = 20;
int pageNum = 1;
DBObject fields = new BasicDBObject();
//排序,-1 降序
DBObject orderBy = new BasicDBObject("release_date", -1); //分页查询
List<DBObject> list = new ArrayList<>();
Cursor cursor = mongoTemplate.getCollection(collection)
.find(query, fields).skip((pageNum - 1) * pageSize)
.limit(pageSize).sort(orderBy);
while (cursor.hasNext()) {
list.add(cursor.next());
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

范围查询

DBObject query = new BasicDBObject();
// $lte 小于等于,$lt 小于
query.put(
"release_date",
new BasicDBObject("$lte", DateUtil.parseDate(
maxTime, DateStyle.YYYY_MM_DD_HH_MM_SS_SSS
.getValue())));
// $gte 大于等于,$gt 大于
query.put(
"release_date",
new BasicDBObject("$gte", DateUtil.parseDate(
minTime, DateStyle.YYYY_MM_DD_HH_MM_SS_SSS
.getValue())));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

in 和not in 查询

// $in 查询
DBObject query = new BasicDBObject("qd_id", new BasicDBObject(
"$in", idStr))
// $nin 查询
DBObject query = new BasicDBObject("qd_id", new BasicDBObject(
"$nin", idStr))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

or 查询

List orArry = new ArrayList<>();
orArry.add(new BasicDBObject("qd_id", new BasicDBObject(
"$in", qd_ids.toArray())));
orArry.add(new BasicDBObject("_id", new BasicDBObject("$in", recommondIds.toArray())));
query.put("$or", orArry);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

聚合查询sum和group by

Map retMap = new HashMap<>();
// $group 分组,newsCount别名,$sum和,$commentSize字段名
String groupStr = "{$group:{_id:null,newsCount:{$sum:1},commentCount:{$sum:\"$commentSize\"},supportCount:{$sum:\"$supportSize\"}}}";
DBObject group = (DBObject) JSON.parse(groupStr);
String minTime = (String)params.get("start") + " 00:00:00:000";
String maxTime = (String)params.get("end") + " 00:00:00:000";
BasicDBObject query = new BasicDBObject();
query.put(
"release_date",
new BasicDBObject("$gte", DateUtil.parseDate(
minTime, DateStyle.YYYY_MM_DD_HH_MM_SS_SSS
.getValue())));
query.put(
"release_date",
new BasicDBObject("$lte", DateUtil.parseDate(
maxTime, DateStyle.YYYY_MM_DD_HH_MM_SS_SSS
.getValue())).append("$gte",
DateUtil.parseDate(minTime,
DateStyle.YYYY_MM_DD_HH_MM_SS_SSS
.getValue())));
DBObject match = new BasicDBObject("$match", query);
// 聚合查询
AggregationOutput output = mongoTemplate.getCollection(collection).aggregate(match,group);
for( Iterator< DBObject > it = output.results().iterator(); it.hasNext(); ){
BasicDBObject dbo = ( BasicDBObject ) it.next();
retMap = com.alibaba.fastjson.JSON.parseObject(dbo.toString(), Map.class);
}
return retMap;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

LBS查询(附近的人)

if (query == null)
query = new BasicDBObject(); List<DBObject> pipeLine = new ArrayList<>();
BasicDBObject pipeBasicDBObject = new BasicDBObject(); pipeBasicDBObject.append("near", new double[] { longitude, latitude })
.append("distanceField", "dist.calculated")
.append("spherical", true).append("query", query)
.append("distanceMultiplier", 6371); if (qType == 1) {
double maxDistance = distance/6371.0000;
pipeBasicDBObject.append("maxDistance", maxDistance);
} BasicDBObject aggregate = new BasicDBObject("$geoNear",
pipeBasicDBObject);
BasicDBObject orderObject = new BasicDBObject("$sort",
new BasicDBObject().append("dist.calculated", 1)); pipeLine.add(aggregate);
pipeLine.add(orderObject); int startNo = (pageNum-1)*pageSize;
int limit = pageSize; if(pageNum == 1)
{
limit = limit+1;
}
else
{
startNo = startNo+1;
} if (qType == 1) {
BasicDBObject skipObject = new BasicDBObject("$skip", startNo);
pipeLine.add(skipObject);
} BasicDBObject limitObject = new BasicDBObject("$limit", limit);
pipeLine.add(limitObject);
List<DBObject> list = new LinkedList<>();
try {
Cursor cursor = mongoTemplate.getCollection(collection).aggregate(
pipeLine, AggregationOptions.builder().build());
while (cursor.hasNext()) {
DBObject dBObject = cursor.next();
list.add(dBObject);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

删除

DBObject deleAim = new BasicDBObject();
deleAim.put("_id", id);
mongoTemplate.getCollection(collection).remove(dbObject)
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

ps:上面示例不全,只附上了重要的代码。

Spring MVC中使用Mongodb总结的更多相关文章

  1. spring mvc中的@propertysource

    在spring mvc中,在配置文件中的东西,可以在java代码中通过注解进行读取了: @PropertySource  在spring 3.1中开始引入 比如有配置文件 config.propert ...

  2. Spring mvc中@RequestMapping 6个基本用法

    Spring mvc中@RequestMapping 6个基本用法 spring mvc中的@RequestMapping的用法.  1)最基本的,方法级别上应用,例如: Java代码 @Reques ...

  3. spring mvc中使用freemark的一点心得

    参考文档: FreeMarker标签与使用 连接http://blog.csdn.net/nengyu/article/details/6829244 freemarker学习笔记--指令参考: ht ...

  4. Http请求中Content-Type讲解以及在Spring MVC中的应用

    引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值 ...

  5. Spring mvc中@RequestMapping 6个基本用法小结(转载)

    小结下spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: @RequestMapping(value="/departments" ...

  6. Spring MVC中处理静态资源的多种方法

    处理静态资源,我想这可能是框架搭建完成之后Web开发的”头等大事“了. 因为一个网站的显示肯定会依赖各种资源:脚本.图片等,那么问题来了,如何在页面中请求这些静态资源呢? 还记得Spring MVC中 ...

  7. Spring MVC 中的基于注解的 Controller【转】

    原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...

  8. spring mvc中的文件上传

    使用commons-fileupload上传文件所需要的架包有:commons-fileupload 和common-io两个架包支持,可以到Apache官网下砸. 在配置文件spring-mvc.x ...

  9. spring mvc中的valid

    当你希望在spring mvc中直接校验表单参数时,你可以采用如下操作: 声明Validator的方式: 1.为每一个Controller声明一个Validator @Controller publi ...

随机推荐

  1. Swift - 17 - 数组的初始化

    import UIKit // 声明数组 var array = ["A", "B", "C", "D", " ...

  2. 网络编程Socket之UDP

    服务器端实现步骤: 1. 创建 DatagramSocket,指定端口号 2. 创建 DatagramPacket 3. 接收客户端发送的数据信息 4. 读取数据 package cn.jmu.edu ...

  3. 解决 Ubuntu15.04 登陆界面无限循环 的问题

    本人新手,在学习linux 安装NVIDIA 驱动的时候出现了一个奇怪的问题:登陆界面输入正确的账户密码,短暂闪烁后又返回了登陆界面.经查阅多种资料,已解决此问题 以下内容来自:http://blog ...

  4. js中callee与caller的区别

    callee是对象的一个属性,该属性是一个指针,指向参数arguments对象的函数首先我们来写个阶成函数:function chen(x){if (x<=1) {return 1;} else ...

  5. js 实现 C# 的 format 方法

    2014-11-08 12:18:51 更新,修复原形链方法被当作关键词的bug,其实之前是想用全局关键词的,不过还是算了,array里有太多单词了.                          ...

  6. Linux socket编程 DNS查询IP地址

    本来是一次计算机网络的实验,但是还没有完全写好,DNS的响应请求报文的冗余信息太多了,不只有IP地址.所以这次的实验主要就是解析DNS报文.同时也需要正确的填充请求报文.如果代码有什么bug,欢迎指正 ...

  7. iTunes 安装终极解决方案

    近日手贱升级了Itunes,升级过程即报失败,然后卸载所有相关东西,再重装,Itunes安装成功,但是报告无法使用iphone,经过几天摸索,发现是Apple Mobile Device Suppor ...

  8. 调试器带参数调试(OD,EDB)

    小东西,不要在意这些细节-- OD带参数比较简单: 文件-- 打开 --  在最下面有一个参数 KALI LINUX下的EDB 命令格式为  edb –run  "对应程序路径"  ...

  9. 编写C# Windows服务,用于杀死Zsd.exe进程

    最近经常在我的xp系统进程中出现Zsd.exe进程.刚开始他占用内存不是很大.但是过了一段时间就会变成几百M 机器就会变得很卡,网上说Zsd可能是病毒.所以我就想要不写一个Windows服务,让他每隔 ...

  10. Hadoop 学习笔记(二) HDFS API

    4.删除HDFS上的文件 package proj; import java.io.IOException; import org.apache.hadoop.conf.Configuration; ...