1. 程式人生 > >java簡單操作mongodb

java簡單操作mongodb

<dependency>

<groupId>org.mongodb</groupId>

<artifactId>mongo-java-driver</artifactId>

<version>3.5.0</version>

</dependency>

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>fastjson</artifactId>

<version>1.2.35</

version>

</dependency>

packagemongodbJava;

importcom.mongodb.MongoClient;

importcom.mongodb.client.FindIterable;

importcom.mongodb.client.MongoCollection;

importcom.mongodb.client.MongoDatabase;

importcom.mongodb.client.result.UpdateResult;

importorg.bson.Document;

importorg.bson.conversions.Bson;

/**

*Helloworld!

*

*/

publicclassApp

{

privatestaticMongoCollection<Document>collection;

public static void getConnection (Stringhost,intport,StringdbName,StringcollectionName) {

MongoClientclient=newMongoClient(host,port);

MongoDatabasedatabase=client.getDatabase(dbName);

collection=database.getCollection(collectionName);

System.out.println(collection);

}

//插入一條

public static void insertOne(Documentdocument){

collection.insertOne(document);

}

//查詢多條

public static FindIterable<Document> findAll(){

FindIterable<Document>documents=collection.find();

returndocuments;

}

//只更新第一條

public static UpdateResult updateOne(Bsonbson,Bsonupdate){

UpdateResultupdateResult=collection.updateOne(bson,update);

returnupdateResult;

}

//只更新第多條

public static UpdateResult updatMany(Bsonbson,Bsonupdate){

UpdateResultupdateResult=collection.updateMany(bson,update);

returnupdateResult;

}

//替換現有文件

public static void replace(Bsonbson,Documentupdate){

collection.replaceOne(bson,update);

}

//只刪除一個

public static void deleteOne(Bsonfilter){

collection.deleteOne(filter);

}

public static voiddeleteMany(Bsonfilter){

collection.deleteMany(filter);

}

}

packagemongodbJava;

importcom.alibaba.fastjson.JSON;

importcom.mongodb.client.FindIterable;

importcom.mongodb.client.result.UpdateResult;

importorg.bson.Document;

importorg.junit.Before;

importorg.junit.Test;

publicclassAppTest{

@Before

public void AppTest(){

App.getConnection("127.0.0.1",27017,"mydb","mybaby");

}

@Test

public void TestInsertOne(){

Documentdoc=newDocument();

doc.put("name","劉亦菲");

doc.put("age",18);

doc.put("sex","女");

App.insertOne(doc);

System.out.println("成功了");

}

@Test

public void TestInsertOne2(){

Documentdoc=newDocument();

doc.append("name","范冰冰").append("age",18).append("sex","女");

App.insertOne(doc);

System.out.println("成功了");

}

@Test

public void TestInsertOne3(){

Mybaby mybaby=new Mybaby("大S","32","女");

String json=JSON.toJSON(mybaby).toString();

Documentdoc=Document.parse(json);

App.insertOne(doc);  //物件轉json插入mongodb

System.out.println("成功了");

}

@Test

public void testFindAll(){

FindIterable<Document>documents=App.findAll();

for(Documentdocument:documents){

System.out.println(document);

}

}

@Test

public void testUpdateOne(){

Documentdoc=newDocument();

doc.append("name","范冰冰");

Documentupdate=newDocument();

update.append("$set",newDocument("age",25));

UpdateResultupdateResult=App.updateOne(doc,update);

System.out.println(updateResult);

//AcknowledgedUpdateResult{matchedCount=1,modifiedCount=0,upsertedId=null}

}

@Test

public void testUpdateMany(){

Documentdoc=newDocument();

doc.append("name","范冰冰");

Documentupdate=newDocument();

update.append("$set",newDocument("sex","女"));

UpdateResultupdateResult=App.updatMany(doc,update);

System.out.println(updateResult);

//AcknowledgedUpdateResult{matchedCount=2,modifiedCount=1,upsertedId=null}

}

}