国内 mongodb-driver jar 下载地址:http://central.maven.org/maven2/org/mongodb/mongo-java-driver/
maven依赖:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.2.0</version>
</dependency>
mongo3.x以前
package da.test;
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;
import com.mongodb.MongoException;
public class MongoDb {
// 1.建立一个Mongo的数据库连接对象
static Mongo connection = null;
// 2.创建相关数据库的连接
static DB db = null;
public MongoDb(String dbName) throws UnknownHostException, MongoException {
connection = new Mongo("127.0.0.1:27017");
db = connection.getDB(dbName);
}
public static void main(String[] args) throws UnknownHostException, MongoException {
// 实例化
MongoDb mongoDb = new MongoDb("foobar");
/**
* 1.创建一个名字叫javadb的数据库
*/
// mongoDb.createCollection("javadb");
/**
* 2.为集合javadb添加一条数据
*/
// DBObject dbs = new BasicDBObject();
// dbs.put("name", "joker");
// dbs.put("age", 2);
// List<String> books = new ArrayList<String>();
// books.add("EXTJS");
// books.add("MONGODB");
// dbs.put("books", books);
// mongoDb.insert(dbs, "javadb");
/**
* 3.批量插入数据
*/
// List<DBObject> dbObjects = new ArrayList<DBObject>();
// DBObject jim = new BasicDBObject("name", "jim");
// DBObject lisi = new BasicDBObject("name", "lisi");
// dbObjects.add(jim);
// dbObjects.add(lisi);
// mongoDb.insertBatch(dbObjects, "javadb");
/**
* 4.根据ID删除数据
*/
// mongoDb.deleteById("5a44fc54105f001424061553", "javadb");
/**
* 5.根据条件删除数据
*/
// DBObject lisi = new BasicDBObject();
// lisi.put("name", "lisi");
// mongoDb.deleteByDbs(lisi, "javadb");
/**
* 6.更新操作,为集合增加email属性
*/
// DBObject update = new BasicDBObject();
// update.put("$set", new BasicDBObject("eamil", "jokerda@qq.com"));
// mongoDb.update(new BasicDBObject(), update, false, true, "javadb");
/**
* 7.查询出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 object = cursor.next();
// System.out.println(object.get("name") + "--" + object.get("age"));
// }
/**
* 7.查询出年龄大于26岁并且英语成绩小于80分
*/
DBObject ref = new BasicDBObject();
ref.put("age", new BasicDBObject("$gte", 26));
ref.put("e", new BasicDBObject("$lte", 80));
DBCursor cursor = mongoDb.find(ref, null, "persons");
while (cursor.hasNext()) {
DBObject object = cursor.next();
System.out.print(object.get("name") + "-->");
System.out.print(object.get("age") + "-->");
System.out.println(object.get("e"));
}
/**
* 8.分页例子
*/
// DBCursor cursor = mongoDb.find(null, null, 6, 3, "persons");
// while (cursor.hasNext()) {
// DBObject object = cursor.next();
// System.out.print(object.get("name") + "-->");
// System.out.print(object.get("age") + "-->");
// System.out.println(object.get("e"));
// }
// 关闭连接对象
connection.close();
}
/**
* 穿件一个数据库集合
*
* @param collName
* 集合名称
* @param db
* 数据库实例
*/
public void createCollection(String collName) {
DBObject dbs = new BasicDBObject();
db.createCollection("javadb", dbs);
}
/**
* 为相应的集合添加数据
*
* @param dbs
* @param collName
*/
public void insert(DBObject dbs, String collName) {
// 1.得到集合
DBCollection coll = db.getCollection(collName);
// 2.插入操作
coll.insert(dbs);
}
/**
* 为集合批量插入数据
*
* @param dbses
* @param collName
*/
public void insertBatch(List<DBObject> dbses, String collName) {
// 1.得到集合
DBCollection coll = db.getCollection(collName);
// 2.插入操作
coll.insert(dbses);
}
/**
* 根据id删除数据
*
* @param id
* @param collName
* @return 返回影响的数据条数
*/
public void deleteById(String id, String collName) {
// 1.得到集合
DBCollection coll = db.getCollection(collName);
DBObject dbs = new BasicDBObject("_id", new ObjectId(id));
coll.remove(dbs);
}
/**
* 根据条件删除数据
*
* @param id
* @param collName
* @return 返回影响的数据条数
*/
public void deleteByDbs(DBObject dbs, String collName) {
// 1.得到集合
DBCollection coll = db.getCollection(collName);
coll.remove(dbs);
}
/**
* 更新数据
*
* @param find
* 查询器
* @param update
* 更新器
* @param upsert
* 更新或插入
* @param multi
* 是否批量更新
* @param collName
* 集合名称
* @return 返回影响的数据条数
*/
public void update(DBObject find, DBObject update, boolean upsert, boolean multi, String collName) {
// 1.得到集合
DBCollection coll = db.getCollection(collName);
coll.update(find, update, upsert, multi);
}
/**
* 查询器(分页)
*
* @param ref
* @param keys
* @param start
* @param limit
* @return
*/
public DBCursor find(DBObject ref, DBObject keys, int start, int limit, String collName) {
DBCursor cur = find(ref, keys, collName);
return cur.limit(limit).skip(start);
}
/**
* 查询器(不分页)
*
* @param ref
* @param keys
* @param start
* @param limit
* @param collName
* @return
*/
public DBCursor find(DBObject ref, DBObject keys, String collName) {
// 1.得到集合
DBCollection coll = db.getCollection(collName);
DBCursor cur = coll.find(ref, keys);
return cur;
}
}
3.x以后
package da.test;
import java.net.UnknownHostException;
import java.util.List;
import org.bson.Document;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
public class MongoDb2 {
// 1.建立一个Mongo的数据库连接对象
private static MongoClient mongoClient = null;
// 2.创建相关数据库的连接
private static MongoDatabase mongoDatabase = null;
public MongoDb2(String dbName) throws UnknownHostException, MongoException {
mongoClient = new MongoClient("localhost", 27017);
mongoDatabase = mongoClient.getDatabase(dbName);
}
public static void main(String args[]) throws Exception {
// 实例化
MongoDb2 mongoDb = new MongoDb2("foobar");
/**
* 1.创建一个名字叫javadb的数据库
*/
// mongoDb.createCollection("javadb");
/**
* 2.为集合javadb添加一条数据
*/
// Document document = new Document("name", "joker").append("age", "2");
// List<String> books = new ArrayList<String>();
// books.add("java");
// books.add("mongodb");
// document.append("books", books);
// mongoDb.insert(document, "javadb");
/**
* 3.批量插入数据
*/
// List<Document> docs = new ArrayList<>();
// docs.add(new Document("name", "jim"));
// docs.add(new Document("name", "lisi"));
// mongoDb.insert(docs, "javadb");
/**
* 4.根据条件删除数据
*/
// mongoDb.deleteByName("name", "jim", "javadb");
/**
* 5.更新操作,为集合增加email属性
*/
// Document doc = new Document("$set", new Document("email",
// "4321@qq.com"));
// mongoDb.update("javadb", new Document().append("name", "lisi"), doc);
/**
* 6.批量更新
*/
// Document doc = new Document("$set", new Document("hobby",
// "basketball"));
// mongoDb.batchUpdate("javadb", new Document(), doc);
/**
* 7.查询出persons
*/
// Document doc = new Document();
// MongoCursor<Document> cursor = mongoDb.find(doc, "persons");
// while (cursor.hasNext()) {
// Document next = cursor.next();
// System.out.println(next.get("name") + "--" + next.get("age"));
// }
/**
* 8.分页查询persons
*/
// Document doc = new Document();
// Document sort = new Document("age", 1);
// MongoCursor<Document> cursor = mongoDb.find(doc, "persons", 2, 4,
// sort);
// while (cursor.hasNext()) {
// Document next = cursor.next();
// System.out.println(next.get("name") + "--" + next.get("age"));
// }
/**
* 9.查询出年龄大于26岁并且英语成绩小于80分
*/
Document doc = new Document();
doc.append("age", new Document("$gte", 26));
doc.append("e", new BasicDBObject("$lte", 80));
MongoCursor<Document> cursor = mongoDb.find(doc, "persons");
while (cursor.hasNext()) {
Document next = cursor.next();
System.out.println(next.toJson());
}
// 关闭连接对象
mongoClient.close();
}
/**
* 创建一个数据库集合
*
* @param collName
* 集合名称
* @param mongoDatabase
* 数据库实例
*/
public void createCollection(String collName) {
mongoDatabase.createCollection("javadb");
}
/**
* 为相应的集合添加数据
*/
public void insert(Document doc, String collName) {
// 1.得到集合
MongoCollection<Document> collection = mongoDatabase.getCollection(collName);
// 2.插入操作
collection.insertOne(doc);
}
/**
* 为集合批量插入数据
*/
public void insert(List<Document> docs, String collName) {
// 1.得到集合
MongoCollection<Document> collection = mongoDatabase.getCollection(collName);
// 2.插入操作
collection.insertMany(docs);
}
/**
* 根据条件删除数据
*/
public void deleteByName(String name, String value, String collName) {
// 1.得到集合
MongoCollection<Document> collection = mongoDatabase.getCollection(collName);
collection.deleteOne(Filters.eq(name, value));
}
/**
* 更新数据
*/
public void update(String collName, Document find, Document doc) {
// 1.得到集合
MongoCollection<Document> collection = mongoDatabase.getCollection(collName);
collection.updateOne(find, doc);
}
/**
* 批量更新数据
*/
public void batchUpdate(String collName, Document find, Document doc) {
// 1.得到集合
MongoCollection<Document> collection = mongoDatabase.getCollection(collName);
collection.updateMany(find, doc);
}
/**
* 查询器(不分页)
*/
public MongoCursor<Document> find(Document doc, String collName) {
// 1.得到集合
MongoCollection<Document> collection = mongoDatabase.getCollection(collName);
FindIterable<Document> findIterable = collection.find(doc);
// System.out.println(collection.count());
MongoCursor<Document> mongoCursor = findIterable.iterator();
return mongoCursor;
}
/**
* 查询器(分页,排序)
*/
public MongoCursor<Document> find(Document doc, String collName, Integer page, Integer size, Document sort) {
// 1.得到集合
MongoCollection<Document> collection = mongoDatabase.getCollection(collName);
FindIterable<Document> findIterable = collection.find(doc).limit(size).skip((page - 1) * size).sort(sort);
// System.out.println(collection.count());
MongoCursor<Document> mongoCursor = findIterable.iterator();
return mongoCursor;
}
}