我是mongodb java api的新手。我正在嘗試對我的數據庫執行查詢。我讀過數據庫找到它的集合,我想檢索用戶的特徵。我的代碼:Java中的查詢mongodb API
ServerAddress serverAdr;
serverAdr = new ServerAddress(".. .. .., ...);
Twitter twitter = null;
MongoOptions options = new MongoOptions();
options.connectionsPerHost = 10;
MongoClient mongoClient = new MongoClient(.. ... ...", ...);
DB db = mongoClient.getDB("trendSetters");
System.out.println("Connect to database successfully");
//JSONObject content = getJSONFromFile("user.json");
Mongo mongo = null;
Set<String> colls = db.getCollectionNames();
mongo = new Mongo(serverAdr, options);
mongo.setWriteConcern(WriteConcern.SAFE);
DB db_se = mongo.getDB("iti_se");
DBCollection incollection = db_se.getCollection("cms_users_unique");
DBCollection outcollection = db_se.getCollection("cms_users_features");
for (String s : colls) {
System.out.println(s);
}
現在我想要執行查詢來從所有ID檢索用戶名例如。在java mongodb API中如何做到這一點?
編輯:我已經嘗試過什麼
BasicDBObject query = new BasicDBObject();
DBCursor cursor;
query = new BasicDBObject("followers", new BasicDBObject("$gt", 1));
cursor = incollection.find(query);
while(cursor.hasNext()){
System.out.println(cursor.next());
}
然而,亙古不返回任何結果。
Basivally時我試圖cursor.hasNext()它試圖改變主機端口警告:異常執行isMaster命令上/ ........:... java.io.IOException:無法連接.... .......:... –
Mongo類很快將被棄用。您應該只使用MongoClient,並使用getDB,getCollection並使用您的集合。你在混合Mongo和MongoClient。 – gasparms