码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2015-09-25 20:13:44      阅读:461      评论:0      收藏:0      [点我收藏+]

标签:

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();
        }
    }
}

 

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

标签:

原文地址:http://www.cnblogs.com/zqzdong/p/4839009.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!