使用java查看mongodb的数据库、集合以及数据信息
[mw_shl_code=java,true]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));
}
}
[/mw_shl_code]
[mw_shl_code=java,true]结果: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"} ,[/mw_shl_code]
[mw_shl_code=bash,true] { "_id" : { "$oid" : "54c3bee1426ca6f36a0d051c"} , "username" : "lis"} , [/mw_shl_code]
[mw_shl_code=bash,true]{ "_id" : { "$oid" : "54c3bee6426ca6f36a0d051d"} , "username" : "wangwu"}]
[/mw_shl_code]
|