1

Let say we have following model,

Base class

class Meeting
{
  String title;
}

First derived class

class OfficialMeeting extends Meeting
{
 int numberOfParticipants;
 String meetingRoomName;
}

Second derived class

class Party extends Meeting
{
 String theme;
}

Composition

class Schedule
{
 List<Meeting> meetings;
}

When I try to search the collection 'schedules' having theme as "Beach Party", Spring data mongo errors out saying Meeting does not have a field called theme.

Please suggest a solution here in terms of model design or search to mongo collection.

varsh
  • 152
  • 10

1 Answers1

0

Yes, of course it should say that. Your Meeting class is the parent class and no way to detect it's a Party or Official Meeting in Run time. Spring Mongo has no "instance of" operator. For your case, if your collection contains both Party document format and OfficialMeeting document format, you should check "theme" value whether exists or not (or check null, depends on your data) to speciy which type it belongs to and from which you can query on the proper type. But anyway, in my experience, I would recommend you don't use many format in your document if not needed, although it's NoSQL. Some properties you can add or remove anytime like "metadata", or array items, but some properties that could determine the type of document should not.

user123
  • 577
  • 5
  • 23