更新时间:2022-12-05 12:27:42 来源:极悦 浏览875次
下面极悦小编来为大家举个Java连接mongodb的例子。
import java.net.UnknownHostException;
import java.util.List;
import java.util.Set;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.DBTCPConnector;
import com.mongodb.Mongo;
import com.mongodb.WriteResult;
/**
* 对于mongon的测试
*
* @author Gaojie
*/
public class MongoDBTest {
Mongo m;
DBTCPConnector conn;
private DB db;
@Test
public void testMongo() throws UnknownHostException {
String host = "202.108.*.*";
m = new Mongo(host,27017);
// m.get
}
@Test
public void testConntect() throws Exception {
testMongo();
System.out.println("Mongo: " + m);
conn = m.getConnector();
System.out.println("DBTCPConnector: " + conn.getServerAddressList());
}
@Test
public void testDB() throws Exception {
testMongo();
testConntect();
// 获取admin的数据库
db = m.getDB("gaojie");
System.out.println("DB=" + db);
boolean auth = db.authenticate("root","123456".tocharArray());
System.out.println("auth=" + auth);
}
@Test
public void testGetAll() throws Exception {
testDB();
// 获取db里面的collection(表)
Set names = db.getCollectionNames();
for (String name : names) {
System.out.println("CollectionName: " + name);
DBCollection coll = db.getCollection(name);
System.out.println("CollectionCount=" + coll.count());
DBCursor cursor = coll.find();
while (cursor.hasNext()) {
System.out.println("DBObject=" + cursor.next());
}
List objs = coll.getIndexInfo();
for (DBObject obj : objs) {
System.out.println("IndexInfo=" + obj);
}
System.out.println("==============");
}
// 获取表结果
}
@Test
public void testCollection() throws Exception {
testDB();
if (db.isAuthenticated()) {
for (String coll : db.getCollectionNames()) {
System.out.println("collection=" + coll);
}
}
}
@Test
public void testInsert() throws Exception {
testDB();
if (db.isAuthenticated()) {
DBCollection coll = db.getCollection("test");
DBObject obj = new BasicDBObject();
obj.put("name","gaojie");
obj.put("age",2403);
obj.put("md5","546466sfsddfsd");
WriteResult wr = coll.insert(obj);
System.out.println("WriteResult=" + wr);
}
}
@Test
public void testInsertObj() throws Exception {
testDB();
if (db.isAuthenticated()) {
DBCollection coll = db.getCollection("test");
DBObject obj = new BasicDBObject();
obj.put("pepole",new User());
WriteResult wr = coll.insert(obj);
System.out.println("WriteResult=" + wr);
}
}
@Test
public void testRemove() throws Exception {
testDB();
if (db.isAuthenticated()) {
DBCollection coll = db.getCollection("test");
DBObject obj = new BasicDBObject();
// obj.put("name","gaojie1");
obj.put("age",110);
System.out.println("WriteResult=" + coll.remove(obj));
}
}
@Test
public void testUpdate() throws Exception {
testDB();
if (db.isAuthenticated()) {
DBCollection coll = db.getCollection("test");
DBObject obj = new BasicDBObject();
obj.put("name","gaojie10");
obj.put("age",110);
DBObject upObj = new BasicDBObject();
upObj.put("name","gaojie10");
// upObj.put("age",110);
System.out.println("WriteResult=" + coll.update(obj,upObj));
}
}
@Test
public void testQuery() throws Exception {
testDB();
if (db.isAuthenticated()) {
DBCollection coll = db.getCollection("test");
DBObject obj = new BasicDBObject();
// obj.put("name","gaojie00");
obj.put("age",110);
DBCursor cursor = coll.find(obj);
while (cursor.hasNext()) {
System.out.println("DBObject=" + cursor.next());
}
}
}
}
0基础 0学费 15天面授
Java就业班有基础 直达就业
业余时间 高薪转行
Java在职加薪班工作1~3年,加薪神器
工作3~5年,晋升架构
提交申请后,顾问老师会电话与您沟通安排学习