您好,登錄后才能下訂單哦!
首先添加mongodb的驅動包
并開啟了mongodb的服務
package com.zzq.test;
import java.net.UnknownHostException;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.util.JSON;
public class MongDBTest {
public static void main(String[] args) throws UnknownHostException, MongoException {
Mongo mg = new Mongo("localhost", 27017);
//查詢所有的Database
for (String name : mg.getDatabaseNames()) {
System.out.println("dbName: " + name);
}
DB db = mg.getDB("mircroblog");
//查詢所有的聚集集合
for (String name : db.getCollectionNames()) {
System.out.println("collectionName: " + name);
}
DBCollection users = db.getCollection("citizens");
//查詢所有的數據
DBCursor cur = users.find();
while (cur.hasNext()) {
System.out.println(cur.next());
}
System.out.println(cur.count());
System.out.println(cur.getCursorId());
System.out.println(JSON.serialize(cur));
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。