1. 环境配置
pom.xml 依赖
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2. 单元测试
命令行创建数据库
use spitdb
创建集合spit并添加数据
db.spit.insert({_id:"1",content:"996还是007,年轻人该何去何从",userid:"1012",nickname:"忧郁猫",visits:NumberInt(2020)});
db.spit.insert({_id:"2",content:"关于秃头程序员如何护发",userid:"1013",nickname:"李大富",visits:NumberInt(1023)});
db.spit.insert({_id:"3",content:"不会做饭的程序员不是一个好厨子",userid:"1013",nickname:"李大嘴",visits:NumberInt(111)});
db.spit.insert({_id:"4",content:"人到中年就发福",userid:"1014",nickname:"土肥圆",visits:NumberInt(1223)});
db.spit.insert({_id:"4",content:"从删库跑路到精通java",userid:"1014",nickname:"工程狗",parentid:"2",visits:NumberInt(1223)});
2.1 查询记录
@Test
public void findTest() {
MongoClient client = new MongoClient("192.168.1.90", 27017);
MongoDatabase spitdb = client.getDatabase("spitdb");
MongoCollection<Document> spit = spitdb.getCollection("spit");
FindIterable<Document> documents = spit.find();
BasicDBObject dbObject = new BasicDBObject("userid", "1013");
FindIterable<Document> filterFind = spit.find(dbObject);
BasicDBObject gtObject = new BasicDBObject("visits", new BasicDBObject("$gt", 1000));
FindIterable<Document> gtFilterFind = spit.find(gtObject);
for (Document doc : gtFilterFind) {
System.out.println("_id: " + doc.get("_id"));
System.out.println("content: " + doc.getString("content"));
System.out.println("userid: " + doc.getString("userid"));
System.out.println("nickname: " + doc.getString("nickname"));
System.out.println("--------------------------------------------");
}
client.close();
}
2.2 新增记录
@Test
public void saveTest() {
MongoClient client = new MongoClient("192.168.1.90", 27017);
MongoDatabase spitdb = client.getDatabase("spitdb");
MongoCollection<Document> spit = spitdb.getCollection("spit");
Map<String, Object> map = new HashMap<String, Object>();
map.put("_id", "5");
map.put("content", "人到中年不得已,一顿能吃三碗米");
map.put("userdid", "1015");
map.put("nickname", "李富贵");
map.put("publishtime", new Date());
Document document = new Document(map);
spit.insertOne(document);
FindIterable<Document> documents = spit.find();
for (Document doc : documents) {
System.out.println("_id: " + doc.get("_id"));
System.out.println("content: " + doc.getString("content"));
System.out.println("userid: " + doc.getString("userid"));
System.out.println("nickname: " + doc.getString("nickname"));
System.out.println("--------------------------------------------");
}
client.close();
}
2.3 更新记录
@Test
public void updateTest() {
MongoClient client = new MongoClient("192.168.1.90", 27017);
MongoDatabase spitdb = client.getDatabase("spitdb");
MongoCollection<Document> spit = spitdb.getCollection("spit");
Document document = new Document("nickname", "张富贵");
UpdateResult a = spit.updateOne(Filters.eq("nickname", "李富贵"), new Document("$set", document));
FindIterable<Document> documents = spit.find();
for (Document doc : documents) {
System.out.println("_id: " + doc.get("_id"));
System.out.println("content: " + doc.getString("content"));
System.out.println("userid: " + doc.getString("userid"));
System.out.println("nickname: " + doc.getString("nickname"));
System.out.println("--------------------------------------------");
}
client.close();
}
2.4 删除记录
@Test
public void deleteTest() {
MongoClient client = new MongoClient("192.168.1.90", 27017);
MongoDatabase spitdb = client.getDatabase("spitdb");
MongoCollection<Document> spit = spitdb.getCollection("spit");
DeleteResult res = spit.deleteOne(Filters.eq("userid", "1014"));
System.out.println(res);
FindIterable<Document> documents = spit.find();
for (Document doc : documents) {
System.out.println("_id: " + doc.get("_id"));
System.out.println("content: " + doc.getString("content"));
System.out.println("userid: " + doc.getString("userid"));
System.out.println("nickname: " + doc.getString("nickname"));
System.out.println("--------------------------------------------");
}
client.close();
}
|