I am working a asp .net core project with mongoDb.
I am trying to get hotel by hotelCode.
HotelBedsContentService.cs
public async Task<HotelDocument> GetHotel(int code)
{
var x = await HotelCollection.Find(x => x.Code == code).FirstOrDefaultAsync();
return x;
}
When passing this line, I got below error.
await _hotelBedsContentService.GetHotel(hotel.HotelCode);
System.FormatException: An error occurred while deserializing the Destination property of class TechneTravel.Domain.Documents.HotelBedsContents.HotelDocument: An error occurred while deserializing the Id property of class TechneTravel.Domain.Documents.HotelBedsContents.DestinationDocument: Cannot deserialize a 'String' from BsonType 'ObjectId'.
---> System.FormatException: An error occurred while deserializing the Id property of class TechneTravel.Domain.Documents.HotelBedsContents.DestinationDocument: Cannot deserialize a 'String' from BsonType 'ObjectId'.
---> System.FormatException: Cannot deserialize a 'String' from BsonType 'ObjectId'.
at MongoDB.Bson.Serialization.Serializers.StringSerializer.DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
...........
HotelDocument.cs
public class HotelDocument
{
public string Id { get; set; }
}
Configuration.cs
BsonClassMap.RegisterClassMap<HotelDocument>(cm =>
{
cm.AutoMap();
cm.SetIgnoreExtraElements(true);
cm.MapIdMember(c => c.Id).SetSerializer(new StringSerializer(BsonType.ObjectId)).SetIgnoreIfDefault(true);
});
Why this error occurs? Is there any mistake in my configuration?
Please help me find the issue
2
Answers
It looks like some
_id
in your collection cannot be converted to ObjectId.The below code works well:
please provide a data sample that trigger your issue
MongoDB handles automatic id generation on Insert for ObjectId type only.
If you want to use string or int or long then you need to set the id before you insert.
You can check your data consistency with something like
https://github.com/iso8859/learn-mongodb-by-example/blob/main/dotnet/02%20-%20Intermediate/CheckIdConsistency.cs