Search code examples
c#.netmongodbmongodb-.net-driver

How does Builders<TA>.Update.Set() handle parallel access?


Assuming 'items' (provided as a parameter in the Set() method below) is defined as List (TM is some structure type, i.e. made of properties only) containing only one item, and 10 processes trying to write simultaneously an update to items (without adding anything to the List):

Builders<TK>.Update.Set("Items", items);

An update means that there is at least one element in the BSON with a modified value.

What will MongoDB in such case?

Will it eventually overwrite with the last process into that single element (leaving that List with a single element)?

Or will it add 10 such elements (items) into that List?


Solution

  • I don't know how doesit work with parallel access. But Set operator always overwrite value with an inserted.

    To add new values to an array you should use addToSet operator (it adds only new values) or push operator.

    You could access them as Builders too:

    Builders<TA>.Update.AddToSet
    Builders<TA>.Update.Push