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>

发布了221 篇原创文章 · 获赞 266 · 访问量 236万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览