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(); } } }
时间: 2024-10-11 13:29:32