如何安装mongodb,请参考http://blog.youkuaiyun.com/zpf336/article/details/50765789
注:此文基于mongoDb API 3.2.0进行的测试。
测试代码如下:
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>mongo </groupId>
-
<artifactId>mongodb </artifactId>
-
<version>0.0.1-SNAPSHOT </version>
-
<packaging>jar </packaging>
-
-
<name>mongodb </name>
-
<url>http://maven.apache.org </url>
-
-
<properties>
-
<project.build.sourceEncoding>UTF-8 </project.build.sourceEncoding>
-
</properties>
-
-
<dependencies>
-
<dependency>
-
<groupId>junit </groupId>
-
<artifactId>junit </artifactId>
-
<version>4.12 </version>
-
<scope>test </scope>
-
</dependency>
-
<dependency>
-
<groupId>org.mongodb </groupId>
-
<artifactId>mongo-java-driver </artifactId>
-
<version>3.2.0 </version>
-
</dependency>
-
-
</dependencies>
-
</project>
-
package mongo.mongodb;
-
-
import java.util.ArrayList;
-
import java.util.List;
-
-
import org.bson.Document;
-
import org.bson.conversions.Bson;
-
-
import com.mongodb.MongoClient;
-
import com.mongodb.client.FindIterable;
-
import com.mongodb.client.MongoCollection;
-
import com.mongodb.client.MongoCursor;
-
import com.mongodb.client.MongoDatabase;
-
import com.mongodb.client.result.UpdateResult;
-
-
public class MongoDb {
-
private static MongoCollection<Document> collection;
-
-
/**
-
* 链接数据库
-
*
-
* @param databaseName
-
* 数据库名称
-
* @param collectionName
-
* 集合名称
-
* @param hostName
-
* 主机名
-
* @param port
-
* 端口号
-
*/
-
public static void connect(String databaseName, String collectionName,
-
String hostName, int port) {
-
"resource")(
-
MongoClient client = new MongoClient(hostName, port);
-
MongoDatabase db = client.getDatabase(databaseName);
-
collection = db.getCollection(collectionName);
-
System.out.println(collection);
-
}
-
-
/**
-
* 插入一个文档
-
*
-
* @param document
-
* 文档
-
*/
-
public static void insert(Document document) {
-
collection.insertOne(document);
-
}
-
-
/**
-
* 查询所有文档
-
*
-
* @return 所有文档集合
-
*/
-
public static List<Document> findAll() {
-
List<Document> results = new ArrayList<Document>();
-
FindIterable<Document> iterables = collection.find();
-
MongoCursor<Document> cursor = iterables.iterator();
-
while (cursor.hasNext()) {
-
results.add(cursor.next());
-
}
-
-
return results;
-
}
-
-
/**
-
* 根据条件查询
-
*
-
* @param filter
-
* 查询条件 //注意Bson的几个实现类,BasicDBObject, BsonDocument,
-
* BsonDocumentWrapper, CommandResult, Document, RawBsonDocument
-
* @return 返回集合列表
-
*/
-
public static List<Document> findBy(Bson filter) {
-
List<Document> results = new ArrayList<Document>();
-
FindIterable<Document> iterables = collection.find(filter);
-
MongoCursor<Document> cursor = iterables.iterator();
-
while (cursor.hasNext()) {
-
results.add(cursor.next());
-
}
-
-
return results;
-
}
-
-
/**
-
* 更新查询到的第一个
-
*
-
* @param filter
-
* 查询条件
-
* @param update
-
* 更新文档
-
* @return 更新结果
-
*/
-
public static UpdateResult updateOne(Bson filter, Bson update) {
-
UpdateResult result = collection.updateOne(filter, update);
-
-
return result;
-
}
-
-
/**
-
* 更新查询到的所有的文档
-
*
-
* @param filter
-
* 查询条件
-
* @param update
-
* 更新文档
-
* @return 更新结果
-
*/
-
public static UpdateResult updateMany(Bson filter, Bson update) {
-
UpdateResult result = collection.updateMany(filter, update);
-
-
return result;
-
}
-
-
/**
-
* 更新一个文档, 结果是replacement是新文档,老文档完全被替换
-
*
-
* @param filter
-
* 查询条件
-
* @param replacement
-
* 跟新文档
-
*/
-
public static void replace(Bson filter, Document replacement) {
-
collection.replaceOne(filter, replacement);
-
}
-
-
/**
-
* 根据条件删除一个文档
-
*
-
* @param filter
-
* 查询条件
-
*/
-
public static void deleteOne(Bson filter) {
-
collection.deleteOne(filter);
-
}
-
-
/**
-
* 根据条件删除多个文档
-
*
-
* @param filter
-
* 查询条件
-
*/
-
public static void deleteMany(Bson filter) {
-
collection.deleteMany(filter);
-
}
-
}
测试类:
-
package mongo.mongodb;
-
-
import java.util.List;
-
-
import org.bson.Document;
-
import org.junit.Before;
-
import org.junit.Test;
-
-
import com.mongodb.client.result.UpdateResult;
-
-
public class MongoTest
-
{
-
-
public void before(){
-
MongoDb.connect( "test", "darren", "172.16.155.151", 27017);
-
}
-
-
-
public void testInsert(){
-
Document document = new Document();
-
document.append( "name", "wang").append( "gender", "female");
-
MongoDb.insert(document);
-
}
-
-
-
public void testFindAll(){
-
List<Document> results = MongoDb.findAll();
-
for(Document doc : results){
-
System.out.println(doc.toJson());
-
}
-
}
-
-
-
public void testFindBy(){
-
Document filter = new Document();
-
filter.append( "name", "li si");
-
List<Document> results = MongoDb.findBy(filter);
-
for(Document doc : results){
-
System.out.println(doc.toJson());
-
}
-
}
-
-
-
public void testUpdateOne(){
-
Document filter = new Document();
-
filter.append( "gender", "male");
-
-
//注意update文档里要包含"$set"字段
-
Document update = new Document();
-
update.append( "$set", new Document( "gender", "female"));
-
UpdateResult result = MongoDb.updateOne(filter, update);
-
System.out.println( "matched count = " + result.getMatchedCount());
-
}
-
-
-
public void testUpdateMany(){
-
Document filter = new Document();
-
filter.append( "gender", "female");
-
-
//注意update文档里要包含"$set"字段
-
Document update = new Document();
-
update.append( "$set", new Document( "gender", "male"));
-
UpdateResult result = MongoDb.updateMany(filter, update);
-
System.out.println( "matched count = " + result.getMatchedCount());
-
}
-
-
-
public void testReplace(){
-
Document filter = new Document();
-
filter.append( "name", "zhang");
-
-
//注意:更新文档时,不需要使用"$set"
-
Document replacement = new Document();
-
replacement.append( "value", 123);
-
MongoDb.replace(filter, replacement);
-
}
-
-
-
public void testDeleteOne(){
-
Document filter = new Document();
-
filter.append( "name", "li");
-
MongoDb.deleteOne(filter);
-
}
-
-
-
public void testDeleteMany(){
-
Document filter = new Document();
-
filter.append( "gender", "male");
-
MongoDb.deleteMany(filter);
-
}
-
}