mongodb - Spring Data Mongo how to get fields on demand
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-05-02 09:22:46
0
0
542

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);

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(0)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template