1

Iam trying to insert object to mongodb and I didnt find how. In my way the object inserted gets the id 00000

Here is my code:

public static async Task InsertData<T>(string collection_name, object insertData) 
DB mongoConnection = new DBconnection();

var collection = mongoConnection._database.GetCollection<object>(collection_name);

await collection.InsertOneAsync(insertData)
}
ronmar
  • 39
  • 2
  • 13

1 Answers1

-1

What is the sample model of insertData?

MongoDB should generate id automatically if you mark Id field in your models with attributes:

[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
marcinax
  • 1,067
  • 7
  • 10
  • I edit the model iam inserting to what you wrote and now the id is null. This is what it was before: [BsonId] [BsonElement("Identify")] public ObjectId _id {get; set;} – ronmar Nov 26 '15 at 11:40
  • 1
    I found this: http://stackoverflow.com/questions/4594270/mongodb-automatically-generated-ids-are-zeroes @marcinax seems like driver bug – ronmar Nov 26 '15 at 11:49