MongoDB学习总结三
MogonDB的java驱动示例:
public class DataBase {
public static void main(String[] args) throws Exception {
try{
//1.建立一个mongo的数据库连接对象
Mongo mongo = new Mongo("127.0.0.1:27017");
//System.out.println(mongo);
//2.查询所有的DataBaseName
List<String > dbNameList = mongo.getDatabaseNames();
for(String name :dbNameList){
System.out.println(name);
}
//3.创建相关数据库(foobar)的连接
DB db = mongo.getDB("foobar");
//4.查询出数据库中所有集合的名称
Set<String> collNames = db.getCollectionNames();
System.out.println("===============================");
for(String name : collNames){
System.out.println(name);
}
System.out.println("===============================");
//查询出所有的数据
DBCollection users = db.getCollection("persons");
DBCursor cursor = users.find();
while(cursor.hasNext()){
DBObject obj = cursor.next();
System.out.println(obj.get("name"));
}
System.out.println(cursor.count());
System.out.println(JSON.serialize(cursor));
}catch(Exception e){
}
}
}
一些操作案例:
public class MongoDb {
//建立一个Mongo的数据库连接对象
static Mongo conn = null;
//创建相关数据库的连接
static DB db =null;
public MongoDb(String dataBaseName) throws Exception{
conn = new Mongo("127.0.0.1:27017");
db = conn.getDB(dataBaseName);
}
public static void main(String[] args) throws Exception {
MongoDb mongoDb = new MongoDb("foobar");
//1.创建一个叫javadb的数据库集合
//mongoDb.createCollection("javadb");
//2.为集合javadb添加一条数据
// DBObject dbs = new BasicDBObject();
// dbs.put("name", "sml");
// dbs.put("age", 2);
//
// List<String> books = new ArrayList<String>();
// books.add("extjs");
// books.add("mongodb");
// dbs.put("books", books);
// mongoDb.insert(dbs,"javadb");
// //批量插入数据
// List<DBObject> dbObject = new ArrayList<DBObject>();
// DBObject jim = new BasicDBObject("name","jim");
// DBObject lisi = new BasicDBObject("name","lisi");
// dbObject.add(jim);
// dbObject.add(lisi);
// mongoDb.insertBatch(dbObject,"javadb");
//根据id删除数据
// int count = mongoDb.deleteById("56a830f99dab9f50c2eff5dd","javadb");
// System.out.println(count);
//根据条件删除数据
// DBObject lisi = new BasicDBObject();
// lisi.put("name","lisi");
// int count = mongoDb.deleteByDbs(lisi,"javadb");
// System.out.println(count);
//更新操作,为集合增加email属性
// DBObject dbObject = new BasicDBObject();
// dbObject.put("$set", new BasicDBObject("email","[email protected]"));
// int count = mongoDb.update(new BasicDBObject(),dbObject,false,true,"javadb");
// System.out.println(count);
//查询persons集合中的name和age
// DBObject keys = new BasicDBObject();
// keys.put("_id", false);
// keys.put("name", true);
// keys.put("age", true);
// DBCursor cursor = mongoDb.find(null,keys,"persons");
// while(cursor.hasNext()){
// DBObject obj = cursor.next();
// System.out.println(obj.get("name"));
// }
//分页查询
DBCursor cursor = mongoDb.find(null, null,6,3, "persons");
while(cursor.hasNext()){
DBObject obj = cursor.next();
System.out.print(obj.get("name")+"-->");
System.out.print(obj.get("age")+"-->");
System.out.println(obj.get("email"));
}
conn.close();
}
private DBCursor find(DBObject ref, DBObject keys, int start, int limit, String collName) {
DBCollection coll = db.getCollection(collName);
DBCursor cursor = find(ref, keys,collName);
return cursor.skip(start).limit(limit);
}
public DBCursor find(DBObject ref, DBObject keys, String collName) {
DBCollection coll = db.getCollection(collName);
DBCursor cursor = coll.find(ref, keys);
return cursor;
}
public int update(DBObject find, DBObject dbObject, boolean upsert, boolean multi, String collName) {
DBCollection coll = db.getCollection(collName);
int count = coll.update(find, dbObject,upsert,multi).getN();
return count;
}
public int deleteByDbs(DBObject lisi, String collName) {
DBCollection coll = db.getCollection(collName);
int count = coll.remove(lisi).getN();
return count;
}
public int deleteById(String id, String collName) {
DBCollection coll = db.getCollection(collName);
int count = coll.remove(new BasicDBObject("_id",new ObjectId(id))).getN();
return count;
}
public void insertBatch(List<DBObject> dbObject, String collName) {
DBCollection coll = db.getCollection(collName);
coll.insert(dbObject);
}
//1.创建一个叫javadb的数据库集合
/*public void createCollection(String collName){
DBObject dbs = new BasicDBObject();
db.createCollection("javadb", dbs);
}*/
public void insert(DBObject dbs,String collName){
DBCollection coll = db.getCollection(collName);
coll.insert(dbs);
}
}