17

If you add, delete or rename a property on a persisted entity, what's the easiest way to update the documents in RavenDB?

henningc
  • 215
  • 1
  • 2
  • 7
  • 1
    Look at the documentation here, it is well described: http://ravendb.net/docs/client-api/partial-document-updates – Dofs Jun 07 '12 at 10:24

2 Answers2

21

raven also has object tracking. so the following works:

var doc = _session.Load<MyDocType>(docId);
doc.PropertyToChange = "New Value";
_session.SaveChanges();
jlew
  • 10,491
  • 1
  • 35
  • 58
  • Say, `MyDocType` contains less fields than there are in the document, will this remove all fields that are not in `MyDocType`? – Caramiriel Mar 11 '16 at 09:19
  • @Caramiriel Yes - because RavenDB is "self-healing", when you save the document back then only fields on the class will be saved back, and any other data from the document will be erased. This behaviour can be changed by setting `new DocumentStore().Conventions.PreserveDocumentPropertiesNotFoundOnModel`. – adrian Apr 12 '16 at 14:01
16

RavenDB supports PATCH commands, see the docs for more info for more info. This way you can update a document directly without having to pull it from the server, update it and then send it back.

Also you can run patches over multiple documents by using Set-based queries, see here for some more info. This lets you do the equivalent of

UPDATE Users
SET IsActive = false
WHERE LastLogin < '2010-05-10'
ashansky
  • 730
  • 2
  • 14
  • 34
Matt Warren
  • 10,279
  • 7
  • 48
  • 63