Update existing documents with new (c# read-only) property

383 views
Skip to first unread message

Trygve Lorentzen

unread,
Mar 26, 2015, 4:57:20 AM3/26/15
to rav...@googlegroups.com

What is the best approach for updating existing documents with the value of a get-only (read-only) property in the C# class? Basically all I need is to load all documents and SaveOrUpdate them for the new (calculated) property to be stored in the document. If using the patching API, would I need to implement the same logic in javascript that I do in C# to calculate the value?

Michael Yarichuk

unread,
Mar 26, 2015, 5:12:03 AM3/26/15
to rav...@googlegroups.com
If the logic for calculated property is not too complicated, I would go for implementing it in javascript and use it in patching requests. More efficient this way.

On Wed, Mar 25, 2015 at 3:11 PM, Trygve Lorentzen <try...@lorentzenconsult.com> wrote:

What is the best approach for updating existing documents with the value of a get-only (read-only) property in the C# class? Basically all I need is to load all documents and SaveOrUpdate them for the new (calculated) property to be stored in the document. If using the patching API, would I need to implement the same logic in javascript that I do in C# to calculate the value?

--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Best regards,

 

Michael Yarichuk

RavenDB Core Team

Tel: 972-4-6227811

Fax:972-153-4-6227811

Email : michael....@hibernatingrhinos.com

 

RavenDB paving the way to "Data Made Simple" http://ravendb.net/  

Daniel Lidström

unread,
Mar 26, 2015, 9:29:33 AM3/26/15
to rav...@googlegroups.com
It depends on the amount of documents. One simple approach for a relatively small number of documents is to simply migrate them all:

var query = DocumentSession.Query<YourModel, YourModelIndex>();
var current = 0;
while (true)
{
    var results = query.Skip(current)
                       .Take(128)
                       .ToArray();
    if (results.Length == 0) break;
    foreach (var result in results)
    {
        // do anything with result, or just wait for SaveChanges...
    }

    current += results.Length;
}

If you have a large amount of documents you may have to create new document sessions for every x items.

Don't make this any more complex (i.e. JavaScript) than it needs to be!

Grisha Kotler

unread,
Mar 26, 2015, 10:09:18 AM3/26/15
to rav...@googlegroups.com
If you want to iterate over the entire set of a query result, you should use the Streaming API.

And saving the documents using bulk insert.

Hibernating Rhinos Ltd  cid:image001.png@01CF95E2.8ED1B7D0

Grisha Kotler l RavenDB Core Team Developer Mobile: +972-54-586-8647

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

RavenDB paving the way to "Data Made Simplehttp://ravendb.net/

--

Chris Marisic

unread,
Mar 27, 2015, 11:19:46 AM3/27/15
to rav...@googlegroups.com
In situations like this I would always choose to use the patch api first. I would create the property in C# and then use the patch api to update existing documents.

If the property was hard to reproduce in the patch api I would load and save all the documents. For this scenario I preferred using Advanced.LoadStartingWith, at this point it's probably easier to use the document streaming 
Reply all
Reply to author
Forward
0 new messages