I am trying to have one db call to either add an element to a nested array or if that element already exists increase a counter on it. In this case we are trying to see if there is a matching SubDocument, if none is found we want to add that sub document to the SubDocs list if one is found we want to increment the Count prop on the SubDocument found in that list.
The reason we are looking for one call to do this is because we will have several services potentially trying to do the same thing at the same time.
public class Document
{
[BsonId]
[BsonIgnoreIfDefault]
[BsonRepresentation(BsonType.ObjectId)]
public string DocumentID { get; set; }
public int Date { get; set; }
public List<SubDocument> SubDocs { get; set; }
}
public class SubDocument
{
public string Count { get; set; }
public string Name { get; set; }
}
Any help would be greatly appreciated. We are using the .NET driver 2.8.1
I was able to do this using a find, and if its found update the counter. But testing so far shows counts will get missed if multiple services are doing this.