search

Home  >  Q&A  >  body text

mongodb - Spring Data Mongo how to get fields on demand

Entity class A references entity class B and uses DBRef. Now I want to query some fields of A and some fields of B contained in it. I find that there is no way to do it. Does anyone have a method? The code is as follows:

public class A {

@Id
private int id;

@DBRef
private B b;

}

public class B{

@Id
private int id;

private String name;

}

Dao code is as follows,
BasicDBObject queryObject = new BasicDBObject();
queryObject.put("id", id); //A’s id
BasicDBObject fieldsObject=new BasicDBObject();
fieldsObject.put("b", 1); //You can get all the fields of B
//fieldsObject.put("b.name", 1); //Personal test failed, the error was that B's id could not be set to null, and the reason was not found
Query query=new BasicQuery(queryObject,fieldsObject);
mongoOperation.find(query, A.class);

曾经蜡笔没有小新曾经蜡笔没有小新2793 days ago557

reply all(0)I'll reply

No reply
  • Cancelreply