mongo api,distinct查詢後,迴圈查詢
阿新 • • 發佈:2019-02-06
為了減輕手工查詢,測試對比資料的工作量。寫成程式碼還挺好用的,在這裡記錄下:
import com.mongodb.*; import java.util.List; public class Test3 { public static void main(String[] args){ try { MongoURI uri = new MongoURI("mongodb://IP:埠/test2");//Constants.DIC_DB_ADDR); // Open the db Mongo mongo = new Mongo(uri); DB db = mongo.getDB(uri.getDatabase()); intsum=0; DBCollection coll = db.getCollection("test");//Constants.DIC_TABLE); /* DBObject greatThanEqualAndLessThanEqual=new BasicDBObject(); greatThanEqualAndLessThanEqual.put("$gte", "1446620400000"); greatThanEqualAndLessThanEqual.put("$lt", "1446624000000"); DBObject orGreatThan=new BasicDBObject("err","0");DBObject orGreatThan2=new BasicDBObject("err","-");*/ DBObject ageCompare=new BasicDBObject(); DBObject distinctkey=new BasicDBObject(); /* ageCompare.put("pid","10014326"); ageCompare.put("ac", "time"); ageCompare.put("p1", "2");*/ List<String> lidList = coll.distinct("lid"); for(int x=0;x<lidList.size();x++) { for (long i = 1455520200000l; i < 1455606600000l; i = i + 300000) { ageCompare.put("lid", lidList.get(x)); //ageCompare.put("ty", "1"); ageCompare.put("ts", i); DBCursor cur = coll.find(ageCompare); //System.out.println(cur.count()); if (cur.count() == 2) System.out.println(lidList.get(x)+":"+i); } } //ageCompare.put("$or", new Object[]{orGreatThan,orGreatThan2}); int result; //System.out.println(sum); mongo.close(); } catch (Exception e) { // Die fast throw new RuntimeException(e); } } }