Question

Hi I have structure called problem which internally has chapter, chapter has DBref to BookEdition and BookEdition has DBRef to Book

Problem {
  String name,
  @DBRef
  Chapter chapter;
}
Chapter {
  String name,
  @DBRef 
  BookEdition edition;
}
BookEdition {
  String name,
  Book book;

}

if I just fetch the problem that it takes good amount of time to fetch so I just want to include the information I need using fields().include() but I am not able to find fields of DBRefed like chapter, bookedition or book

Query q = new Query().addCriteria(Criteria.where("html").ne("").ne(null)
                    .and("chapter.$id").ne(""));
            q.fields().include("id")
            .include("name")
            .include("html");

Works fine but If I just want chapter id

Query q = new Query().addCriteria(Criteria.where("html").ne("").ne(null)
                    and("chapter.$id").ne(""));
     q.fields().include("id")
     .include("name")
     .include("html")
     .include("chapter._id")
     .include("chapter.bookEdition.isbn13")
     .include("chapter.$bookEdition.$editionNumber")
     .include("chapter.$bookEdition.$book.subjectId")
     .include("chapter.$bookEdition.$book.title");

It doesnt fetches chapter id, bookedition. I hope I am able to explain my question. Is there any ways to specify specific field of DBRef to be included ?

Was it helpful?

Solution

You cannot define field specs for DBRefs as they can only be fetched with all fields at once. It's generally recommended not to use DBRefs (not by us but by the MongoDB guys actually, see the reference documentation) but to take care of the associated objects in you application code. This means you'd rather store an ID and then go ahead and issue a dedicated query to resolve the related objects. This allows you to use field specs again when loading the related objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top