先导入mongo的jar包.MongoDB.java
package com.util;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
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;
/**
* @author nidayu
* @date 2015-1-4
* @Description xxx
* @version V1.0
*/
public class MongoDB {
static Mongo connection;
static DB db;
public MongoDB(String databaseName) throws UnknownHostException {
connection = new Mongo("127.0.0.1:27017");
db = connection.getDB(databaseName);
}
public static void main(String[] args) throws UnknownHostException {
// 连接数据库
MongoDB mongodb = new MongoDB("ndy");
// 创建集合
String collName = "javadb";
mongodb.createCollection(collName);
// 插入一条数据
DBObject obj = new BasicDBObject();
obj.put("name", "hello");
obj.put("age", "5");
obj.put("email", "dayu.ni@quantgroup.cn");
List<String> books = new ArrayList<String>();
books.add("EXTJS");
books.add("HBase");
obj.put("books", books);
mongodb.insert(obj, collName);
// 批量插入数据
List<DBObject> dbos = new ArrayList<DBObject>();
DBObject dbo1 = new BasicDBObject("name", "aaa");
DBObject dbo2 = new BasicDBObject("name", "bbb");
dbos.add(dbo1);
dbos.add(dbo2);
mongodb.insertBatch(dbos, collName);
// 根据ID删除数据
int count = mongodb.deleteById("54a94f18bb8b785e749b5c2b", collName);
System.out.println(count);
// 根据条件进行删除
DBObject aaa = new BasicDBObject("name", "aaa");
mongodb.deleteByDbs(aaa, collName);
// 更新操作
DBObject update = new BasicDBObject();
update.put("$set", new BasicDBObject("name", "aaa"));
System.out.println(mongodb.update(new BasicDBObject(), update, false, true, collName));
// 查询某些列
DBObject keys = new BasicDBObject();
keys.put("_id", false);
keys.put("name", true);
DBCursor cur = mongodb.find(null, keys, collName);
while (cur.hasNext()) {
DBObject dbo = cur.next();
System.out.println(dbo.get("name"));
}
// 关闭连接
connection.close();
}
// 创建一个集合
public void createCollection(String collection) {
DBObject dbs = new BasicDBObject();
db.createCollection(collection, dbs);
}
// 插入一条数据
public void insert(DBObject obj, String collectionName) {
DBCollection dbcoll = db.getCollection(collectionName);
dbcoll.insert(obj);
}
// 批量插入数据
public void insertBatch(List<DBObject> dbses, String collectionName) {
DBCollection dbcoll = db.getCollection(collectionName);
dbcoll.insert(dbses);
}
// 删除数据
public int deleteById(String id, String collectionName) {
DBCollection dbcoll = db.getCollection(collectionName);
int count = dbcoll.remove(new BasicDBObject("_id", new ObjectId(id))).getN();
return count;
}
// 根据条件进行删除
public int deleteByDbs(DBObject obj, String collectionName) {
DBCollection dbcoll = db.getCollection(collectionName);
int count = dbcoll.remove(obj).getN();
return count;
}
// 更新数据
private int update(BasicDBObject basicDBObject, DBObject update, boolean b, boolean c, String collectionName) {
DBCollection dbcoll = db.getCollection(collectionName);
int count = dbcoll.update(basicDBObject, update, b, c).getN();
return count;
}
// 查询数据
private DBCursor find(DBObject object, DBObject keys, String collectionName) {
DBCollection dbcoll = db.getCollection(collectionName);
DBCursor cur = dbcoll.find(object, keys);
return cur;
// 分页
// return cur.limit(5).skip(0);
}
}