maven:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>```
MongoTest1.java
import com.google.gson.Gson;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.ArrayList;
import java.util.List;
import static com.mongodb.client.model.Sorts.descending;
import static com.mongodb.client.model.Sorts.orderBy;
public class MongoTest1 {
public static void main(String[] args) {
try{
//获得连接(无需验证用户名密码)
MongoClient mongoClient=new MongoClient("localhost",27017);
//连接数据库
MongoDatabase mongoDatabase=mongoClient.getDatabase("5");
System.out.println("Connect to database successfully");
//创建集合
//mongoDatabase.createCollection("testCollection");
//System.out.println("集合创建成功");
//获取集合,打印文档
MongoCollection<Document> collection=mongoDatabase.getCollection("6");
System.out.println("集合 testCollection 选择成功");
//插入文挡
/*Document document=new Document();
document.append("name","cc")
.append("age", 24)
.append("job","battery");
List<Document> documents=new ArrayList<Document>();
documents.add(document);
collection.insertMany(documents);
System.out.println("文档插入成功");*/
//检索所有文档
/*FindIterable<Document> findIterable=collection.find();
MongoCursor<Document> mongoCursor=findIterable.iterator();
while (mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
}*/
//更新文档 将文档中age=18的文档修改为age=24
/*collection.updateMany(Filters.eq("age", 18), new Document("$set",new Document("age",24)));
FindIterable<Document> findIterable=collection.find();
MongoCursor<Document> mongoCursor=findIterable.iterator();
while (mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
}*/
//删除符合条件的第一个文档
//collection.deleteOne(Filters.eq("age", 24));
//删除所有符合条件的文档
//collection.deleteMany(Filters.eq("age", 24));
//Document document=new Document();
//以time排序获得最后一条记录
/*document=collection.find().sort(orderBy(descending("time"))).first();
String documentStr=document.toJson();
MongoResult mongoResult=new Gson().fromJson(documentStr, MongoResult.class);*/
//分页
int pageNo=1;
int pageSize=3;
List<MongoResult> list=new ArrayList<MongoResult>();
MongoCursor<Document> cursor=collection.find().sort(orderBy(descending("time"))).skip((pageNo - 1) * pageSize).limit(pageSize).iterator();
try {
while (cursor.hasNext()) {
String docStr=cursor.next().toJson();
MongoResult mongoResult=new Gson().fromJson(docStr,MongoResult.class);
System.out.println(mongoResult.getResult());
list.add(mongoResult);
}
} finally {
cursor.close();
}
}catch (Exception e){
System.out.println("出现异常");
}
}
}
MongoResult.java:
/**
-
Created by ztc on 2017/3/30.
*/
public class MongoResult {
private String result;
private String time;public String getResult() {
return result;
}public void setResult(String result) {
this.result = result;
}public String getTime() {
return time;
}public void setTime(String time) {
this.time = time;
}
}