Java操作MongoDB
首先需要添加进入Java-MongoDB驱动包
java操作Mongodb常用的几个类:
Mongo:连接服务器,执行一些数据库操作的选项,如新建立一个数据库等
DB:对应一个数据库,可以用来建立集合等操作
DBCollection:对应一个集合(类似表),可能是我们用得最多的,可以添加删除记录等
DBObject接口和BasicDBObject对象:表示一个具体的记录,BasicDBObject实现了DBObject,是key-value的数据结构,用起来和HashMap是基本一致的。
DBCursor:用来遍历取得的数据,实现了Iterable和Iterator
具体实现:
package m1;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
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.QueryOperators;
public class TT {
Mongo mongo;
public TT(Mongo mongo) {
this.mongo = mongo;
}
public void insert() {
DB db = mongo.getDB("data1");
DBCollection collection = db.getCollection("user");
BasicDBObject basicDBObject = new BasicDBObject();
basicDBObject.put("name", "KKK");
basicDBObject.put("age",15);
basicDBObject.put("sex","man");
collection.insert(basicDBObject);
}
public void insert_Many() {
DB db = mongo.getDB("data1");
DBCollection collection = db.getCollection("u1");
List<DBObject> objects = new LinkedList<>();
objects.add(new BasicDBObject("name","a").append("age",77).append("sex","woman"));
objects.add(new BasicDBObject("name","b").append("age",99).append("sex","woman"));
objects.add(new BasicDBObject("name","c").append("age",99).append("sex","woman"));
objects.add(new BasicDBObject("name","d").append("age",99).append("sex","woman"));
objects.add(new BasicDBObject("name","e").append("age",99).append("sex","woman"));
collection.insert(objects);
}
public void read_ALL() {
DB db = mongo.getDB("data1");
DBCollection collection = db.getCollection("u1");
DBCursor cursor = collection.find();
while (cursor.hasNext()) {
DBObject object = cursor.next();
System.out.println(object);
}
}
public void read() {
DB db = mongo.getDB("data1");
DBCollection collection = db.getCollection("u1");
DBCursor cursor = collection.find();
while (cursor.hasNext()) {
BasicDBObject basicDBObject = (BasicDBObject) cursor.next();
if (basicDBObject!=null) {
System.out.println(basicDBObject.getString("name"));
System.out.println(basicDBObject.getInt("age"));
System.out.println(basicDBObject.getString("sex"));
}
}
}
public void find() {
DB db = mongo.getDB("data1");
DBCollection collection = db.getCollection("user");
BasicDBObject queryObject = new BasicDBObject("name","ppp");
DBObject object = collection.findOne(queryObject);
System.out.println(object.get("age"));
}
public void find_1() {
DB db = mongo.getDB("data1");
DBCollection collection = db.getCollection("user");
BasicDBObject ageObj = new BasicDBObject("age",new BasicDBObject("$gt",23));
BasicDBObject sexObj = new BasicDBObject("sex","woman");
BasicDBObject andObj = new BasicDBObject("$and",Arrays.asList(ageObj,sexObj));
DBCursor cursor = collection.find(andObj);
while(cursor.hasNext()){
DBObject obj = cursor.next();
System.out.println(obj.toString());
}
}
public void update() {
DB db = mongo.getDB("data1");
DBCollection collection = db.getCollection("u1");
DBCursor cursor = collection.find();
BasicDBObject doc = new BasicDBObject();
BasicDBObject res = new BasicDBObject();
res.put("age",55);
doc.put("$set",res);
collection.update(new BasicDBObject(),doc,false,true);
}
public void delete() {
DB db = mongo.getDB("data1");
DBCollection collection = db.getCollection("u1");
BasicDBObject object = new BasicDBObject();
object.put("name","a");
collection.remove(object);
}
}