'Get BsonId before inserting to database ASP.NET
I'm working on a custom attribute that i need to build from the _Id generated by Mongo.
Is there a way to do it before inserting the document ?
My C# Source - Model Class EmployeeModel
public class EmployeeModel
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("build_obj")]
[JsonProperty("build_obj")]
public string UrlBuild { get; set; }
}
What I want to achieve
{
"_id" : ObjectId("575845a713d284da0ac2ee81"),
"build_obj" : "thisIsanExample.com/575845a713d284da0ac2ee81",
}
{
"_id" : ObjectId("575845d213d284da0ac2ee82"),
"build_obj" : "thisIsanExample.com/575845a713d284da0ac2ee81,
}
Here's the service
private static IMongoClient _client;
private static IMongoDatabase _database;
_client = new MongoClient();
_database = _client.GetDatabase("RMS");
var collection = _database.GetCollection<EmployeeModel>("Employee");
public Employee Create(Employee emp)
{
emp.build_obj = "thisIsanExample.com/" + emp.Id
_employees.InsertOne(emp);
}
But couldn't manage to do it, Kindly assist me how to fetch the Id before insert on database ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
