1. 程式人生 > >java如何獲取mongodb的連線並執行簡單的操作?

java如何獲取mongodb的連線並執行簡單的操作?

package mongodb;

/** 
* @author : suyuyuan
* @date :2016年6月23日 下午3:10:03 
* @version 1.0 
*/


import java.net.UnknownHostException;
import com.mongodb.DBCollection;


public class MongoTest {


public static void main(String[] args) {
DBCollection collection;
try {
// 獲取連線
collection = MongoUtil.getCollection();
// 插入資料
MongoUtil.insertDate(collection);
// 查詢資料
MongoUtil.queryDate(collection);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}


}

package mongodb;


import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;


import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;


/**
 * @author : suyuyuan
 * @date :2016年6月24日 上午10:36:28
 * @version 1.0
 */
public class MongoUtil {


private static String dbIp = "localhost";
private static int dbPort = 27017;
private static String mydb= "mongotest";
private static String table = "myData";




public static DBCollection getCollection() throws UnknownHostException {
// 例項化Mongo物件,
Mongo mongo = new Mongo(dbIp, dbPort);
// 連線名為yourdb的資料庫,假如資料庫不存在的話,mongodb會自動建立
DB db = mongo.getDB(mydb);
// 從Mongodb中獲得名為myData的資料集合,如果該資料集合不存在,Mongodb會為其新建立
DBCollection collection = db.getCollection(table);

//以上部分可以用如下替換:

// MongoClient mongoClient = new MongoClient(new ServerAddress(dbIp, dbPort));
// DB db = mongoClient.getDB(mydb);
// DBCollection collection = db.getCollection(table); 


return collection;
}


public static void insertDate(DBCollection collection) {
// 建立要儲存的document
BasicDBObject document = new BasicDBObject();
List<DBObject> documents = new ArrayList<DBObject>();
document.put("id", 1113);
document.put("msg", "mongodb test1 花生醬");
document.put("msg2", "mongodb test1 花生醬");
document.put("msg3", "mongodb test1 花生醬");
document.put("msg4", "mongodb test2 暗世界");
document.put("msg5", "mongodb test2 暗世界");
// 將新建立的document儲存到collection中去
collection.insert(document); // 單條插入
BasicDBObject document2 = new BasicDBObject();
document2.put("id", 1114);
document2.put("msg", "1");
document2.put("msg2", "2");
document2.put("msg3", "3");
document2.put("msg4", "4");
document2.put("msg5", "5");
BasicDBObject document3 = new BasicDBObject();
document3.put("id", 1115);
document3.put("msg", "6");
document3.put("msg2", "7");
document3.put("msg3", "8");
document3.put("msg4", "9");
document3.put("msg5", "10");

documents.add(document2);
documents.add(document3);
collection.insert(documents); // 多條插入
}


public static void queryDate(DBCollection collection) {
// 建立要查詢的document
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("id", 1113);
// 使用collection的find方法查詢document
DBCursor cursor = collection.find(searchQuery);
// 迴圈輸出結果
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
searchQuery.put("id", 1114);
cursor = collection.find(searchQuery);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
searchQuery.put("id", 1115);
cursor = collection.find(searchQuery);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}


}

注意:需要下載相關的jar包:

pom檔案配置如下:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test-group</groupId>
  <artifactId>test-artifact</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>test-artifact Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
<groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>

<!-- <dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
</dependency> -->
<dependency>
   <groupId>javax.mail</groupId>
   <artifactId>javax.mail-api</artifactId>
   <version>1.5.5</version>
</dependency>

<dependency>
    <groupId>javax.activation</groupId>
   <artifactId>activation</artifactId>
   <version>1.1.1</version>
</dependency>

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>

<dependency>
<groupId>org.mongodb.morphia</groupId>
        <artifactId>morphia</artifactId>
        <version>1.2.0</version>
</dependency>

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.2.2</version>
</dependency>


  </dependencies>
  <build>
    <finalName>test-artifact</finalName>
  </build>
</project>