我是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