Java项目简单操作MongoDB数据库
01)新建Maven项目[NewMongo]
02)pom.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tcc.tcc</groupId>
<artifactId>NewMongo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mongodb/bson -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>3.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.4.1</version>
</dependency>
</dependencies>
</project>
03)MongoDB工具类MongoUtils.java
package com.tcc.util;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
/**
* @Description mongodb数据库工具
* @Author Tcc
* @Date 2018年1月22日下午4:21:41
*/
public class MongoUtils {
/**
* mongodb数据库ip地址
*/
public final static String MONGO_IP_PATH = "192.168.188.128";
/**
* 端口号
*/
public final static Integer MONGO_PORT = 27017;
/**
* 数据库名称
*/
public final static String MONGO_NAME = "mycol";
/**
* 集合名称
*/
public final static String COLLECTION_NAME = "test";
/**
* @MethodDesc 获取collection
* @Return MongoCollection<Document>
* @Author Tcc
* @Date 2018年1月22日下午4:40:21
*/
public static MongoCollection<Document> getCollection(){
MongoClient mongoClient = new MongoClient(MONGO_IP_PATH ,MONGO_PORT);
System.out.println(mongoClient);
MongoDatabase mongoDatabase = mongoClient.getDatabase(MONGO_NAME);
MongoCollection<Document> collection = mongoDatabase.getCollection(COLLECTION_NAME);
System.out.println("Connect to mongodb database successfully");
return collection;
}
}
04)向mongodb中插入数据AddTest.java
package com.tcc.test;
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import com.mongodb.client.MongoCollection;
import com.tcc.util.MongoUtils;
/**
* @Description 在mongodb中添加
* @Author Tcc
* @Date 2018年1月22日下午4:45:21
*/
public class AddTest{
public static void main( String args[] ){
try{
//获取mongodb的collection
MongoCollection<Document> collection = MongoUtils.getCollection();
/**
* 1. 创建文档 org.bson.Document 参数为key-value的格式
* 2. 创建文档集合List<Document>
* 3. 将文档集合插入数据库集合中 mongoCollection.insertMany(List<Document>) 插入单个文档可以用 mongoCollection.insertOne(Document)
* */
Document document = new Document("title", "Mongo").
append("description", "dae").
append("likes", 10).
append("by", "Ff10");
List<Document> documents = new ArrayList<Document>();
documents.add(document);
collection.insertMany(documents);
System.out.println("文档插入成功");
}catch(Exception e){
e.printStackTrace();
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
05)修改数据UpdateTest.java
package com.tcc.test;
import org.bson.Document;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import com.tcc.util.MongoUtils;
/**
* @Description 在mongodb中修改
* @Author Tcc
* @Date 2018年1月22日下午4:44:32
*/
public class UpdateTest{
public static void main( String args[] ){
try{
//获取mongodb的collection
MongoCollection<Document> collection = MongoUtils.getCollection();
//更新文档 将文档中likes=100的文档修改为likes=20
collection.updateMany(Filters.eq("likes", 10), new Document("$set",new Document("likes",20)));
//检索查看结果
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
while(mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
}
}catch(Exception e){
e.printStackTrace();
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
06)查询数据SearchTest.java
package com.tcc.test;
import org.bson.Document;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.tcc.util.MongoUtils;
/**
* @Description 在mongodb中查询
* @Author Tcc
* @Date 2018年1月22日下午4:44:55
*/
public class SearchTest{
public static void main( String args[] ){
try{
//获取mongodb的collection
MongoCollection<Document> collection = MongoUtils.getCollection();
/**
* 1. 获取迭代器FindIterable<Document>
* 2. 获取游标MongoCursor<Document>
* 3. 通过游标遍历检索出的文档集合
* */
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
while(mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
}
}catch(Exception e){
e.printStackTrace();
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
07)删除数据DeleteTest.java
package com.tcc.test;
import org.bson.Document;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import com.tcc.util.MongoUtils;
/**
* @Description 在mongodb中删除
* @Author Tcc
* @Date 2018年1月22日下午4:44:07
*/
public class DeleteTest{
public static void main( String args[] ){
try{
//获取mongodb的collection
MongoCollection<Document> collection = MongoUtils.getCollection();
//删除符合条件的第一个文档
collection.deleteOne(Filters.eq("likes", 20));
//删除所有符合条件的文档
collection.deleteMany (Filters.eq("title", "MongoDB"));
//检索查看结果
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
while(mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
}
}catch(Exception e){
e.printStackTrace();
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/ud_world/article/details/79130900 ud_world的博客)