1

Possible Duplicate:
How to set primary key in mongodb?

As I understood _id is generated by mongodb. Is there any way to set it manually? And if so, how to do it using C# driver?

Community
  • 1
  • 1
Oleg Dudnyk
  • 1,009
  • 1
  • 10
  • 22

2 Answers2

4

I managed to find answer by myself. For C# driver it is enough to give the name Id or _id to desired property, and not to have ObjectId type property in your class. Then inside driver somehow decide that this property is _id for your data in mongodb.

Oleg Dudnyk
  • 1,009
  • 1
  • 10
  • 22
  • I'm using string type for the property and it is null(in mongo). Can you share your code. public string _id { get; set; } – RollerCosta Oct 24 '17 at 12:20
1

Yes, you can generate an ObjectID at the client side. Every client library should provide a way to that. Here's how it is in ruby driver

require 'mongo'
oid = BSON::ObjectId.new

But the _id field doesn't have to be an ObjectId. It can be anything (except for array). You can provide your own value when inserting the document and mongo will happily accept it.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367