发布网友 发布时间:2022-04-24 21:02
共1个回答
热心网友 时间:2022-04-07 21:23
package com.fz.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 SimpleTest { public static void main(String[] args) throws UnknownHostException, MongoException { Mongo mg = new Mongo("192.168.2.77", 27017); // 查询所有的Database for (String name : mg.getDatabaseNames()) { System.out.println("dbName: " + name); } DB db = mg.getDB("test"); // 查询所有的聚集集合 for (String name : db.getCollectionNames()) { System.out.println("collectionName: " + name); } DBCollection users = db.getCollection("users"); // 查询所有的数据 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)); }} 收起