0

I have a collection.

look
 { "_id" : 13.0 , "tenantref" : { "$id" : 3.0}}

I need to retrieve the value of id 3.0 using java. I am getting null pointer exception.

    BasicDBObject field = new BasicDBObject();
    BasicDBObject field = new BasicDBObject();
    field.put("tenantref.$id", 1);

    DBCursor cursor = mongo.getDB("number").getCollection("testthree").find(query,field);   

    while (cursor.hasNext()) 
    {       
    System.out.println(cursor.next().get("tenantref.$id"));
    }

I am getting null pointer exception. How do I need to get the value?

Ramya
  • 1,067
  • 6
  • 16
  • 29

1 Answers1

1
cursor.next().get("tenantref.$id"));

The get() function of BasicDBObject does not support the dot syntax. You have to traverse the object hierarchy by hand. Try

((DBObject)cursor.next().get("tenantref")).get("$id");
Philipp
  • 67,764
  • 9
  • 118
  • 153