FindAndModify upsert capability

89 views
Skip to first unread message

Gabriel Duford

unread,
Aug 17, 2010, 10:21:16 PM8/17/10
to mongodb-csharp
Hi,
I am implementing some kind of serial identifier (simple incrementing
counter) functionality as part of a WCF service hosted in IIS. I want
to store the counters in a Mongo collection.
I use the IMongoCollection.FindAndModify with a Mo.Inc(...) document
successfully when the document already exists in the collection.
However, the first time my "UserFriendlyUniqueIdGenerator" service is
accessed, the document doesn't exist, and I can't just check for
existence and create it if it doesn't exist because that must be done
atomically (race condition).

I found out in the MondoDB documentation that it is possible to
specify an "upsert" option to the FindAndModify command. (http://
www.mongodb.org/display/DOCS/findandmodify+Command and
http://github.com/mongodb/mongo/blob/master/jstests/find_and_modify4.js)
I looked at the mongodb-csharp code and this option is not exposed.

I think I can build and call the command myself and extract the
result, but it would be nice if this option could be exposed by the
driver in a future version. That would allow to create or increment a
counter atomically.

Can't wait for the next official build.

gabriel

Evo

unread,
Aug 18, 2010, 11:10:09 AM8/18/10
to mongodb-csharp
Take a look at the overload for the Update method on Collection that
takes a document, a selector, and an UpdateFlags. You will have to do
something similar to...

database.GetCollection<Document>("MyCollection").Update(new
Document("FieldToIncrement", new Document("$inc", 1)), new Document(),
UpdateFlags.Upsert)

I didn't test that but should be something similar...

On Aug 17, 7:21 pm, Gabriel Duford <gabrielduf...@gmail.com> wrote:
> Hi,
> I am implementing some kind of serial identifier (simple incrementing
> counter) functionality as part of a WCF service hosted in IIS. I want
> to store the counters in a Mongo collection.
> I use the IMongoCollection.FindAndModify with a Mo.Inc(...) document
> successfully when the document already exists in the collection.
> However, the first time my "UserFriendlyUniqueIdGenerator" service is
> accessed, the document doesn't exist, and I can't just check for
> existence and create it if it doesn't exist because that must be done
> atomically (race condition).
>
> I found out in the MondoDB documentation that it is possible to
> specify an "upsert" option to the FindAndModify command. (http://www.mongodb.org/display/DOCS/findandmodify+Commandandhttp://github.com/mongodb/mongo/blob/master/jstests/find_and_modify4.js)

Gabriel Duford

unread,
Aug 18, 2010, 11:50:59 AM8/18/10
to mongodb-csharp
I tested that this works. Thanks.
However, unless there's a way to get the updated document, I can't use
this. Sad, since this is way simpler than the creating the
findandmodify command with the upsert option...

gabriel

On Aug 18, 11:10 am, Evo <evopatr...@gmail.com> wrote:
> Take a look at the overload for the Update method on Collection that
> takes a document, a selector, and an UpdateFlags. You will have to do
> something similar to...
>
> database.GetCollection<Document>("MyCollection").Update(new
> Document("FieldToIncrement", new Document("$inc", 1)), new Document(),
> UpdateFlags.Upsert)
>
> I didn't test that but should be something similar...
>
> On Aug 17, 7:21 pm, Gabriel Duford <gabrielduf...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Hi,
> > I am implementing some kind of serial identifier (simple incrementing
> > counter) functionality as part of a WCF service hosted in IIS. I want
> > to store the counters in a Mongo collection.
> > I use the IMongoCollection.FindAndModify with a Mo.Inc(...) document
> > successfully when the document already exists in the collection.
> > However, the first time my "UserFriendlyUniqueIdGenerator" service is
> > accessed, the document doesn't exist, and I can't just check for
> > existence and create it if it doesn't exist because that must be done
> > atomically (race condition).
>
> > I found out in the MondoDB documentation that it is possible to
> > specify an "upsert" option to the FindAndModify command. (http://www.mongodb.org/display/DOCS/findandmodify+Commandandhttp://gi...)

Steve Wagner

unread,
Aug 18, 2010, 12:51:05 PM8/18/10
to mongodb...@googlegroups.com
Hi Gabriel, thanks for your report. Could you please report this request
to our issue tracker at http://jira.mongodb.org/secure/Dashboard.jspa so
we dont overlook it?

Thanks!

-Steve

Gabriel Duford

unread,
Aug 18, 2010, 1:34:44 PM8/18/10
to mongodb-csharp
Done http://jira.mongodb.org/browse/CSHARP-57
Thanks guys!

gabriel
Reply all
Reply to author
Forward
0 new messages