RavedDB “outerjoin”











up vote
1
down vote

favorite












I have 2 collections of documents in my ravenDB, location and address like this:



public class Location()
{
public string Id{get;set;}
public string Name{get;set;}
public List<Address> Addresses{get;set;}

}

public class Address()
{
public string Id{get;set;}
public string StreetName {get;set;}
public string PostalCode {get;set;}
}


So a Location can have zero og many addresses. And I can have many addresses that are not assigned to a Location.



If I have data like this:



//2 Location document
{
Id:"Location/1",
Name: "My Location 1"
Addresses: [{id:"Address/1"},
{id:"Address/2"}
]
}

{
Id:"Location/2",
Name: "My Location 2"
Addresses: [
{id:"Address/2"}
]
}


//3 Address document
{
Id: "Address/1",
StreetName: "Street1 10",
PostalCode: "1000"
}

{
Id: "Address/2",
StreetName: "Street1 11",
PostalCode: "1000"
}

{
Id: "Address/3",
StreetName: "Street1 12",
PostalCode: "2000"
}


I'm struggleing to find the best way to create an index of this which will give me a resultset like this:



StreetAddress PostalCode    Location
Street1 10 1000 My Location 1
Street2 11 1000 My Location 2
Street2 11 1000 My Location 1
Street3 12 2000


I would appreciate any input on this:-)










