1

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 ?

plzdontkillme
  • 1,497
  • 3
  • 20
  • 38
  • FYI: A DBRef is just like a "type-less" foreign key -- and MongoDB can't do a join. So, any references are fetched independently of the first query as one or more additional queries? I'm not familiar with MongoTemplate, but given the syntax you're using, it seems unlikely that it would be able to make the additional queries necessary and then retrieve just a subset of properties. – WiredPrairie Nov 09 '13 at 02:50
  • I improved formatting but there some misplaced semicolons there. Could you fix this by yourself. – zero323 Nov 09 '13 at 09:06

1 Answers1

3

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.

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211