Paul D'Ambra
Paul D'Ambra

Reputation: 7814

How do I index for a nested date in RavenDB

I may be barking up entirely the wrong tree here but... I have a RavenDB with lots of objects of type RavenPerson stored.

Each RavenPerson has a list of mailing bodies each of which has a date associated.

{
  "Bodies": [
    {
      "RenewalRunDate": "2011-11-30T00:00:00.0000000+00:00",
      "BodyText": "A bunch of text"
    }
  ]
}

I want to take a given date and get a list of the person's id and the related body text.

So...

    public class MailingsByDate : AbstractIndexCreationTask<RavenPerson, PersonMailingModel>
    {
        public MailingsByDate()
        {
            Map = people =>
                  people.SelectMany(person => person.Bodies,
                                    (person, body) =>
                                    new PersonMailingModel
                                        {
                                            MembershipNumber = person.MembershipNumber,
                                            MailingBody = body.BodyText,
                                            MailingDate = body.RenewalRunDate
                                        });
        }
    }

but when I call this

        var mailings = DocumentSession.Query<PersonMailingModel, MvcApplication.MailingsByDate>()
                                      .Where(pmm => pmm.MailingDate == date.Value);

I get an exception

Unable to cast object of type 'IntegraRenewalMailLibrary.RavenPerson' to type 'RenewalLogs.Models.PersonMailingModel'.

Thanks in advance!


Update: Altering the call to the index as below doesn't resolve the exception

        var mailings = DocumentSession.Query<PersonMailingModel, MvcApplication.MailingsByDate>()
                                      .Where(pmm => pmm.MailingDate == date.Value)
                                      .As<PersonMailingModel>()
                                      .ToList();

Update to Update: I've created a visual studio solution with example test cases here: https://github.com/pauldambra/IndexExploration

using version 1.0.888

I definitely haven't wrapped my head around what is happening in the indexes so I'm guessing this is me either not doing it right or trying to do the wrong thing...


Ayende pointed me to the solution on twitter which was to take the SelectMany call out the index and replace it with a call using the linq query form

    public class RecipientsByDate : AbstractIndexCreationTask<Person, PersonMailing>
    {
        public RecipientsByDate()
        {
            Map = people => from person in people
                            from body in person.Bodies
                            select new PersonMailing
                                       {
                                           MembershipNumber = person.MembershipNumber,
                                           MailingBody = body.BodyText,
                                           MailingDate = body.MailingDate
                                       };

        }
    }

Upvotes: 3

Views: 234

Answers (1)

Ayende Rahien
Ayende Rahien

Reputation: 22956

Call As() in the end of this call

Upvotes: 1

Related Questions