Is it possible to load only a part of a Ravendb document.
I want to fetch a document by id, but only some fields should be fetched.
I know I can use session.Query
with a Select
call, but then I can't query on the id of the document so I have to use session.Load
instead, but this fetches the whole document.
Do I need to create an index for this?
Asked
Active
Viewed 676 times
1

Sven Schelfaut
- 522
- 1
- 7
- 18
1 Answers
1
You can use something called Results Transformers to achieve this.
Say you have an entity "Customer"
public class Customer
{
public string Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }
}
and you want to load only the Id and the Name property, you define a Results Transformer:
public class CustomerNameTransformer : AbstractTransformerCreationTask<Customer>
{
public CustomerNameTransformer()
{
TransformResults = results => from customer in results
select new CustomerNameViewModel
{
Id = customer.Id,
Name = customer.Name
};
}
}
and your "view model":
public class CustomerNameViewModel
{
public string Id { get; set; }
public string Name { get; set; }
}
With this, you can access the Customer entity as a "Customer Name ViewModel" in several ways:
//Load and transform by one id
CustomerNameViewModel viewModel = session.Load<CustomerNameTransformer, CustomerNameViewModel>("customers/1");
//Load and transform by several id's
CustomerNameViewModel[] viewModels = session.Load<CustomerNameTransformer, CustomerNameViewModel>(new[]{ "customers/1", "customers/2"});
//Query and transform
List<CustomerNameViewModel> viewModels = session.Query<Customer>()
.TransformWith<CustomerNameTransformer, CustomerNameViewModel>()
.ToList();
Results Transformers are executed server side before the data is returned to the client. They are created by the same IndexCreation task that creates the index definitions on the server.
You can read more about Results Transformers in the documentation:
Hope this helps!

Jens Pettersson
- 1,167
- 1
- 9
- 14
-
This seems to work, but I have the following remarks: - The feature is obsolete, is there a better alternative? - In this Session.Overload you can't just pass in the id, but it has to be "collection/id" – Sven Schelfaut Mar 17 '15 at 15:26
-
Which feature is obsolete? None of the above (Load with one id, Load with array of ids and Query) are obsolete in the latest stable build (3.0.3599) – Jens Pettersson Mar 17 '15 at 19:36
-
Now I see what you mean. It's not obsolete, it's just that you simply cannot load by a single integer when using transformation. This is because we're not explicitly specifying the type (i.e
) so the Raven Client doesn't know how to prefix the id with the collection name. Every id is stored as a string internally in raven, even if the entity id is only an integer. We (my team) always use string ids and load documents with the full string id ("Customers/1") as it offers several other benefits as well. – Jens Pettersson Mar 18 '15 at 06:55