Is it possible to batch updates?

50 views
Skip to first unread message

rksprst

unread,
Jan 18, 2011, 6:29:02 PM1/18/11
to mongodb-user
I make the following updates, using the same query:


//update the session count

database.GetCollection<TrackedUser>("TrackedItems").Update(query,
Update.Inc("Sessions.$.ActCount", 1));

//update the last activity date

database.GetCollection<TrackedUser>("TrackedItems").Update(query,
Update.Set("LastACDT", DateTime.UtcNow));

//update the last session date

database.GetCollection<TrackedUser>("TrackedItems").Update(query,
Update.Set("Sessions.$.SessionLastDT", DateTime.UtcNow));


Is there a way to batch them so that they all get executed during on
connection to mongo. Or will this be fast enough? This might get
called thousands of times a minute.

I'm using the 10gen c# driver.

Robert Stam

unread,
Jan 18, 2011, 6:38:49 PM1/18/11
to mongodb-user
Try:

var collection =
database.GetCollection<TrackedUser>("TrackedItems");
var utcNow = DateTime.UtcNow;
var update =
Update
.Inc("Sessions.$.ActCount", 1)
.Set("LastACDT", utcNow)
.Set("Sessions.$.SessionLastDT", utcNow);
collection.Update(query, update);

I recommend only calling DateTime.UtcNow once to avoid the tiny chance
that it might return a different value the second time.
Reply all
Reply to author
Forward
0 new messages