MongoDB(二)-- Java API 实现增删改查

时间:2023-03-08 18:45:23
MongoDB(二)-- Java API 实现增删改查

一、下载jar包

  http://central.maven.org/maven2/org/mongodb/mongo-java-driver/

二、代码实现

 package com.xbq.mongodb;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bson.types.ObjectId;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.util.JSON; /**
* @ClassName: MongoDBTest
* @Description: TODO MongoDB 增删改查 操作,包含批量操作
* @author xbq
* @version 1.0
* @date 2017-4-5 上午11:50:06
*/
public class MongoDBTest { private static final String HOST = "192.168.242.129";
private static final int PORT = 27017;
private static final String DB_NAME = "testDB";
private static Mongo mongo;
private static DB db; static {
// 连接到MongoDB
mongo = new Mongo(HOST, PORT);
// 打开数据库 testDB
db = mongo.getDB(DB_NAME);
} public static void main(String[] args) {
// 获取集合 xbqTable,若该集合不存在,mongoDB将自动创建该集合
DBCollection dbCollection = db.getCollection("testTable"); // 查询该数据库所有的集合名
for(String name : mongo.getDatabaseNames()){
System.out.println(name);
} // addOne(dbCollection);
// addList(dbCollection);
// addByJson(dbCollection); // deleteOne(dbCollection);
// deleteByIn(dbCollection);
// deleteAll(dbCollection); // updateOne(dbCollection);
// updateMulti(dbCollection); // queryOne(dbCollection);
// queryPage(dbCollection);
// queryRange(dbCollection);
queryList(dbCollection); } // ====================================查询开始==============================================
/**
* @Title: queryOne
* @Description: TODO 查询 name为 张三的 一条记录
* @param dbCollection
* @return: void
*/
public static void queryOne(DBCollection dbCollection){
DBObject documents = new BasicDBObject("name","张三");
DBObject result = dbCollection.findOne(documents);
System.out.println(result);
} /**
* @Title: queryPage
* @Description: TODO 分页查询 , 查询 跳过前2条 后的 3条 数据
* @param dbCollection
* @return: void
*/
public static void queryPage(DBCollection dbCollection){
DBCursor cursor = dbCollection.find().skip(2).limit(3);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} /**
* @Title: queryRange
* @Description: TODO 范围查询,查询 第3条 到 第5条 之间的记录
* @param dbCollection
* @return: void
*/
public static void queryRange(DBCollection dbCollection) {
DBObject range = new BasicDBObject();
range.put("$gte", 50);
range.put("$lte", 52); DBObject dbObject = new BasicDBObject();
dbObject.put("age", range);
DBCursor cursor = dbCollection.find(dbObject);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} /**'
* @Title: queryList
* @Description: TODO 查询出全部的 记录
* @param dbCollection
* @return: void
*/
public static void queryList(DBCollection dbCollection) {
DBCursor cursor = dbCollection.find();
DBObject dbObject = null;
while(cursor.hasNext()){
dbObject = cursor.next();
System.out.println(dbObject);
}
} // ====================================增加开始==============================================
/**
* @Title: addOne
* @Description: TODO 新增 一条记录
* @param dbCollection
* @return: void
*/
public static void addOne(DBCollection dbCollection){
DBObject documents = new BasicDBObject("name","张三").append("age", 45).append("sex", "男").append("address",
new BasicDBObject("postCode", 100000).append("street", "深南大道888号").append("city", "深圳"));
dbCollection.insert(documents);
} /**
* @Title: addList
* @Description: TODO 批量新增 记录 , 增加的记录 中 可以使用各种数据类型
* @param dbCollection
* @return: void
*/
public static void addList(DBCollection dbCollection){
List<DBObject> listdbo= new ArrayList<DBObject>();
DBObject dbObject = new BasicDBObject();
dbObject.put("name", "老王");
// 可以直接保存List类型
List<String> list = new ArrayList<String>();
list.add("非隔壁老王");
dbObject.put("remark", list);
listdbo.add(dbObject); dbObject = new BasicDBObject();
// 可以直接保存map
Map<String,List<String>> map = new HashMap<String,List<String>>();
List<String> hobbys = new ArrayList<String>();
hobbys.add("看花");
hobbys.add("采花");
map.put("爱好", hobbys);
dbObject.put("hobby", map);
listdbo.add(dbObject); dbObject = new BasicDBObject();
dbObject.put("name", "老张");
dbObject.put("age", 52);
dbObject.put("job", "看守老王");
dbObject.put("remark", new BasicDBObject("address", "广东省深圳市").append("street", "深南大道888号"));
listdbo.add(dbObject); dbCollection.insert(listdbo);
} /**
* @Title: addByJson
* @Description: TODO json转对象后 ,执行新增
* @param dbCollection
* @return: void
*/
public static void addByJson(DBCollection dbCollection){
String json = "{ \"name\" : \"王五\" , \"age\" : 66 , \"job\" : \"看守老王\" , \"remark\" : { \"address\" : \"广东省深圳市\" , \"street\" : \"深南大道888号\"}}";
DBObject dbObject = (DBObject) JSON.parse(json);
dbCollection.insert(dbObject);
} // ====================================修改开始==============================================
/**
* @Title: update
* @Description: TODO 修改指定记录
* @param dbCollection
* @return: void
*/
public static void updateOne(DBCollection dbCollection) {
// 先根据id查询将 这条 记录查询出来
DBObject qryResult = dbCollection.findOne(new ObjectId("58e4a11c6c166304f0635958"));
// 修改指定的值
qryResult.put("age", 55); DBObject olddbObject = new BasicDBObject();
olddbObject.put("_id", new ObjectId("58e4a11c6c166304f0635958"));
dbCollection.update(olddbObject, qryResult);
} /**
* @Title: updateMulti
* @Description: TODO 修改 多条记录
* @param dbCollection
* @return: void
*/
public static void updateMulti(DBCollection dbCollection) {
DBObject newdbObject = new BasicDBObject();
newdbObject.put("name", "张三");
newdbObject.put("address", "广东深圳");
newdbObject.put("remark", "张三是一个NB的Coder"); DBObject olddbObject = new BasicDBObject();
olddbObject.put("name", "张三");
// 需要加上这个
DBObject upsertValue = new BasicDBObject("$set", newdbObject);
// 后面的两个参数:1.若所更新的数据没有,则插入 ; 2、同时更新多个符合条件的文档(collection)
dbCollection.update(olddbObject, upsertValue, true, true);
} // ====================================删除开始==============================================
/**
* @Title: deleteFirst
* @Description: TODO 删除第一个
* @param
* @return: void
*/
public static void deleteFirst(DBCollection dbCollection){
DBObject dbObject = dbCollection.findOne();
dbCollection.remove(dbObject);
} /**
* @Title: deleteOne
* @Description: TODO 删除指定的一条记录
* @param dbCollection
* @return: void
*/
public static void deleteOne(DBCollection dbCollection){
DBObject dbObject = new BasicDBObject();
dbObject.put("_id", new ObjectId("58e49c2d6c166309e0d50484"));
dbCollection.remove(dbObject);
} /**
* @Title: deleteByIn
* @Description: TODO 删除多条记录 例如:select * from tb where name in('12','34')
* @param dbCollection
* @return: void
*/
public static void deleteByIn(DBCollection dbCollection) {
List<String> list = new ArrayList<String>();
list.add("老张");
list.add("老王");
list.add("张三");
DBObject dbObject = new BasicDBObject("$in", list); DBObject delObject = new BasicDBObject();
delObject.put("name", dbObject);
dbCollection.remove(delObject);
} /**
* @Title: deleteAll
* @Description: TODO 删除全部的记录
* @param dbCollection
* @return: void
*/
public static void deleteAll(DBCollection dbCollection){
DBCursor cursor = dbCollection.find();
while(cursor.hasNext()){
dbCollection.remove(cursor.next());
}
}
}

三、源码下载

  点我下载