package com.mongodb;

import java.net.UnknownHostException;

import com.mongodb.util.JSON;

public class Test1 {

public static void main(String[] args) throws UnknownHostException {

Mongo mongo = new Mongo("chaoren1", 27017);
//查询所有的databases
for (String db : mongo.getDatabaseNames()) {
System.out.println(db);
}
DB db = mongo.getDB("tutorise");
//查询所有的聚集集合
for (String name : db.getCollectionNames()) {
System.out.println(name);
}
DBCollection users = db.getCollection("user");
//查询所有数据
DBCursor cursor = users.find();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
System.out.println(cursor.count());
System.out.println(cursor.getCursorId());
System.out.println(JSON.serialize(cursor));
}



}
</pre><p>结果:</p><p>  </p><pre code_snippet_id="588344" snippet_file_name="blog_20150124_3_7754259" name="code" class="plain">local
tutorise
numbers
system.indexes
user
{ "_id" : { "$oid" : "54c3bedc426ca6f36a0d051b"} , "username" : "zhangsan"}
{ "_id" : { "$oid" : "54c3bee1426ca6f36a0d051c"} , "username" : "lis"}
{ "_id" : { "$oid" : "54c3bee6426ca6f36a0d051d"} , "username" : "wangwu"}
3
0
[ { "_id" : { "$oid" : "54c3bedc426ca6f36a0d051b"} , "username" : "zhangsan"} ,
{ "_id" : { "$oid" : "54c3bee1426ca6f36a0d051c"} , "username" : "lis"} ,
{ "_id" : { "$oid" : "54c3bee6426ca6f36a0d051d"} , "username" : "wangwu"}]