-
Type: Bug
-
Resolution: Fixed
-
Priority: Unknown
-
Affects Version/s: None
-
Component/s: None
-
None
Summary
When a document class has a property of type IList<string> and is decorated with the attribute to set the type as ObjectId, then assigning that property a value of List<string> properly respects the ObjectId type, but a string[] does not and stores the data as a string array in the database.
Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).
2.18.0
How to Reproduce
class TestDocument { [BsonRepresentation(BsonType.ObjectId)] public IList<string> MyObjectIdList { get; set; } } var objId = ObjectId.GenerateNewId().ToString(); // This is incorrectly encoded to the database as an array of strings (not respecting the ObjectId representation) var doc = new TestDocument { MyObjectIdList = new[] { objId } }; await collection.InsertOneAsync(document); // This does not find the inserted document (the search respects the ObjectId representation and attempts to find with an ObjectId value) await collection.Find(Builders<TestDocument>.Filter.AnyEq(doc => doc.MyObjectIdList, objId)).ToListAsync(); // This is correctly encoded to the database as an array of ObjectIds var doc = new TestDocument { MyObjectIdList = new List<string> { objId } }; await collection.InsertOneAsync(document); // This finds the inserted document await collection.Find(Builders<TestDocument>.Filter.AnyEq(doc => doc.MyObjectIdList, objId)).ToListAsync();
Additional Background
None