简体   繁体   中英

Data retrieval from mongodb java api

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

尝试这个:

BasicDBObject query = new BasicDBObject("user.id",  userId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM