zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
  • 相关阅读:
    28完全背包+扩展欧几里得(包子凑数)
    HDU 3527 SPY
    POJ 3615 Cow Hurdles
    POJ 3620 Avoid The Lakes
    POJ 3036 Honeycomb Walk
    HDU 2352 Verdis Quo
    HDU 2368 Alfredo's Pizza Restaurant
    HDU 2700 Parity
    HDU 3763 CDs
    POJ 3279 Fliptile
  • 原文地址:https://www.cnblogs.com/zqzdong/p/4839009.html
Copyright © 2011-2022 走看看