I am using C# mongodb driver to update record in mongodb. Below code works fine for me but it's automatically converting all occurrences of "id" to "_id".
var client = GetMongoClient();
var collection1 = GetMongoCollection("collection1");
var collection2 = GetMongoCollection("collection2");
using (var session = await client.StartSessionAsync())
{
session.StartTransaction();
try
{
var filter = Builders<BsonDocument>.Filter.Eq("_id", projectInfo._Id);
BsonDocument projectInfoBD = projectInfo.ToBsonDocument();
var recordAfterUpdate = await collection1.ReplaceOneAsync(session, filter, projectInfoBD);
......
}
catch (Exception ex)
{
await session.AbortTransactionAsync();
return false;
}
}
My C# classes
public class ProjectInfo
{
public string _Id { get; set; } //This is primary key which unique for project info
public ProjectBasicDetail BasicDetails { get; set; }
}
public class ProjectBasicDetail
{
public string Name { get; set; }
public string Description { get; set; }
public Option Status { get; set; }
public TextOption CreatedBy { get; set; }
}
public class TextOption
{
public string Id { get; set; } //don't want to convert to "_id"
public string Name { get; set; }
}
public class Option
{
public int Id { get; set; } //don't want to convert to "_id"
public string Name { get; set; }
}
After updating record I am expecting updated record to look like this
{
"_id": "kjsldfkjlsdkfjsd",
"basicDetails": {
"name": "test name",
"description": "test desc",
"status": {
"id": 11,
"name": "processing"
},
"createdBy": {
"id": "123",
"name": "some user"
}
}
}
but it saved like below one. It's converting all occurrences of "id" to "_id" which I don't want
{
"_id": "kjsldfkjlsdkfjsd", //This is ok
"basicDetails": {
"name": "test name",
"description": "test desc",
"status": {
"_id": 11, //This should be "id"
"name": "processing"
},
"createdBy": {
"_id": "123", //This should be "id"
"name": "some user"
}
}
}
Thanks in advance...