Using addToSet and $inc in one call?

740 views
Skip to first unread message

Ryan

unread,
Jul 16, 2011, 5:43:34 PM7/16/11
to mongodb-csharp
I have an embedded document that I'd like to examine.
If a value is there increment it.
If it isn't create it and set it's initial value to the increment
value

Do I use $inc somehow or just '+=' in c#? Or something else
completely?

Here is the docuement
{
"_id": {
"$oid": "4e2201b45f60ea1abcb01325"
},
"bActionArray": [
{
"bActionTimeStamp": "Sat, 16 Jul 2011 14:25:08 GMT -07:00",
"bAction": "BCreated",
"affectedBID": "",
"bLocation": [
-122.123456,
47.12345
]
}
],
"bAttributeArray": [
{
"attribute": "largeBackyard",
"attributeValue": 3
},
{
"attribute": "graniteCounters",
"attributeValue": 6
},
{
"attribute": "waterView",
"attributeValue": 1
}
],
"bStatus": "Active"
}

And here's the method that is able to add attributes, but I can't get
it to find them if they're already there and increment. It's just
adding a duplicate with the new value.

public bool UpdateBAttribute(string bID, string bAttribute, int
incrementValue)
{
MongoCollection _b =
MongoConnection.GetMongoCollection("b");
ObjectId queryId = new ObjectId(bID);

var query = Query.And(
Query.EQ("_id",queryId),
Query.EQ("bAttributeArray.attribute",bAttribute)
);

BAttribute ba = new BAttribute();
ba.attribute = bAttribute;
ba.attributeValue += incrementValue;

var update = Update.AddToSet("bAttributeArray",
ba.ToBsonDocument());


try
{
var result = _b.Update(query, update);
return true;

}
catch
{
return false;
}
}

Any help would be great!! Thanks.

Robert Stam

unread,
Jul 17, 2011, 12:48:52 AM7/17/11
to mongodb-csharp
$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.

Ryan

unread,
Jul 18, 2011, 12:21:13 AM7/18/11
to mongodb-csharp
Thanks a ton Robert! I think this is the right track, but I have a
problem...

when we reach this line:
var result = collection.Update(query, update);
result is always null...


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.
>

Scott Hernandez

unread,
Jul 18, 2011, 12:31:09 AM7/18/11
to mongodb...@googlegroups.com
Did you make sure that you are doing Safe writes?

Ryan

unread,
Jul 18, 2011, 12:38:27 AM7/18/11
to mongodb-csharp
Thanks Scott. I've been living dangerously till now! I didn't realize
I needed to be in safemode. Lots of things make sense now :)

On Jul 17, 9:31 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:
> Did you make sure that you are doing Safe writes?
>
Reply all
Reply to author
Forward
0 new messages