一、簡介
MongoDB常用的JAVA客戶端驅動有兩種:
- mongodb-driver(官方):https://docs.mongodb.com/ecosystem/drivers/
- springdata-mongodb:https://spring.io/projects/spring-data-mongodb
NOTE:
3.6版本的MongoDB,只允許localhost連接,如果您要使用非本地的客戶端連接,需要綁定IP纔可以,在這裏我們關閉mongodb服務端,添加參數--bind_ip
,重新啓動服務。繼續測試~~~
二、maven座標
注意:本博客演示的是官方提供的
mongodb-driver
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.6.1</version>
</dependency>
三、插入
@Test
public void testInsert(){
MongoClient mongoClient = new MongoClient("192.168.128.156", 27017);
// 獲取指定數據庫
MongoDatabase database = mongoClient.getDatabase("test");
// 獲取指定集合
MongoCollection<Document> collection = database.getCollection("users");
// 創建需要插入的單個文檔
Document doc = new Document("name", "MongoDB")
.append("type", "database")
.append("count", 1)
.append("versions", Arrays.asList("v3.2", "v3.0", "v2.6"))
.append("info", new Document("x", 203).append("y", 102));
// 測試插入
collection.insertOne(doc);
// 測試插入多個文檔
ArrayList<Document> documents = new ArrayList<Document>();
for (int i = 0; i < 100; i++) {
documents.add(new Document("i",i));
}
collection.insertMany(documents);
mongoClient.close();
}
四、修改
@Test
public void test3(){
MongoClient mongoClient = new MongoClient("192.168.128.156", 27017);
// 獲取指定數據庫
MongoDatabase database = mongoClient.getDatabase("test");
// 獲取指定集合
MongoCollection<Document> collection = database.getCollection("users");
// update t_user set i = 110 where i = 10
//UpdateResult updateResult = collection.updateOne(new Document("i", 10), new Document("$set", new Document("i", 110)));
// update t_user set i =10 where i < 20
// lt(key,value) 靜態導入 import static com.mongodb.client.model.Filters.lt;
collection.updateMany(lt("i",20),new Document("$set",new Document("i",10).append("test","test value")));
mongoClient.close();
}
五、 刪除
@Test
public void test4(){
MongoClient mongoClient = new MongoClient("192.168.128.156", 27017);
// 獲取指定數據庫
MongoDatabase database = mongoClient.getDatabase("test");
// 獲取指定集合
MongoCollection<Document> collection = database.getCollection("users");
DeleteResult deleteResult = null;
//deleteResult =collection.deleteOne(eq("i", 110));
deleteResult = collection.deleteMany(lt("i", 20));
System.out.println("刪除的文檔數:"+deleteResult.getDeletedCount());
mongoClient.close();
}
六、 查詢
@Test
public void test5(){
MongoClient mongoClient = new MongoClient("192.168.128.156", 27017);
// 獲取指定數據庫
MongoDatabase database = mongoClient.getDatabase("test");
// 獲取指定集合
MongoCollection<Document> collection = database.getCollection("users");
// 查所有
FindIterable<Document> documents = null;
documents = collection.find();
// 條件查詢 select * fromt t_user where i = 20
documents = collection.find(eq("i",20));
// select * from t_user where i <= 20
documents = collection.find(lte("i",30));
// select * from t_user where i = 30 or i = 40 or i = 50
documents = collection.find(in("i",30,40,50));
// SELECT * FROM t_user WHERE i = 50 OR i < 25
documents = collection.find(or(new Document("i",50),lt("i",25)));
// 模糊查詢 參數二:模糊條件
documents = collection.find(regex("test","test"));
// 排序 SELECT * FROM t_user WHERE i >= 90 order by i desc
documents = collection.find(gt("i",90)).sort(Sorts.descending("i"));
// 分頁查詢
documents = collection.find().skip(50).limit(10);
for (Document document : documents) {
System.out.println(document);
}
mongoClient.close();
}