0

I ve got a field tweets in mongodb database which contains several characteristics of the tweet stored. One of the user field which contain information about the user which post the tweet. I want to retrieve given the id of the user, some other information of the user which is stored in the database. Database's structure:

_id
tweet_info
user
      id
      description
      default_profile

I want, having the user id to retrieve field description and default_profile. What query should I perform here? My code until now:

MongoClient mongoClient = new MongoClient("..", 27017);
    DB db = mongoClient.getDB("...");
    DBCollection coll = db.getCollection("...");


    BasicDBObject query = new BasicDBObject("??,  ??);
    DBCursor  cursor = coll.find(query);
    System.out.println(cursor);
    try {
        while (cursor.hasNext()) {
             final DBObject result = cursor.next();
             Map<String, Object> value = (Map<String, Object>) result.get("user");
            System.out.println(value.get("default_profile"));
            System.out.println(value.get("description"));
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }

How can I retrieve this field given the user.id?? What I am trying is the following:

 BasicDBObject query = new BasicDBObject("user.id",  975789703); // id example
    DBCursor  cursor = coll.find(query);
    System.out.println(cursor);
    try {
        while (cursor.hasNext()) {
             final DBObject result = cursor.next();
             Map<String, Object> value = (Map<String, Object>) result.get("user");
            System.out.println(value.get("default_profile"));
            System.out.println(value.get("default_profile_image"));
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }

When I am trying to perform a query like:

BasicDBObject query = new BasicDBObject("user.name", "");

It returns all the users. After several minutes I am getting the message:

Cursor id=0, ns=iti_se.cms, query={ "user.id" : 507813128}, numIterated=0, limit=20, 
readPreference=primary
Exception in thread "main" com.mongodb.MongoException: interrupted at shutdown
 at com.mongodb.MongoException.parse(MongoException.java:82)
 at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:314)
 at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:295)
 at com.mongodb.DBCursor._check(DBCursor.java:368)
 at com.mongodb.DBCursor._hasNext(DBCursor.java:459)
at com.mongodb.DBCursor.hasNext(DBCursor.java:484)
at twitter4j.examples.tweets.UpdateStatus.default_image(UpdateStatus.java:94)
at twitter4j.examples.tweets.UpdateStatus.main(UpdateStatus.java:112)

Java Result: 1

1 Answer 1

2

Try this:

BasicDBObject query = new BasicDBObject("user.id",  userId);
Sign up to request clarification or add additional context in comments.

11 Comments

The field id is int32. So, userID it must be inside in quotes" " or not?
Actually when I perform the query, it start runs without any effect. It just print the first cursor. It searches 10M tweets. Is it normal?
You get all tweets for current userId.
For the last twenty minutes it runs but i didnt get unitl now any result.
Do you have no result? cursor.size() == 0 ? is userId exist?
|

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.