8

I am a beginner to MongoDB and I'm playing around with it using the JAVA driver.

I have the following code

MongoClient client = new MongoClient();
DB d = client.getDB("world");
DBCollection c = d.getCollection("zips");
DBCursor cursor = c.find();

Now my question is that I want to use a simple cursor to go through the documents. The getDB() method is deprecated but it works fine. In the documentation it's mentioned that getDB can be replaced with MongoClient.getDatabase(); but getDatabase() returns a MongoDatabase not a DB.

Can someone point out the correct way to make a DBCursor without using any deprecated method. Thanks.

PS: I know there are frameworks like morphia, jongo etc but please keep them out of this discussion. I want to currently resort only to the JAVA driver. EDIT: The difference is about getting a cursor in the JAVA driver not between DB and MongoClient

Newton
  • 418
  • 2
  • 9
  • 19
  • See official tutorial: https://docs.mongodb.org/getting-started/java/query/#query-for-all-documents-in-a-collection – Robert May 02 '16 at 11:54

1 Answers1

15

Yes. Thats true. you can replace getDB with getDatabase. this is how you can use it.

        /**** Get database ****/
        // if database doesn't exists, MongoDB will create it for you
        MongoDatabase mydatabase = mongoClient.getDatabase("mydatabase");

        /**** Get collection / table from 'testdb' ****/
        // if collection doesn't exists, MongoDB will create it for you

        FindIterable<Document> mydatabaserecords = mydatabase.getCollection("collectionName").find();
        MongoCursor<Document> iterator = mydatabaserecords.iterator();
        while (iterator.hasNext()) {
            Document doc = iterator.next();
            // do something with document
        }

Example:

So lets say that your document is something like below:

{
  "name": "Newton",
  "age": 25
}

Then fields can be fetched as below

while (iterator.hasNext()) {
    Document doc = iterator.next();
    String name = doc.getString("name");
    int age = doc.getInteger("age");
    System.out.println("Name: " + name);
    System.out.println("Age: " + age);
}

I hope this clears your doubt.

Deendayal Garg
  • 5,030
  • 2
  • 19
  • 33
  • thanks that works. just a quick one. the while loop prints in the format Document{{city=ROCHESTER}} is there a JSON parser which can only print city=ROCHESTER or will i have to make one myself? – Newton May 02 '16 at 16:58
  • edited the answer with an example. Let me know if that make sense. – Deendayal Garg May 02 '16 at 17:30
  • yes that perfectly makes sense. would you be kind enough to tell me if i am right about the following: Using FindIterable and MongoCursor give the same results as using FindIterable and MongoCursor. the only diff is that in the first case if anything other than Document comes into iterator it will give an exception or may need to be cast and in the second case everything might need to be cast? – Newton May 02 '16 at 18:42
  • `MongoCursor` and `FindIterable` are two completely different things. `MongoCursor` is just an extention of `Iterator` class whereas `FindIterable` allows you to build queries. I think this link explains it perfectly. https://groups.google.com/d/msg/mongodb-user/pcVX84PPwM0/FJ1EjPkAAz0J – Deendayal Garg May 02 '16 at 18:51
  • i think you misunderstood. my question meant for example in your sample code if i replace MongoCursor iterator with MongoCursor iterator – Newton May 02 '16 at 18:55
  • 1
    oh Yeah. sorry abt that. you are right. its like raw type vs generics. – Deendayal Garg May 02 '16 at 19:00