Frage

I am new to MongoDB and want to serialize my object efficiently. That means when updating an object i want only to write the fields that changed. If my object has sub objects in a list, i only want to add or remove the changed sub objects. That is because if i have a post with 1000 comments i want to avoid to serialize the whole object each time a new post comes around.

all i found is the convention ignoreifempty, ignoreisdefault and the shouldserializexyz pattern.

is it possible to write a convention like ignoreifnotchanged (i track the dirty fields in my objects) or is there a more general shouldserializexyz because i don't want to write the method for every property.

War es hilfreich?

Lösung

What you want to look into is the Update method with an update argument that specifies which fields you want to update.

You can use the Update builder to build the update argument value. Look into:

Update.Set(name, value)
Update.Push(name, value)

(there are many more also).

You can chain Update methods together to update more than one field at a time.

There is currently no built in change tracking, so you would have to keep track of which fields have changed yourself, and build the update argument based on your knowledge of what changed.

There is also a pending JIRA feature request that is related to this. See:

https://jira.mongodb.org/browse/CSHARP-237

Please comment or vote on the JIRA if you want.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top