On Jul 16, 9:48 pm, Robert Stam <
rstam10...@gmail.com> wrote:
> $addToSet is tricky when used with compound documents. The problem is
> that if even one element has a different value, then as far as
> $addToSet is concerned that's a new document to add to the set. You
> may think it's a duplicate, but to $addToSet they look different.
>
> I don't know of any way that you can handle incrementing existing
> attributes and adding them only if they are new in a single statement.
> The best I can suggest is to assume the attribute exists and attempt
> to increment it, but to check whether it actually existed and adding
> it as a new attribute if not. You can use a regular $push to add it
> (no need for $addToSet since you know it's new).
>
> Here's a test program I used to validate the concept. Hope it helps:
>
>
http://www.pastie.org/2225343
>
> Another alternative you could consider would be to fetch the document
> into your C# program and make all the updates to the document in
> memory on the client, and then call collection.Save(document) when you
> are done.
>