在mysql,sqlserver和orcale中都提供了java的驅動,為我們封裝好了很多的接口,在java層調用起來了是很方便的。在mongodb中同樣為我們提供了這樣的驅動。和mysql一樣,我們只需將下載好的mongo.jar進行buildPath即可,就可以調用其為我們封裝的接口了。
try {
//建立一個mongo的連接數據庫的對象
Mongo mongo = new Mongo("127.0.0.1:27017");
//查詢所有數據庫的明後才能
ListdataBases = mongo.getDatabaseNames();
// for (String name : dataBases) {
// System.out.println(name);
// }
//創建連接數據庫(mydb)的連接
DB db = mongo.getDB("mydb");
//查詢mydb中所有的集合名稱
Setset = db.getCollectionNames();
// for (String collectionName : set) {
// System.out.println(collectionName);
// }
//查詢students集合中的所有數據
DBCollection dbCollection = db.getCollection("students");
DBCursor dbCursor = dbCollection.find();
//將該cursor序列化成json對象
System.out.println(JSON.serialize(dbCursor));
// while (dbCursor.hasNext()) {
// DBObject object = dbCursor.next();
// System.out.println(object.get("name"));//得到name字段的值
// }
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
新建一個”mongnew”數據庫,並且插入一條數據
//建立一個mongo的連接數據庫的對象
Mongo mongo = new Mongo("127.0.0.1:27017");
//創建一個mongonew數據庫
DB db = mongo.getDB("mongonew");
//創建一個集合,並且插入一條記錄
DBObject dbObject = new BasicDBObject();
dbObject.put("age",22);
dbObject.put("name","張三");
Listfruits = new ArrayList<>();
fruits.add("orange");
fruits.add("apple");
fruits.add("water");
dbObject.put("fruits",fruits);
DBCollection teacherCollection = db.getCollection("teachers");//得到teacher集合
teacherCollection.insert(dbObject);//插入dbObject
批量插入數據
ListdbList = new ArrayList<>();
DBObject object = null;
for (int i = 1; i < 4; i++) {
object = new BasicDBObject();
object.put("age",i);
object.put("name","aName"+i);
dbList.add(object);
}
DBCollection teacherCollection2 = db.getCollection("teachers");//得到teacher集合
teacherCollection2.insert(dbList);//批量插入dbObject,參數是一個list集合
根據條件刪除數據
刪除age=3的數據
DBCollection dbCollection3 = db.getCollection("teachers");
dbCollection3.remove(new BasicDBObject("age",3));
給集合批量增加屬性
DBCollection dbCollection4 = db.getCollection("teachers");
DBObject updateObject = new BasicDBObject();
updateObject.put("$set",new BasicDBObject("passworld","123"));
dbCollection4.update(new BasicDBObject(),updateObject,false,true);
只查詢某些字段
DBCollection dbCollection5 = db.getCollection("teachers");
DBObject keys = new BasicDBObject();
keys.put("_id",false);
keys.put("name",true);
keys.put("passworld",true);
DBCursor dbCursor = dbCollection5.find(null,keys);
DBObject dbObjec = null;
while (dbCursor.hasNext()) {
dbObjec = dbCursor.next();
System.out.println(JSON.serialize(dbObjec));
}
分頁查詢
//分頁查詢
DBCollection dbCollection6 = db.getCollection("teachers");
DBCursor cursor = dbCollection6.find().limit(2).skip(0);
DBObject dbObject = null;
while (cursor.hasNext()) {
dbObject = cursor.next();
System.out.println(JSON.serialize(dbObject));
}
分頁查詢指定的鍵
DBCollection dbCollection6 = db.getCollection("teachers");
DBObject keys = new BasicDBObject();
keys.put("_id",false);
keys.put("name",true);
keys.put("passworld",true);
DBCursor cursor = dbCollection6.find(null,keys).limit(2).skip(0);
DBObject dbObject = null;
while (cursor.hasNext()) {
dbObject = cursor.next(); System.out.println(JSON.serialize(dbObject));
}