对mongoDB的javaAPI操作,网上的帖子不算多,比较零散,下面就贴一下自己总结的一些java访问mongo的常用方法
包括:查找mongo中所有数据库,某个数据库下的所有表,表的所有内容,以及CRUD操作,全表复制等等。
IP和端口号请自行修改,需要的jar包,请到http://www.mongodb.org/自行下载
MongoDBUtil.java
package com.portal.mongo.util;
/**
* Mongo原生操作
* @author LiuKe
* @version 1.0 2013-10-28
*/
import java.net.UnknownHostException;
import java.util.List;
import java.util.Set;
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.MongoException;
import com.portal.model.User;
public class MongoDBUtil{
private static Mongo m = null ;
static {
try {
m =new Mongo("192.168.1.15",33458);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
public static void dropTable(String dataBaseName , String tableName)
{
DB db = m.getDB(dataBaseName);
DBCollection coll = db.getCollection(tableName);
coll.drop();
}
public static void insertData()
{
DB db = m.getDB("cloudData_base");
DBCollection coll = db.getCollection("test");
BasicDBObject doc = new BasicDBObject();
doc.put("userName", "hahaha");
doc.put("password", "123");
doc.put("tryTimes", "0");
doc.put("lastLoginTime", "");
doc.put("thisLoginTime", "");
coll.insert(doc);
// coll.remove(doc);//也可以做这种删除操作
}
public static void deleteData(String dataBaseName , String tableName ,String key , String value)
{
DB db = m.getDB(dataBaseName);
DBCollection coll = db.getCollection(tableName);
BasicDBObject doc = new BasicDBObject();
doc.put(key, value);
System.out.println(coll.findOne(doc));
coll.findAndRemove(doc);
}
public static void queryAll(String dataBaseName , String tableName)
{
DB db = m.getDB(dataBaseName);
DBCollection coll = db.getCollection(tableName);
DBCursor cur = coll.find();
while (cur.hasNext())
{
DBObject o = cur.next();
System.out.println(o.toString());
}
}
public static void queryOne(String dataBaseName , String tableName , String key ,String value)
{
DB db = m.getDB(dataBaseName);
DBCollection coll = db.getCollection(tableName);
BasicDBObject dbo = new BasicDBObject();
dbo.put(key, value);
System.out.println(coll.findOne(dbo));
}
public static void queryOneAndUpdate()throws Exception
{
DB db = m.getDB("String dataBaseName");
DBCollection coll = db.getCollection("table Name");
BasicDBObject dbo1 = new BasicDBObject();
dbo1.put("col1", "value1");
DBObject dbo =coll.findOne(dbo1);
System.out.println("要修改的数据:"+dbo);
BasicDBObject dbo2 = new BasicDBObject();
dbo2.put("col2","value2");
dbo2.put("col3","value3");
coll.findAndModify(dbo, dbo2);
}
public static void copy(String dataBase_src , String table_src , String dataBase_dest , String table_dest)
{
dropTable(dataBase_dest , table_dest );
DB db_src = m.getDB(dataBase_src);
DBCollection coll_src = db_src.getCollection(table_src);
DB db_dest = m.getDB(dataBase_dest);
DBCollection coll_dest = db_dest.getCollection(table_dest);
DBCursor dbCursor = coll_src.find();
while (dbCursor.hasNext())
{
DBObject dbo = dbCursor.next();
coll_dest.insert(dbo);
}
}
public static void getAllTableNames(String dataBaseName)
{
DB db = m.getDB(dataBaseName);
Set<String> colls = db.getCollectionNames();
for (String s : colls) {
System.out.println(s);
}
}
public static void getAllDataBaseNames()
{
List<String> nameList = m.getDatabaseNames();
for (String s : nameList) {
System.out.println(s);
}
}
public static void dropDataBase(String dataBaseName)
{
m.dropDatabase(dataBaseName);
}
public static void main(String[] args) throws Exception {
// 插入一条数据
// insertData();
// 查询一条数据,arg1:dataBaseName , arg2:tableName , arg3:key , arg4:value
// queryOne("dbtester_base_013" , "tester" , "testerId" , "test_137");
// 删除一条数据,arg1:dataBaseName , arg2:tableName , arg3:key , arg4:value
// deleteData("dbtester_base_013" , "tester" , "testerId" , "test_140");
// 查询并修改一条数据
// queryOneAndUpdate();
// 完全覆盖的拷贝arg1:src_dataBase , arg2:src_table , arg3 : dest_dataBase , arg4 : dest_table
// copy("dataBaseName1","tableOfdataBaseName1","dataBaseName2","tableOfdataBaseName2");
// 获得所有数据库名
// getAllDataBaseNames();
// 删除数据库
// dropDataBase("cloudData_base");getAllDataBaseNames();
// 获得某数据库中所有表名,arg1:dataBaseName
// getAllTableNames("cloudData_base");
// 查询表中所有数据,arg1:dataBaseName , arg2:tableName
// queryAll("cloudData_base" , "test" );
// 删除指定表,arg1:dataBaseName , arg2:tableName
// dropTable("cloudData" , "employees" );
}
}