0
我有兩個集合的MongoDB只讀取特定領域
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}}
當我做DBREF取,我越來越引用它的所有字段。
DBCursor cursor = coll.find();
while(cursor.hasNext()){
DBRef addressObj = (DBRef)cursor.next().get("tenant_id");
System.out.println(addressObj.fetch());
}
輸出:
{"_id":1,"name":"alex"},
{"_id":2,"name":"sandy"}
但我需要獲取唯一的名字領域如何限制它。我嘗試過這樣。
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());
}
但是我得到空指針異常此行
DBCursor cursor = coll.find(query,field);
如何限制MongoDB中的Java領域?