Earlz Earlz - 1 year ago 70
C# Question

Get _id of an inserted document in MongoDB?

say I have a product listing. When I add a new product I save it using something like

var doc=products.Insert<ProductPDO>(p);

The problem is that I want after this is done to redirect the user to the page with the product. So I need to redirect to say

However, I see no way of getting the ObjectID right afterwards without manually querying the database and look for a document with all the same fields and such.

Is there an easier way? (also,
in this instance returns null for some reason)

Answer Source

You can check the id field of the inserted document. It should be filled in.

Edited by asker:

Just to be clear, in order to make an id field in your own classes you just use:

public ObjectId ID{get;set;}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download