0

I'm searching in the database for the URL but with this code I can not. Why? Normally I want to print all the type and the URL where exist in this database. When I have the print only with the type is working but with the print for URL nothing.

MongoClient mongoClient;
DB db;

mongoClient = new MongoClient("localhost", 27017);
db = mongoClient.getDB("behaviourDB_areas");    


DBCollection cEvent = db.getCollection("event");

    BasicDBObject orderBy = new BasicDBObject();
    orderBy.put("timeStamp",1);


    DBCursor cursorEvents = null;

    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("user_id", "55b20db905f333defea9827f");

    cursorEvents = cEvent.find(searchQuery).sort(orderBy);

        int count=0;

        if(cursorEvents.hasNext()){

            while(cursorEvents.hasNext()){

                count++;           

                System.out.println(cursorEvents.next().get("type").toString());
                System.out.println(cursorEvents.next().get("url").toString());
                System.out.println(count);
            }   
        }

        mongoClient.close();
    }   
}
William
  • 33
  • 1
  • 10
  • 2
    What exactly is not working? Are there any error messages? What are you expecting to happen? What type of collection you use? How does the data looks like exactly? – Simon K. Dec 17 '15 at 10:37
  • Thank you very much for your response.....I am new with all this ...with mongodb...with java....am trying!! – William Dec 17 '15 at 11:58
  • Just basic stackoverflow guidelines ;) – Simon K. Dec 17 '15 at 12:00
  • When i try to run it ..i take this(in the first picture)...and the data it must be like (in the second picture).But my i want to take only the type and when appears url i want it too. The type of collections is what happen when someone navigate on internet....and have to find every type of actions in every page. (picture_1) http://i.stack.imgur.com/KvNjr.jpg (picture_2) http://i.stack.imgur.com/eKybw.jpg – William Dec 17 '15 at 15:51
  • which version of mongo-driver are you using? – Dev Dec 22 '15 at 10:43

1 Answers1

0

cursor.next() should be called only once, calling it second time will return next document. documentation

NullPointerException could be thrown because next document does not exist or get("url") returns null.

Following snippet should solve both issues.

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("behaviourDB_areas");
    MongoCollection cEvent = db.getCollection("event", Document.class);

    MongoCursor<Document> cursorEvents = cEvent
            .find(new BasicDBObject("user_id", "55b20db905f333defea9827f"))
            .sort(new BasicDBObject("timeStamp",1))
            .iterator();

    int count = 0;

    if(cursorEvents.hasNext()) {
        Document doc = cursorEvents.next();
        System.out.println(doc.getString("type"));
        if (doc.containsKey("url")) {
            System.out.println(doc.getString("url"));
        }
        System.out.println(++count);
    }

    cursorEvents.close();
    mongoClient.close();