JAVA-查看MongoDB中的数据(包含Map集合)

package com.xinsight.server;

import java.net.UnknownHostException;
import java.util.HashMap;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
/**
 *查找Mongodb中某一列包含Map集合的所有数据 
 */
public class FindMongodbMap {
    private static Mongo mg ;
    private static DB db;
    private static int num = 0;
    public static void main(String args[]){
        try {
            mg = new Mongo("localhost",27017);
            db = mg.getDB("admin");
            boolean auth = db.authenticate("aa","aa".toCharArray() );
            if(!auth){
                System.err.println("链接Mongodb失败");
            }else{
                db = mg.getDB("zq1");
                DBCollection collection = db.getCollection("zq1");
                DBCursor u = collection.find();
                while(u.hasNext()){
                    num++;
                    BasicDBObject basi = (BasicDBObject)u.next();
                    HashMap map = (HashMap)basi.get("m");
                    if(num % 100 == 0){
                        System.err.println("--------------------------------------------------------------------");
                        Thread.sleep(3000);
                    }
                    System.out.println(basi.get("v1") + "    " + basi.get("v2") + "    " +basi.get("v3") + "    " +basi.get("v4") + "    " + map.get("time") + "    " + map.get("sex") + "    " + map.get("age") + "    " + map.get("name"));
                }
                System.out.println("共找到:" + num + "条数据");
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

 

posted @ 2015-09-25 18:08  zqzdong  阅读(3454)  评论(0编辑  收藏  举报