简体   繁体   中英

Cannot deserialize a 'String' from BsonType 'ObjectId'

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

It looks like some _id in your collection cannot be converted to ObjectId. The below code works well:

        // the below is not required, but it works with that too
        //BsonClassMap.RegisterClassMap<HotelDocument>(cm =>
        //{
        //    cm.AutoMap();
        //    cm.SetIgnoreExtraElements(true);
        //    cm.MapIdMember(c => c.Id).SetSerializer(new StringSerializer(BsonType.ObjectId)).SetIgnoreIfDefault(true);
        //});

        var id = ObjectId.GenerateNewId();
        var client = new MongoClient();
        var db = client.GetDatabase("d");
        var coll = db.GetCollection<HotelDocument>("c");
        coll.InsertOne(new HotelDocument() { Id = id.ToString() });

        var result = coll.Find(c => c.Id == id.ToString()).ToList();

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

HashSet<BsonType> inconsistentIds = new HashSet<BsonType>();
var cursor = await collection.Find(new BsonDocument()).Project(Builders<BsonDocument>.Projection.Include("_id")).ToCursorAsync();
foreach (BsonDocument doc in cursor.ToEnumerable())
     inconsistentIds.Add(doc["_id"].BsonType);

Console.WriteLine($"There is {inconsistentIds.Count} id type");

https://github.com/iso8859/learn-mongodb-by-example/blob/main/dotnet/02%20-%20Intermediate/CheckIdConsistency.cs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM