I have two collections
checkone
{"_id":1,"name":"alex"},
{"_id":2,"name":"sandy"}
checktwo
{"_id":11,"password":"alex",tenant_id:{$ref:"checkone","$id":1}}
{"_id":12,"password":"suman",tenant_id:{$ref:"checkone","$id":2}}
when I do dbref fetch, am getting all the fields referencing it.
DBCursor cursor = coll.find();
while(cursor.hasNext()){
DBRef addressObj = (DBRef)cursor.next().get("tenant_id");
System.out.println(addressObj.fetch());
}
Output:
{"_id":1,"name":"alex"},
{"_id":2,"name":"sandy"}
But I need to fetch only NAME field how to restrict it. I have tried it like this.
BasicDBObject query=new BasicDBObject();
Map<String, Object> whereMap = new HashMap<String, Object>();
whereMap.put("tennat_id", 1);
query.putAll(whereMap);
BasicDBObject field=new BasicDBObject("name",1);
DBCursor cursor = coll.find(query,field);
while(cursor.hasNext()){
DBRef addressObj = (DBRef)cursor.next().get("tenant_id");
System.out.println(addressObj.fetch());
}
But I am getting null pointer exception in this line
DBCursor cursor = coll.find(query,field);
How to restrict the fields in mongodb java?