How to get id of last document inserted with csharp driver?

3,599 views
Skip to first unread message

Projapati

unread,
Apr 19, 2011, 12:04:57 AM4/19/11
to mongodb-user
MongoCollection<BsonDocument> pa =
db.GetCollection<BsonDocument>("photos");
SafeModeResult result = pa.Insert(docPhoto, SafeMode.True);

I must know the id of the new document like I get from
SCOPE_IDENTITY() in SQL

How do I get the object id used by mongo?

Keith Branton

unread,
Apr 19, 2011, 12:08:43 AM4/19/11
to mongod...@googlegroups.com
Doesn't docPhoto contain the _id field after the insert? That's how the java driver works. Maybe C# driver works the same way?


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Projapati

unread,
Apr 19, 2011, 12:15:04 AM4/19/11
to mongodb-user
I saw that code from Java driver. CSharp driver is little different.

I am pregenerating the objectid and assigning to the doc and then
insert.

Checking if that works.

Thanks Keith.

Projapati

unread,
Apr 19, 2011, 12:28:25 AM4/19/11
to mongodb-user
That didn't do any good.
The document get created in db but I get all 0000000000000000 as
object id.

BsonDocument docPhoto = new BsonDocument();
ObjectId oid = new ObjectId();
docPhoto.SetDocumentId(oid);

//set other fields

MongoCollection<BsonDocument> pa =
db.GetCollection<BsonDocument>("photos");
SafeModeResult result = pa.Insert(docPhoto, SafeMode.True);

strNewPhotoId = oid.ToString(); <--- prints all 0000000

Anyway, I will debug.

Scott Hernandez

unread,
Apr 19, 2011, 12:30:49 AM4/19/11
to mongod...@googlegroups.com
Both ways work in java, and here (and pretty much all the drivers for
that matter).

Robert Stam

unread,
Apr 19, 2011, 9:28:03 AM4/19/11
to mongodb-user
The simplest way is to let the driver assign the Id value
automatically and just check it afterwards:

var document = new BsonDocument { { "x", 1 }, { "y", 2 } };
collection.Insert(document);
var id = document["_id"].AsObjectId;

If you want to generate the Id yourself, the simplest way is:

var id = ObjectId.GenerateNewId();
var document = new BsonDocument { { "_id", id }, { "x", 1 },
{ "y", 2 } };
collection.Insert(document);

A few notes on your sample code:

- ObjectId is a value type (a C# struct)
- new ObjectId() creates an ObjectId of all zeros (or use
ObjectId.Empty)
- use ObjectId.GenerateNewId() to generate a new ObjectId
- SetDocumentId is not normally called by client code, just assign a
value to document["_id"]
- When Insert generates a new Id in you pa variable it doesn't change
your oid variable

On Apr 19, 12:30 am, Scott Hernandez <scotthernan...@gmail.com> wrote:
> Both ways work in java, and here (and pretty much all the drivers for
> that matter).
>

Projapati

unread,
Apr 19, 2011, 12:40:47 PM4/19/11
to mongodb-user
I already got it. Thank you very much.
Reply all
Reply to author
Forward
0 new messages