Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
RavenDB Transformations and Joining Simple Data
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  17 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
jamesamuir  
View profile  
 More options May 2 2012, 5:43 pm
From: jamesamuir <jamesam...@gmail.com>
Date: Wed, 2 May 2012 14:43:25 -0700 (PDT)
Local: Wed, May 2 2012 5:43 pm
Subject: RavenDB Transformations and Joining Simple Data
I was wondering if someone could help me understand RavenDB
transformations as I cant seem to get them working correctly. I tried
following Ayende's post

http://ayende.com/blog/4661/ravendb-live-projections-or-how-to-do-joi...

 but I am not able to apply it to my situation. I have two models, a
UserModel and an UserAddressModel. The code is as follows

public class UserModel
    {
        #region Properties

        public string Id { get; set; }

        public string AccountId { get; set; }

        public string UserName { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Email { get; set; }

        public DateTime InsertDate { get; set; }

        #endregion
    }
and

public class UserAddressModel
    {

        public string Id { get; set; }

        public string AccountId { get; set; }

        public string Address1 { get; set; }

        public string Address2 { get; set; }

        public string City { get; set; }

        public string State { get; set; }

        public string Zipcode { get; set; }

        public float Latitude { get; set; }

        public float Longitude { get; set; }

        public DateTime InsertDate { get; set; }

    }
I have my RavenDB Index set up like such.

public class UserDashboard_ByName :
AbstractIndexCreationTask<UserModel>
    {
        public UserDashboard_ByName()
        {
            Map = users => from user in users
                                   select new { user.UserName,
user.AccountId };

            TransformResults =
                (database, users) => from user in users
                                             let useraddress =
database.Load<UserAddressModel>(user.AccountId)
                                             select new
                                                        { FirstName =
user.FirstName,
                                                          LastName =
user.LastName,
                                                          Address1 =
useraddress.Address1,
                                                          Address2 =
useraddress.Address2

                                                        };
        }
    }
and I am calling it with the following code

using (var session = DataDocumentStore.Instance.OpenSession())
            {

                //Get the AccountId
                var userDashboard =
                (from user in session.Query<UserModel,
UserDashboard_ByName>()
                 where user.UserName == userName
                 select user).SingleOrDefault();

            }
When I call the index, it returns a UserModel type, not the anonymous
type that I am expecting. Also, I do not understand how I can call

let useraddress = database.Load<UserAddressModel>(user.AccountId)
when there is no specific relationship that has been specified in code
or anywhere else.

When I view the index in the RavenDB studio, I am getting null for all
of the address fields as well.

Maybe someone can explain to me how I should be joining data in
RavenDB? Some expanded documentation or a nudge in the right direction
to understanding this better would be greatly appreciated. Thanks in
advance.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Itamar Syn-Hershko  
View profile  
 More options May 2 2012, 6:05 pm
From: Itamar Syn-Hershko <ita...@hibernatingrhinos.com>
Date: Thu, 3 May 2012 01:05:56 +0300
Local: Wed, May 2 2012 6:05 pm
Subject: Re: [RavenDB] RavenDB Transformations and Joining Simple Data

You are trying to do a relational operation with RavenDB. That won't work.

You want to have all the user data in a User class, and if for some of your
screens you only need his name and address, you can easily use a
TransformResults function to filter it out - but for most use cases you
don't even have to do that


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Warren  
View profile  
 More options May 2 2012, 6:17 pm
From: Matt Warren <mattd...@gmail.com>
Date: Wed, 2 May 2012 15:17:31 -0700 (PDT)
Local: Wed, May 2 2012 6:17 pm
Subject: Re: RavenDB Transformations and Joining Simple Data

In reply to your other question:

> Also, I do not understand how I can call

> let useraddress = database.Load<UserAddressModel>(user.AccountId)
> when there is no specific relationship that has been specified in code
> or anywhere else.

This works because you are just asking RavenDB to load a doc using the
given key (contained in the user.AccountId field), so it does. The code in
your transform result is what sepcifies the relationship.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jamesamuir  
View profile  
 More options May 2 2012, 7:31 pm
From: jamesamuir <jamesam...@gmail.com>
Date: Wed, 2 May 2012 16:31:53 -0700 (PDT)
Local: Wed, May 2 2012 7:31 pm
Subject: Re: RavenDB Transformations and Joining Simple Data
So am I correct in assuming that any relationships need to be stored
in each document? Therefore, if I wanted to associate multiple
addresses with my user the class should look like this?

public class UserModel
    {
        #region Properties
        public string Id { get; set; }
        public string AccountId { get; set; }
        public string UserName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public DateTime InsertDate { get; set; }

        public List<UserAddressModel> UserAddresses { get; set; }

        #endregion
    }

If this is the case, what exactly is the "let" statement used for and
how would I go about getting a list of all user addresses since each
is contained in a specific UserModel?

On May 2, 6:05 pm, Itamar Syn-Hershko <ita...@hibernatingrhinos.com>
wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Itamar Syn-Hershko  
View profile  
 More options May 2 2012, 7:34 pm
From: Itamar Syn-Hershko <ita...@hibernatingrhinos.com>
Date: Thu, 3 May 2012 02:34:51 +0300
Local: Wed, May 2 2012 7:34 pm
Subject: Re: [RavenDB] Re: RavenDB Transformations and Joining Simple Data

inline

On Thu, May 3, 2012 at 2:31 AM, jamesamuir <jamesam...@gmail.com> wrote:
> So am I correct in assuming that any relationships need to be stored
> in each document? Therefore, if I wanted to associate multiple
> addresses with my user the class should look like this?

Yes

You will have one User class (not sure why you name it UserModel) which
will have a list of UserAddress objects, like you did. They are all within
the same transactional boundary.

I'm not following. Just return the User object by Id or by any other
queryable parameter and you'll have the list of address - the entire object
will be returned to you


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jamesamuir  
View profile   Translate to Translated (View Original)
 More options May 2 2012, 7:41 pm
From: jamesamuir <jamesam...@gmail.com>
Date: Wed, 2 May 2012 16:41:21 -0700 (PDT)
Local: Wed, May 2 2012 7:41 pm
Subject: Re: RavenDB Transformations and Joining Simple Data
Sorry, I should have clarified that last statement. Hypothetically, I
wanted to return a list of all of the addresses across the spectrum of
users, similar to SELECT * FROM USERADDRESS. I guess this is what I am
having trouble understanding. If I was using a relational database,
the address table would be abstracted from the user and I would be
able to query against all addresses but still have the ability to
associate each address with a specific user. In RavenDB each address
is compartmentalized at the user level.  Thank you for taking the time
to assist me with this.

On May 2, 7:34 pm, Itamar Syn-Hershko <ita...@hibernatingrhinos.com>
wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Lang  
View profile  
 More options May 2 2012, 7:45 pm
From: Daniel Lang <d.l...@flexbit.at>
Date: Wed, 2 May 2012 16:45:00 -0700 (PDT)
Subject: Re: RavenDB Transformations and Joining Simple Data

If you really want to have a list of all addresses for *some* reason (it's
actually hard to think about a good use-case, but let's say you want to
have all addresses sorted alphabetically) then you can define a map index
on those addresses and use fieldstorage to store the values inside the
lucene index. Using this approach, you can actually get the results from
the index itself.

Am Donnerstag, 3. Mai 2012 01:41:21 UTC+2 schrieb jamesamuir:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jamesamuir  
View profile  
 More options May 2 2012, 8:08 pm
From: jamesamuir <jamesam...@gmail.com>
Date: Wed, 2 May 2012 17:08:56 -0700 (PDT)
Local: Wed, May 2 2012 8:08 pm
Subject: Re: RavenDB Transformations and Joining Simple Data
Daniel,
I was thinking in a more general sense but used the address example as
it was already part of the discussion. If I move beyond the address
example to, lets say, a UserTask i think it would make more sense of
what i would like to accomplish.

public class UserTask
    {
        #region Properties
        public string Id { get; set; }
        public string AccountId { get; set; }
        public string TaskDesc { get; set; }
        public DateTime InsertDate { get; set; }
        #endregion
    }

If each UserTask (which could be many) as associated at the User
level, it seems difficult to obtain a list of all of the tasks. For
example, if I wanted to display a list of the newest tasks and the
user it is assigned to. Im not sure what the index would look like for
something like this if you could provide a simple example for me?

On May 2, 7:45 pm, Daniel Lang <d.l...@flexbit.at> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Itamar Syn-Hershko  
View profile  
 More options May 2 2012, 8:11 pm
From: Itamar Syn-Hershko <ita...@hibernatingrhinos.com>
Date: Thu, 3 May 2012 03:11:11 +0300
Local: Wed, May 2 2012 8:11 pm
Subject: Re: [RavenDB] Re: RavenDB Transformations and Joining Simple Data

Assigning a user to a task doesn't mean you actually are going to embed the
task object within the user object

If you are going to be adding a lot of tasks and querying them like you
mentioned, you will just store UserTask objects with a UserId property that
will hold the User ID, and create an index on it


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jamesamuir  
View profile  
 More options May 2 2012, 11:59 pm
From: jamesamuir <jamesam...@gmail.com>
Date: Wed, 2 May 2012 20:59:45 -0700 (PDT)
Local: Wed, May 2 2012 11:59 pm
Subject: Re: RavenDB Transformations and Joining Simple Data
So here is the index I created to try this using the UserName as the
index

public class UserTasks_ByName : AbstractIndexCreationTask<UserTask>
    {
        public UserTasks_ByName()
        {
            Map = usertasks => from usertask in usertasks
                               select new { UserName =
usertask.UserName };

            TransformResults =
                (database, usertasks) => from usertask in usertasks
                                     let user =
database.Load<UserModel>(usertask.UserName)
                                     select new
                                     {
                                         FirstName = user.FirstName,
                                         LastName = user.LastName,
                                         TaskDesc = usertask.TaskDesc,
                                         InsertDate =
usertask.InsertDate

                                     };
        }
    }

and here is a result in RavenDB Studio for a that index

{
  "FirstName": null,
  "LastName": null,
  "TaskDesc": "New Task",
  "InsertDate": "2012-05-02T23:53:33.6165908"

}

Can you point out what it is that I am doing wrong in this instance?

On May 2, 8:11 pm, Itamar Syn-Hershko <ita...@hibernatingrhinos.com>
wrote:

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Maverix  
View profile  
 More options May 3 2012, 12:16 am
From: Maverix <ajha...@gmail.com>
Date: Wed, 2 May 2012 21:16:07 -0700 (PDT)
Local: Thurs, May 3 2012 12:16 am
Subject: Re: RavenDB Transformations and Joining Simple Data

From my understanding what you are seeing is correct.
In the index those fields are not populated.  

TransformResults are run on demand (not sure if it is client side or server)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Warren  
View profile  
 More options May 3 2012, 4:28 am
From: Matt Warren <mattd...@gmail.com>
Date: Thu, 3 May 2012 01:28:13 -0700 (PDT)
Local: Thurs, May 3 2012 4:28 am
Subject: Re: RavenDB Transformations and Joining Simple Data

TransformResults runs on the server, so in effect it modifies the Json that
is sent out to the client.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Itamar Syn-Hershko  
View profile  
 More options May 3 2012, 4:28 am
From: Itamar Syn-Hershko <ita...@hibernatingrhinos.com>
Date: Thu, 3 May 2012 11:28:36 +0300
Local: Thurs, May 3 2012 4:28 am
Subject: Re: [RavenDB] Re: RavenDB Transformations and Joining Simple Data

The problem is here:

let user = database.Load<UserModel>(usertask.UserName)

You can't load a user based on a property - only based on the document ID,
which is probably usertask.UserName

In this scenario you probably want to use Includes to load all task with
their user documents:
ravendb.net/docs/client-api/querying/handling-document-relationships

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Warren  
View profile  
 More options May 3 2012, 4:30 am
From: Matt Warren <mattd...@gmail.com>
Date: Thu, 3 May 2012 01:30:26 -0700 (PDT)
Local: Thurs, May 3 2012 4:30 am
Subject: Re: RavenDB Transformations and Joining Simple Data

It look like it's not loading the associated user correctly, can you post a
sample showing what the UserTask doc looks like as Json.

But, I think your TransformResult is wrong, try this:

 TransformResults =  (database, usertasks) => from usertask in usertasks
                                     let user
= database.Load<UserModel>(usertask.*UserId*)
                                     select new
                                     {
                                         FirstName = user.FirstName,
                                         LastName = user.LastName,
                                         TaskDesc = usertask.TaskDesc,
                                         InsertDate = usertask.InsertDate
                                     };  

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jamesamuir  
View profile  
 More options May 3 2012, 10:46 pm
From: jamesamuir <jamesam...@gmail.com>
Date: Thu, 3 May 2012 19:46:58 -0700 (PDT)
Local: Thurs, May 3 2012 10:46 pm
Subject: Re: RavenDB Transformations and Joining Simple Data
I managed to get the UserTask example working tonight and I think I'm
starting to understand how to model the data relationships a bit
better in RavenDB. I wish there was a bit more documentation out there
that explained how to handle data relationships beyond the ravendb.net/
docs/client-api/querying/handling-document-relationships post. I keep
reading it but it seems to be over the head of new RavenDB user. I
must say that this is my first foray into NoSQL and I like the way it
integrates into the .Net framework. I guess I'll just keep picking at
it and hope that a book comes out soon. Thank you to everyone that
helped me.

On May 3, 4:30 am, Matt Warren <mattd...@gmail.com> wrote:

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jamesamuir  
View profile  
 More options May 3 2012, 10:49 pm
From: jamesamuir <jamesam...@gmail.com>
Date: Thu, 3 May 2012 19:49:23 -0700 (PDT)
Local: Thurs, May 3 2012 10:49 pm
Subject: Re: RavenDB Transformations and Joining Simple Data
Sorry, I should have mentioned that Itamar and Matt's suggestions were
correct. I needed to use UserId instead of UserName in my index. All
of the result also appear as expected in Raven Studio.

On May 3, 4:30 am, Matt Warren <mattd...@gmail.com> wrote:

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Itamar Syn-Hershko  
View profile  
 More options May 3 2012, 11:04 pm
From: Itamar Syn-Hershko <ita...@hibernatingrhinos.com>
Date: Fri, 4 May 2012 06:04:48 +0300
Local: Thurs, May 3 2012 11:04 pm
Subject: Re: [RavenDB] Re: RavenDB Transformations and Joining Simple Data

See here http://ayende.com/blog/tags/raven and here
http://www.code972.com/blog/tag/eventszilla/

In particular:
http://ayende.com/blog/156193/as-the-userrsquo-s-put-it-insight-into-...
http://ayende.com/blog/84993/document-based-modeling-auctions
http://ayende.com/blog/96257/document-based-modeling-auctions-amp-bids

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »