mongoDB的简单使用

一 依赖的包:

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.11.3</version>
</dependency>

 

二 要点:

1.mongo分为 db — collections — row 三级。 db对应关系数据库的实例,collections对应表 ,row即对应行
2.文档存储的NOSQL
3.支持主从结构

 

三 初始化:

Mongo mongo = new MongoClient(parseUri(url), getDefaultOptions());

一些参数:
private MongoClientOptions getDefaultOptions() {
return new MongoClientOptions.Builder().socketKeepAlive(true) // 是否保持长链接
.connectTimeout(5000) // 链接超时时间
.socketTimeout(5000) // read数据超时时间
.readPreference(ReadPreference.primary()) // 最近优先策略
.autoConnectRetry(false) // 是否重试机制
.connectionsPerHost(30) // 每个地址最大请求数
.maxWaitTime(1000 * 60 * 2) // 长链接的最大等待时间
.threadsAllowedToBlockForConnectionMultiplier(50) // 一个socket最大的等待请求数
.writeConcern(WriteConcern.NORMAL).build();
}

 

四 插入数据:

DBCollection collection = mongo.getDB(DB).getCollection(COLLECTIONS);
DBObject dbObject = new BasicDBObject();
dbObject.put(“activityName”, vo.getActivityName());
dbObject.put(“adddress”, vo.getAdddress());
….

WriteResult insert = collection.insert(dbObject, WriteConcern.NORMAL);

插入的 insert.getN() 返回的是 0
—————————-实际数据——————————
/* 9 */
{
“_id” : ObjectId(“52836b1de4b0db1dbee47096”),
“activityName” : “hehhehe1”,
“adddress” : “hahaha1”,
“cost” : “123422”,
“endDate” : “2013-10-12”,
“startDate” : “2013-11-12”,
“founderMember” : null,
“joinUserNum” : 1,
“summary” : “hahhaa22”,
“originURL” : “www.dianping.com”,
“type” : 1
}

 

五 查找数据:

简单查找:
—————————-实际数据——————————
/* 9 */
{
“_id” : ObjectId(“52836b1de4b0db1dbee47096”),
“activityName” : “hehhehe1”,
“adddress” : “hahaha1”,
“cost” : “123422”,
“endDate” : “2013-10-12”,
“startDate” : “2013-11-12”,
“founderMember” : null,
“joinUserNum” : 1,
“summary” : “hahhaa22”,
“originURL” : “www.dianping.com”,
“type” : 1
}
查找语法:
// select * from XXX where cost = 123455
DBCollection collection = mongo.getDB(DB).getCollection(COLLECTIONS);
BasicDBObject query = new BasicDBObject(“cost”, “222”);
DBCursor dbCursor = collection.find(query);
for (DBObject dbObject : dbCursor) {
System.out.println(“–“+dbObject.toString());
}

高级查找:
// select * from XXX where cost > 123455
DBCollection collection = mongo.getDB(DB).getCollection(COLLECTIONS);
BasicDBObject query = new BasicDBObject(“cost”, new BasicDBObject(QueryOperators.GT, “123455”));
DBCursor dbCursor = collection.find(query);
for (DBObject dbObject : dbCursor) {
System.out.println(“–“+dbObject.toString());
}
QueryOperators支持
public static final String OR = “$or”;
public static final String AND = “$and”;
public static final String GT = “$gt”;
public static final String GTE = “$gte”;
public static final String LT = “$lt”;
public static final String LTE = “$lte”;
public static final String NE = “$ne”;
public static final String IN = “$in”;
public static final String NIN = “$nin”;
public static final String MOD = “$mod”;
public static final String ALL = “$all”;
public static final String SIZE = “$size”;
public static final String EXISTS = “$exists”;
public static final String ELEM_MATCH = “$elemMatch”;

详细见 QueryOperators类

 

六 更新:

全量更新
—————————-原始数据——————————
/* 9 */
{
“_id” : ObjectId(“52836b1de4b0db1dbee47096”),
“activityName” : “hehhehe1”,
“adddress” : “hahaha1”,
“cost” : “123422”,
“endDate” : “2013-10-12”,
“startDate” : “2013-11-12”,
“founderMember” : null,
“joinUserNum” : 1,
“summary” : “hahhaa22”,
“originURL” : “www.dianping.com”,
“type” : 1
}

更新语句:
DBCollection collection = mongo.getDB(DB).getCollection(COLLECTIONS);
BasicDBObject query = new BasicDBObject(“cost”, “123422”);
BasicDBObject newValue = new BasicDBObject(“cost”, “123456”);

WriteResult update = collection.update(query, newValue);
System.out.println(update.getN()); // update.getN()返回的是影响的行数

—————————-更新数据——————————
/* 9 */
{
“_id” : ObjectId(“52836b1de4b0db1dbee47096”),
“cost” : “123456”
}

若是数据只是填写了部分的话,那么其余的数据都会被丢失掉
局部更新:
—————————-原始数据——————————
/* 9 */
{
“_id” : ObjectId(“527c5a3ce4b01732e9972484”),
“activityName” : “hehhehe”,
“adddress” : “hahaha”,
“cost” : “222”,
“endDate” : “2013-10-12”,
“founderMember” : null,
“joinUserNum” : 1,
“originURL” : “www.dianping.com”,
“source” : “sina”,
“startDate” : “2013-11-12”,
“summary” : “hahhaa”,
“type” : 1
}

// update xxx set cost = 222_new where cosr = 222
DBCollection collection = mongo.getDB(DB).getCollection(COLLECTIONS);
BasicDBObject query = new BasicDBObject(“cost”, “222”);
BasicDBObject newValue = new BasicDBObject(“cost”, “222_new”);
BasicDBObject setValue = new BasicDBObject(“$set”, newValue);

WriteResult update = collection.update(query, setValue);
只更新局部数据

 

七 删除:

// delete from xxx where cost = 123456
DBCollection collection = mongo.getDB(DB).getCollection(COLLECTIONS);
BasicDBObject query = new BasicDBObject(“cost”, “123456”);
WriteResult remove = collection.remove(query, WriteConcern.FSYNC_SAFE);
System.out.println(remove.getN());

//remove.getN()影响的行数 只有在insert的时候是0

 

八 其他:

mongo的order by
指定升序(1)
降序即可(-1)

作者: inter12

在这苦短的人生中,追求点自己的简单快乐

发表评论

电子邮件地址不会被公开。 必填项已用*标注