Hi Team, Please find below error for inserting document from .Net to MongoDb Collection.
‘69f2cd42-b6a4-418f-a4c2-ece3d719ed6f’ is not a valid 24 digit hex string.
Id Property is string GUID which is accepting when I am inserting document from MongoDbCompass or web Collection Explorer but whenever we insert the same from .net code, it generating exception.
Details Exception:
at MongoDB.Bson.ObjectId.Parse(String s)
at MongoDB.Bson.Serialization.Serializers.StringSerializer.SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, String value)
at MongoDB.Bson.Serialization.Serializers.SealedClassSerializerBase`1.Serialize(BsonSerializationContext context, BsonSerializationArgs args, TValue value)
at MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Serialize(BsonSerializationContext context, BsonSerializationArgs args, Object value)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Serialize(IBsonSerializer serializer, BsonSerializationContext context, Object value)
at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.SerializeNormalMember(BsonSerializationContext context, Object obj, BsonMemberMap memberMap)
at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.SerializeClass(BsonSerializationContext context, BsonSerializationArgs args, TClass document)
at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.Serialize(BsonSerializationContext context, BsonSerializationArgs args, TClass value)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Serialize[TValue](IBsonSerializer`1 serializer, BsonSerializationContext context, TValue value)
at MongoDB.Driver.OperatorUpdateDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.CombinedUpdateDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.CombinedUpdateDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.CombinedUpdateDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.CombinedUpdateDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.CombinedUpdateDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.MongoCollectionImpl`1.ConvertWriteModelToWriteRequest(WriteModel`1 model, Int32 index)
at System.Linq.Enumerable.<SelectIterator>d__174`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation..ctor(CollectionNamespace collectionNamespace, IEnumerable`1 requests, MessageEncoderSettings messageEncoderSettings)
at MongoDB.Driver.MongoCollectionImpl`1.CreateBulkWriteOperation(IClientSessionHandle session, IEnumerable`1 requests, BulkWriteOptions options)
at MongoDB.Driver.MongoCollectionImpl`1.<BulkWriteAsync>d__30.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at MongoDB.Driver.MongoCollectionImpl`1.<UsingImplicitSessionAsync>d__106`1.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at MongoDB.Driver.MongoCollectionBase`1.<UpdateOneAsync>d__107.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Core.User.Infrastructure.NoSql.MongoDb.User.Repositories.UserRepository.<AddAsync>d__4.MoveNext() in
[BsonRepresentation(BsonType.ObjectId)]
[JsonProperty("id")]
public string Id { get; set; }
JSON working when inserting from MongoDbCompass:
"_id": "69f2cd42-b6a4-418f-a4c2-ece3d719ed6f",
"UserName": "SC1005",
"EmailID": "[email protected]",
"Password": "APt/zqxocK9haWvu5Iv8+NgWSUuWzep8HrFFRcSkrOveYsBVQDYn7YfxFRVE99R8Mw==",
"FullName": "Pravin Yadav",
"LastPwdChangedDateTime": "2021-08-09T12:54:05.62+05:30",
"UserGroup": {
"_id": "69f2cd42-b6a4-418f-a4c2-ece3d719ed6f",
"GroupName": "Administrator"
"UserVisibility": {
"_id": "69f2cd42-b6a4-418f-a4c2-ece3d719ed6f",
"GroupName": "Administrator"
"ShowLocalLanguage": false,
"CreatedDetails": {
"UserId": "69f2cd42-b6a4-418f-a4c2-ece3d719ed6f",
"SessionId": "69f2cd42-b6a4-418f-a4c2-ece3d719ed6f",
"UserName": "SC1005",
"Timestamp": "2021-08-09T12:54:05.62+05:30"
public UserResponse(string id, string userName, string emailID, string password, string fullName, UserGroupResponse userGroup, UserVisibilityResponse userVisibility, bool showLocalLanguage, DateTimeOffset? lastPwdChangedDateTime, bool active, bool isDeleted, ActivityDetails createdDetails, ActivityDetails lastUpdatedDetails)
Id = id;
UserName = userName;
EmailID = emailID;
Password = password;
FullName = fullName;
UserGroup = userGroup;
UserVisibility = userVisibility;
ShowLocalLanguage = showLocalLanguage;
LastPwdChangedDateTime = lastPwdChangedDateTime;
Active = active;
IsDeleted = isDeleted;
CreatedDetails = createdDetails;
LastUpdatedDetails = lastUpdatedDetails;
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
[JsonProperty("id")]
public string Id { get; set; }
public string UserName { get; set; }
public string EmailID { get; set; }
public string Password { get; set; }
public string FullName { get; set; }
public UserGroupResponse UserGroup { get; set; }
public UserVisibilityResponse UserVisibility { get; set; }
public bool ShowLocalLanguage { get; set; }
public DateTimeOffset? LastPwdChangedDateTime { get; set; }
public bool Active { get; set; }
public bool IsDeleted { get; set; }
public ActivityDetails CreatedDetails { get; set; }
public ActivityDetails LastUpdatedDetails { get; set; }
@Pravin_Yadav you need to decorate the model with this attribute [BsonId].
public class UserResponse
[BsonId]
public string Id { get; set; }
public string UserName { get; set; }
static void Main(string[] args)
MongoClient dbClient = new MongoClient("mongodb://localhost:27017");
var database = dbClient.GetDatabase("test");
database.GetCollection<UserResponse>(nameof(UserResponse)).InsertOne(new UserResponse { Id = Guid.NewGuid().ToString(), UserName = "Test User" });
and DotNet 5
private readonly IMongoDatabase _db;
private readonly IMongoCollection<Application.User.Responses.UserResponse> _collection;
private readonly MongoClient _client;
public UserRepository(MongoClient client)
_client = client;
_db = _client.GetDatabase("UserDatabase");
_collection = _db.GetCollection<UserResponse>("Users");
public void Add(UserResponse item, CancellationToken cancellationToken)
_collection.InsertOne(item);
Get started on your Generative AI adventure with Atlas Vector Search. Click here for more info.