首先,肯定是需要jar包了
maven工程,在pow,xml文件中加入以下依赖`
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.4</version>
</dependency>
非maven工程则需要自己找jar包了
MongoDBUtil类
public class MongoDBUtil {
//无密码连接
public static MongoClient getConnect(String host,Integer port){
return new MongoClient(host, port);
}
//有密码验证连接
public static MongoClient getConnectByPassword(String host,Integer port,String username,String databaseName,String passoword){
List<ServerAddress> adds = new ArrayList<>();
//ServerAddress()两个参数分别为 服务器地址 和 端口
ServerAddress serverAddress = new ServerAddress(host,port);
adds.add(serverAddress);
List<MongoCredential> credentials = new ArrayList<>();
//MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
MongoCredential mongoCredential = MongoCredential.createScramSha1Credential("username", "databaseName", "password".toCharArray());
credentials.add(mongoCredential);
//通过连接认证获取MongoDB连接
MongoClient mongoClient = new MongoClient(adds, credentials);
return mongoClient;
}
}
MongoDBHelper类(为了好测试,所以将方法全设为static的)
public class MongoHelper {
private static MongoDatabase database;
static{
MongoClient client=MongoDBUtil.getConnect("127.0.0.1", 27017);
database=client.getDatabase("test");
}
//往集合插入一个文档
public static void insertOne(String collectionName,Document document){
MongoCollection<Document>collection=database.getCollection(collectionName);
collection.insertOne(document);
}
//往集合插入多个文档
public static void insertMany(String collectionName,List<Document> documents){
MongoCollection<Document>collection=database.getCollection(collectionName);
collection.insertMany(documents);
}
//删除集合中符合条件的文档,只删除一个
public static void deleteOne(String collectionName,Bson filter){
MongoCollection<Document>collection=database.getCollection(collectionName);
collection.deleteOne(filter);
}
//删除集合中符合条件的文档,删除多个
public static void deleteMany(String collectionName,Bson filter){
MongoCollection<Document>collection=database.getCollection(collectionName);
collection.deleteMany(filter);
}
//更新符合条件的文档,只更新一个
public static void updateOne(String collectionName,Bson filter,Document document){
MongoCollection<Document>collection=database.getCollection(collectionName);
collection.updateOne(filter, document);
}
//更新符合条件的文档,只更新一个
public static void updateMany(String collectionName,Bson filter,Document document){
MongoCollection<Document>collection=database.getCollection(collectionName);
collection.updateMany(filter, document);
}
//找寻名为collectionName的集合,顺便打印所有的文档
public static void find(String collectionName){
MongoCollection<Document>collection=database.getCollection(collectionName);
System.out.println(collection);
FindIterable<Document> iterable=collection.find();
MongoCursor<Document> cursor=iterable.iterator();
while(cursor.hasNext()){
System.out.println(cursor.next());
}
}
//根据条件查询
public static void findByFilter(String collectionName,Bson filter){
MongoCollection<Document>collection=database.getCollection(collectionName);
System.out.println(collection);
FindIterable<Document> iterable=collection.find(filter);
MongoCursor<Document> cursor=iterable.iterator();
while(cursor.hasNext()){
System.out.println(cursor.next());
}
}
//查询集合的文档个数
public static long findCount(String collectionName){
MongoCollection<Document>collection=database.getCollection(collectionName);
return collection.count();
}
//查询符合条件的集合的文档个数
public static long findCount(String collectionName,Bson filter){
MongoCollection<Document>collection=database.getCollection(collectionName);
return collection.count(filter);
}
public static void main(String[] args) {
/*Document document=new Document("name","张三").append("age", 18);//创建一个文档
Document document1=new Document("name","李四").append("age", 18);
insertOne("teacher",document);
List<Document> documents=new ArrayList<Document>();
documents.add(document1);
insertMany("teacher", documents);*/
Bson filter=Filters.eq("name", "张三");//名字等于张三
/*deleteOne("teacher",filter);
deleteMany("teacher",filter);*/
filter=Filters.gt("age", 18);//年龄大于18
Document document=new Document("$set", new Document("age", 18));//不能直接写new Document("age":18) 不然会将整个文档改成age:18
updateOne("teacher", filter, document);
updateMany("teacher", filter, document);
find("teacher");
filter=Filters.eq("name", "cyz");
findByFilter("teacher",filter);
System.out.println(findCount("teacher"));
}
}