mongoDB学习笔记
package com.cms.test;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.WriteResult;
/**
* mongo 数据库直连测试
* @author Jason
*
*/
public class MongDBTest {
public static void main(String[] args) throws Exception {
List<ServerAddress> list = new ArrayList<ServerAddress>();
// 连接数据库 ip 端口
list.add(new ServerAddress("10.39.XXX.XXX", 27010));
MongoClient mongoClient = new MongoClient(list);
//数据库名称
DB psdoc = mongoClient.getDB("db_center");
//表名
DBCollection collection=psdoc.getCollection("a_test");
BasicDBObject queryObject = null;
DBObject orderBy = new BasicDBObject();
// 不带查询条件查询一条数据
DBCursor find = collection.find().limit(1).sort(orderBy);
/**
* 一般的条件查询
*/
DBObject query = new BasicDBObject("mobile","15333010583");
DBCursor collectionList = collection.find(query );
while (collectionList.hasNext()) {
DBObject next = collectionList.next();
Object real_name = next.get("real_name");
Object mobile = next.get("mobile");
System.out.println(real_name +"====="+mobile );
}
/**
* 全匹配模糊查询 包含120 的电话号码
* 当需要前匹配或后匹配时 删除对应的 .*即可
*/
Pattern queryPattern = Pattern.compile("^.*120.*$", Pattern.CASE_INSENSITIVE);
queryObject = new BasicDBObject();
queryObject.put("mobile",queryPattern);
collectionList = collection.find(queryObject) ;
while (collectionList.hasNext()) {
DBObject next = collectionList.next();
Object real_name = next.get("real_name");
Object mobile = next.get("mobile");
System.out.println(real_name +"====="+mobile );
}
/**
* 查询 比较符号 的用法
*/
// $gt:>
// $gte:>=
// $eq: =
// $ne: !=
// $lt: <
// $lte: <=
// $in: in(后面的值为bson对象数组)
// $nin: not in(后面的值为bson对象数组)
queryObject = new BasicDBObject();
queryObject.put("mobile", new BasicDBObject("$gt","19200273120"));
DBCursor select1 = collection.find(queryObject).limit(1);
while (select1.hasNext()) {
DBObject next = select1.next();
Object real_name = next.get("real_name");
Object mobile = next.get("mobile");
Object create_time = next.get("create_time");
System.out.println(real_name +"====="+mobile );
}
/**
* 查询 in的用法
*/
BasicDBList values = new BasicDBList();
values.add("19200273121");
values.add("15312010583");
queryObject = new BasicDBObject();
queryObject.put("mobile",new BasicDBObject("$in",values));
DBCursor select3 = collection.find(queryObject);
while (select3.hasNext()) {
DBObject next = select3.next();
Object real_name = next.get("real_name");
Object mobile = next.get("mobile");
System.out.println(real_name +"====="+mobile );
}
/**
* 时间查询 数据库看到的时间不是真实时间 加8小时后才是正确的时间
*/
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DBObject dbObject = new BasicDBObject();
String startDate = "2018-06-13 16:00:06";
String endDate = "2019-06-13 16:06:46";
dbObject.put("$gte", sdf.parse(startDate));
dbObject.put("$lte", sdf.parse(endDate));
queryObject = new BasicDBObject();
queryObject.put("date",dbObject);
DBCursor select4 = collection.find(queryObject);
while (select4.hasNext()) {
DBObject next = select4.next();
Object real_name = next.get("name");
Object create_time = next.get("date");
String str = sdf.format(create_time);
System.out.println(real_name +"=========="+str);
}
/**
* 插入数据
*/
queryObject = new BasicDBObject();
queryObject.put("real_name", "zhangs");
queryObject.put("name", "张三");
queryObject.put("mobile", "15333010583");
queryObject.put("address", "Beijing");
queryObject.put("create_time", new Date());
WriteResult insert = collection.insert(queryObject);
/**
* 修改字段参数
*/
//查询name 叫zhangs的
queryObject = new BasicDBObject();
queryObject.put("name", "zhangs");
// 修改对象
dbObject= new BasicDBObject();
dbObject.put("name", "zhangs1111");
DBObject update= new BasicDBObject();
update.put("$set", dbObject);
//修改find方法找到的一个对象
// collection.update(queryObject, update);
// 修改find方法查询到数据集合
WriteResult updateMulti = collection.updateMulti(queryObject, update);
System.out.println(updateMulti.getN());
/**
* 删除数据
*/
queryObject = new BasicDBObject();
queryObject.put("name", "zhangs");
WriteResult remove = collection.remove(queryObject);
System.out.println(remove.getN());
// 修改集合名称
// collection.rename("a_test1");
}
}