新建maven项目,添加依赖:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
测试代码:
package com.skyer.test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bson.Document;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.QueryOperators;
import com.mongodb.client.ListIndexesIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
public class TestMongo {
// 声明相关变量
MongoClientURI connectionString = null;
MongoClient mongoClient = null;
MongoDatabase database = null;
MongoCollection<Document> collection = null;
/**
* 获取相关对象
*/
@Before
public void getConnection() {
connectionString = new MongoClientURI("mongodb://192.168.88.128:27017"); // 服务器连接地址
mongoClient = new MongoClient(connectionString); // 获取客户端对象
database = mongoClient.getDatabase("skyer"); // 获取数据库
collection = database.getCollection("test"); // 获取连接
}
/**
* 添加一个
*/
@Test
public void insert() {
Document doc = new Document("name", "skyer")
.append("type", "database")
.append("count", 1)
.append("versions", Arrays.asList("v2.7", "v4.2", "v7.2"))
.append("info", new Document("x", 27).append("y", 42));
collection.insertOne(doc);
}
/**
* 批量添加
*/
@Test
public void insertMany() {
List<Document> docs = new ArrayList<Document>();
for (int i = 0; i < 100; i++) {
docs.add(new Document("i", i));
}
collection.insertMany(docs);
}
/**
* 查询首个
*/
@Test
public void queryFirst() {
Document doc = collection.find().first();
System.out.println(doc.toJson());
}
/**
* 查询全部
*/
@Test
public void findAll() {
for (Document doc : collection.find()) {
System.out.println(doc.toJson());
}
}
/**
* 查询全部
*/
@Test
public void findAll2() {
MongoCursor<Document> cursor = collection.find().iterator();
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
}
/**
* 分页查询
*/
@Test
public void findByPage() {
MongoCursor<Document> cursor = collection.find().skip(0).limit(5).iterator(); // 查询前五个记录
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
}
/**
* 查询一个
*/
@Test
public void findOne() {
Document doc = collection.find(new Document("i", 27)).first(); // 查询i值为27的记录
System.out.println(doc.toJson());
}
/**
* 条件查询
*/
@Test
public void findByCondition1() {
BasicDBObject condition = new BasicDBObject("i", new BasicDBObject(QueryOperators.GT, 95)); // 查询i大于95的记录
MongoCursor<Document> cursor = collection.find(condition).iterator();
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
}
/**
* 条件查询
*/
@Test
public void findByCondition2() {
BasicDBObject condition = new BasicDBObject(QueryOperators.AND,
new BasicDBObject[] {
new BasicDBObject().append("i", new BasicDBObject(QueryOperators.GT, 95)),
new BasicDBObject().append("i", new BasicDBObject(QueryOperators.LTE, 97))
}); // 查询95<i<=97的记录
MongoCursor<Document> cursor = collection.find(condition).iterator();
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
}
/**
* 条件查询:in
*/
@Test
public void testIn() {
BasicDBObject condition = new BasicDBObject("i", new BasicDBObject(QueryOperators.IN, new int[] {27, 42}));
MongoCursor<Document> cursor = collection.find(condition).iterator();
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
}
/**
* 条件查询:not in
*/
@Test
public void testNotIn() {
BasicDBObject condition = new BasicDBObject("i", new BasicDBObject(QueryOperators.NIN, new int[] {34, 47}));
MongoCursor<Document> cursor = collection.find(condition).iterator();
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
}
/**
* 创建普通索引
*/
@Test
public void createIndex() {
String result = collection.createIndex(new BasicDBObject("name", 1));
System.out.println(result);
}
/**
* 创建文本索引
*/
@Test
public void createTextIndex() {
String result = collection.createIndex(new Document("name", "text"));
System.out.println(result);
}
/**
* 获取所有索引
*/
@Test
public void getAllIndex() {
ListIndexesIterable<Document> list = collection.listIndexes();
for (Document doc : list) {
System.out.println(doc.toJson());
}
}
/**
* 删除
*/
@Test
public void testDelete() {
DeleteResult result = collection.deleteOne(new BasicDBObject("i", 2));
System.out.println(result.getDeletedCount());
}
/**
* 批量删除
*/
@Test
public void testDeleteMany() {
DeleteResult result = collection.deleteMany(new BasicDBObject("i", new BasicDBObject(QueryOperators.IN, new int[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
})));
System.out.println(result.getDeletedCount());
}
/**
* 更新
*/
@Test
public void testUpdateOne() {
UpdateResult result = collection.updateOne(new BasicDBObject("i", 10), new Document("$set", new Document("i", 110)));
System.out.println(result.getMatchedCount());
}
/**
* 释放资源
*/
@After
public void releaseConnection() {
mongoClient.close();
}
}