share|improve this question


























    up vote
    1
    down vote

    favorite












    I have 2 collections of documents in my ravenDB, location and address like this:



    public class Location()
    {
    public string Id{get;set;}
    public string Name{get;set;}
    public List<Address> Addresses{get;set;}

    }

    public class Address()
    {
    public string Id{get;set;}
    public string StreetName {get;set;}
    public string PostalCode {get;set;}
    }


    So a Location can have zero og many addresses. And I can have many addresses that are not assigned to a Location.



    If I have data like this:



    //2 Location document
    {
    Id:"Location/1",
    Name: "My Location 1"
    Addresses: [{id:"Address/1"},
    {id:"Address/2"}
    ]
    }

    {
    Id:"Location/2",
    Name: "My Location 2"
    Addresses: [
    {id:"Address/2"}
    ]
    }


    //3 Address document
    {
    Id: "Address/1",
    StreetName: "Street1 10",
    PostalCode: "1000"
    }

    {
    Id: "Address/2",
    StreetName: "Street1 11",
    PostalCode: "1000"
    }

    {
    Id: "Address/3",
    StreetName: "Street1 12",
    PostalCode: "2000"
    }


    I'm struggleing to find the best way to create an index of this which will give me a resultset like this:



    StreetAddress PostalCode    Location
    Street1 10 1000 My Location 1
    Street2 11 1000 My Location 2
    Street2 11 1000 My Location 1
    Street3 12 2000


    I would appreciate any input on this:-)










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have 2 collections of documents in my ravenDB, location and address like this:



      public class Location()
      {
      public string Id{get;set;}
      public string Name{get;set;}
      public List<Address> Addresses{get;set;}

      }

      public class Address()
      {
      public string Id{get;set;}
      public string StreetName {get;set;}
      public string PostalCode {get;set;}
      }


      So a Location can have zero og many addresses. And I can have many addresses that are not assigned to a Location.



      If I have data like this:



      //2 Location document
      {
      Id:"Location/1",
      Name: "My Location 1"
      Addresses: [{id:"Address/1"},
      {id:"Address/2"}
      ]
      }

      {
      Id:"Location/2",
      Name: "My Location 2"
      Addresses: [
      {id:"Address/2"}
      ]
      }


      //3 Address document
      {
      Id: "Address/1",
      StreetName: "Street1 10",
      PostalCode: "1000"
      }

      {
      Id: "Address/2",
      StreetName: "Street1 11",
      PostalCode: "1000"
      }

      {
      Id: "Address/3",
      StreetName: "Street1 12",
      PostalCode: "2000"
      }


      I'm struggleing to find the best way to create an index of this which will give me a resultset like this:



      StreetAddress PostalCode    Location
      Street1 10 1000 My Location 1
      Street2 11 1000 My Location 2
      Street2 11 1000 My Location 1
      Street3 12 2000


      I would appreciate any input on this:-)










      share|improve this question













      I have 2 collections of documents in my ravenDB, location and address like this:



      public class Location()
      {
      public string Id{get;set;}
      public string Name{get;set;}
      public List<Address> Addresses{get;set;}

      }

      public class Address()
      {
      public string Id{get;set;}
      public string StreetName {get;set;}
      public string PostalCode {get;set;}
      }


      So a Location can have zero og many addresses. And I can have many addresses that are not assigned to a Location.



      If I have data like this:



      //2 Location document
      {
      Id:"Location/1",
      Name: "My Location 1"
      Addresses: [{id:"Address/1"},
      {id:"Address/2"}
      ]
      }

      {
      Id:"Location/2",
      Name: "My Location 2"
      Addresses: [
      {id:"Address/2"}
      ]
      }


      //3 Address document
      {
      Id: "Address/1",
      StreetName: "Street1 10",
      PostalCode: "1000"
      }

      {
      Id: "Address/2",
      StreetName: "Street1 11",
      PostalCode: "1000"
      }

      {
      Id: "Address/3",
      StreetName: "Street1 12",
      PostalCode: "2000"
      }


      I'm struggleing to find the best way to create an index of this which will give me a resultset like this:



      StreetAddress PostalCode    Location
      Street1 10 1000 My Location 1
      Street2 11 1000 My Location 2
      Street2 11 1000 My Location 1
      Street3 12 2000


      I would appreciate any input on this:-)







      ravendb






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 7:41









      Foysti

      61




      61
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          One option is to model the other way around, i.e.



          public class Location()
          {
          public string Id {get;set;}
          public string Name {get;set;}

          }

          public class Address()
          {
          public string Id {get;set;}
          public string StreetName {get;set;}
          public string PostalCode {get;set;}
          // Locations is a list with the Location documents IDs
          public List<string> Locations {get;set;}

          }


          And then the index can be:



          from address in docs.Addresses
          select new {
          StreetAddress = address.StreetName,
          PostalCode = address.PostalCode
          }


          And then use the following Query:



          from index 'AddressesIndex' as x
          load x.Locations as locations
          select {
          StreetName: x.StreetName,
          PostalCode: x.PostalCode,
          Locations: locations.map(x => x.Name)
          }


          ===========================================================================



          A second option is to use the same models as you have in the question description
          and define a
          multi-map-reduce index.



          The multi-map-reduce index will be:



          The first map:



          from locationDoc in docs.Locations
          from addressId in locationDoc.Addresses
          let address = LoadDocument(addressId, "Addresses")
          select new {
          StreetName = address.StreetName,
          PostalCode = (string)null,
          LocationNames = new {locationDoc.Name},
          }


          The second map:



          from address in docs.Addresses
          select new{
          StreetName = address.StreetName,
          PostalCode = address.PostalCode,
          LocationNames = (string)null
          }


          The reduce:



          from result in results
          group result by new { result.StreetName } into g
          select new {
          StreetName = g.Key.StreetName,
          PostalCode = g.Select(x => x.PostalCode).FirstOrDefault(x => x != null),
          LocationNames = g.SelectMany(x => x.LocationNames)
          }





          share|improve this answer





















          • Thank you:-) I ended up with a multi-map-reduce index
            – Foysti
            Nov 27 at 23:09











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426030%2fraveddb-outerjoin%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          One option is to model the other way around, i.e.



          public class Location()
          {
          public string Id {get;set;}
          public string Name {get;set;}

          }

          public class Address()
          {
          public string Id {get;set;}
          public string StreetName {get;set;}
          public string PostalCode {get;set;}
          // Locations is a list with the Location documents IDs
          public List<string> Locations {get;set;}

          }


          And then the index can be:



          from address in docs.Addresses
          select new {
          StreetAddress = address.StreetName,
          PostalCode = address.PostalCode
          }


          And then use the following Query:



          from index 'AddressesIndex' as x
          load x.Locations as locations
          select {
          StreetName: x.StreetName,
          PostalCode: x.PostalCode,
          Locations: locations.map(x => x.Name)
          }


          ===========================================================================



          A second option is to use the same models as you have in the question description
          and define a
          multi-map-reduce index.



          The multi-map-reduce index will be:



          The first map:



          from locationDoc in docs.Locations
          from addressId in locationDoc.Addresses
          let address = LoadDocument(addressId, "Addresses")
          select new {
          StreetName = address.StreetName,
          PostalCode = (string)null,
          LocationNames = new {locationDoc.Name},
          }


          The second map:



          from address in docs.Addresses
          select new{
          StreetName = address.StreetName,
          PostalCode = address.PostalCode,
          LocationNames = (string)null
          }


          The reduce:



          from result in results
          group result by new { result.StreetName } into g
          select new {
          StreetName = g.Key.StreetName,
          PostalCode = g.Select(x => x.PostalCode).FirstOrDefault(x => x != null),
          LocationNames = g.SelectMany(x => x.LocationNames)
          }





          share|improve this answer





















          • Thank you:-) I ended up with a multi-map-reduce index
            – Foysti
            Nov 27 at 23:09















          up vote
          1
          down vote













          One option is to model the other way around, i.e.



          public class Location()
          {
          public string Id {get;set;}
          public string Name {get;set;}

          }

          public class Address()
          {
          public string Id {get;set;}
          public string StreetName {get;set;}
          public string PostalCode {get;set;}
          // Locations is a list with the Location documents IDs
          public List<string> Locations {get;set;}

          }


          And then the index can be:



          from address in docs.Addresses
          select new {
          StreetAddress = address.StreetName,
          PostalCode = address.PostalCode
          }


          And then use the following Query:



          from index 'AddressesIndex' as x
          load x.Locations as locations
          select {
          StreetName: x.StreetName,
          PostalCode: x.PostalCode,
          Locations: locations.map(x => x.Name)
          }


          ===========================================================================



          A second option is to use the same models as you have in the question description
          and define a
          multi-map-reduce index.



          The multi-map-reduce index will be:



          The first map:



          from locationDoc in docs.Locations
          from addressId in locationDoc.Addresses
          let address = LoadDocument(addressId, "Addresses")
          select new {
          StreetName = address.StreetName,
          PostalCode = (string)null,
          LocationNames = new {locationDoc.Name},
          }


          The second map:



          from address in docs.Addresses
          select new{
          StreetName = address.StreetName,
          PostalCode = address.PostalCode,
          LocationNames = (string)null
          }


          The reduce:



          from result in results
          group result by new { result.StreetName } into g
          select new {
          StreetName = g.Key.StreetName,
          PostalCode = g.Select(x => x.PostalCode).FirstOrDefault(x => x != null),
          LocationNames = g.SelectMany(x => x.LocationNames)
          }





          share|improve this answer





















          • Thank you:-) I ended up with a multi-map-reduce index
            – Foysti
            Nov 27 at 23:09













          up vote
          1
          down vote










          up vote
          1
          down vote









          One option is to model the other way around, i.e.



          public class Location()
          {
          public string Id {get;set;}
          public string Name {get;set;}

          }

          public class Address()
          {
          public string Id {get;set;}
          public string StreetName {get;set;}
          public string PostalCode {get;set;}
          // Locations is a list with the Location documents IDs
          public List<string> Locations {get;set;}

          }


          And then the index can be:



          from address in docs.Addresses
          select new {
          StreetAddress = address.StreetName,
          PostalCode = address.PostalCode
          }


          And then use the following Query:



          from index 'AddressesIndex' as x
          load x.Locations as locations
          select {
          StreetName: x.StreetName,
          PostalCode: x.PostalCode,
          Locations: locations.map(x => x.Name)
          }


          ===========================================================================



          A second option is to use the same models as you have in the question description
          and define a
          multi-map-reduce index.



          The multi-map-reduce index will be:



          The first map:



          from locationDoc in docs.Locations
          from addressId in locationDoc.Addresses
          let address = LoadDocument(addressId, "Addresses")
          select new {
          StreetName = address.StreetName,
          PostalCode = (string)null,
          LocationNames = new {locationDoc.Name},
          }


          The second map:



          from address in docs.Addresses
          select new{
          StreetName = address.StreetName,
          PostalCode = address.PostalCode,
          LocationNames = (string)null
          }


          The reduce:



          from result in results
          group result by new { result.StreetName } into g
          select new {
          StreetName = g.Key.StreetName,
          PostalCode = g.Select(x => x.PostalCode).FirstOrDefault(x => x != null),
          LocationNames = g.SelectMany(x => x.LocationNames)
          }





          share|improve this answer












          One option is to model the other way around, i.e.



          public class Location()
          {
          public string Id {get;set;}
          public string Name {get;set;}

          }

          public class Address()
          {
          public string Id {get;set;}
          public string StreetName {get;set;}
          public string PostalCode {get;set;}
          // Locations is a list with the Location documents IDs
          public List<string> Locations {get;set;}

          }


          And then the index can be:



          from address in docs.Addresses
          select new {
          StreetAddress = address.StreetName,
          PostalCode = address.PostalCode
          }


          And then use the following Query:



          from index 'AddressesIndex' as x
          load x.Locations as locations
          select {
          StreetName: x.StreetName,
          PostalCode: x.PostalCode,
          Locations: locations.map(x => x.Name)
          }


          ===========================================================================



          A second option is to use the same models as you have in the question description
          and define a
          multi-map-reduce index.



          The multi-map-reduce index will be:



          The first map:



          from locationDoc in docs.Locations
          from addressId in locationDoc.Addresses
          let address = LoadDocument(addressId, "Addresses")
          select new {
          StreetName = address.StreetName,
          PostalCode = (string)null,
          LocationNames = new {locationDoc.Name},
          }


          The second map:



          from address in docs.Addresses
          select new{
          StreetName = address.StreetName,
          PostalCode = address.PostalCode,
          LocationNames = (string)null
          }


          The reduce:



          from result in results
          group result by new { result.StreetName } into g
          select new {
          StreetName = g.Key.StreetName,
          PostalCode = g.Select(x => x.PostalCode).FirstOrDefault(x => x != null),
          LocationNames = g.SelectMany(x => x.LocationNames)
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 at 17:24









          Danielle

          1501312




          1501312












          • Thank you:-) I ended up with a multi-map-reduce index
            – Foysti
            Nov 27 at 23:09


















          • Thank you:-) I ended up with a multi-map-reduce index
            – Foysti
            Nov 27 at 23:09
















          Thank you:-) I ended up with a multi-map-reduce index
          – Foysti
          Nov 27 at 23:09




          Thank you:-) I ended up with a multi-map-reduce index
          – Foysti
          Nov 27 at 23:09


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426030%2fraveddb-outerjoin%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Berounka

          Sphinx de Gizeh

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...