0

I am new to mongodb java api. I am trying to perform queries to my database. I ve read the database found the collections in it and I want to retrieve characteristics of users. My code:

    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);
    }

Now I want to perform queries to retrieve from all ids the usernames for example. How is it possible to do so in java mongodb API?

EDIT: What i ve tried

 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());
    }

However, it doesnot return nothing.

2 Answers 2

1

You asked: "I want to perform queries to retrieve from all ids the usernames for example."

To retrieve all documents from collection you can use find() method of the DBCollection.

DBCursor cursor;
cursor = incollection.find();

After that, you can get the field value by its name for each document as below

while(cursor.hasNext()){
   System.out.println(cursor.next().get("username"));
}

If you need to add more criterias to query documents:

BasicDBObject query1;
DBCursor cursor;
query1 = new BasicDBObject("age", new BasicDBObject("$gt", 25));
cursor = incollection.find(query1);
while(cursor.hasNext()){
     System.out.println(cursor.next().get("username"));
}
Sign up to request clarification or add additional context in comments.

Comments

1

DBCollections objects have method find which takes mainly a DBObject argument. For instance, if you want to find users with age > 50 this can help you.

query = new BasicDBObject("age", new BasicDBObject("$gt", 50));
cursor = incollection.find(query);

while(cursor.hasNext()){
    System.out.println(cursor.next());
}

BasicDBObject take first argument, that is a field, and second argument that is another BasicDBObject or a value. With composition of these objects you can build any query.

I recommend you take a look at MongoDB Documentation, it's pretty good.

Mongo Java Driver http://docs.mongodb.org/ecosystem/drivers/java/

See Javadoc. http://api.mongodb.org/java/current/

2 Comments

Basivally when I tried cursor.hasNext() it tried to change port in host WARNING: Exception executing isMaster command on /........:... java.io.IOException: couldn't connect to ...........:...
Mongo class soon will be deprecated. You should use just MongoClient and from it, getDB, getCollection and work with your collection. You are mixing Mongo and MongoClient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.