[CSHARP-1496] Search doesn't work when embedded document stored with type Created: 08/Dec/15 Updated: 05/Apr/19 Resolved: 08/Dec/15 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Operations |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Task | Priority: | Major - P3 |
| Reporter: | Alpesh | Assignee: | Unassigned |
| Resolution: | Done | Votes: | 0 |
| Labels: | question | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
I have got 2 documents in a collection. Second document is updated once and so has got different representation of Platforms (with type and value). It gets serialized into C# object properly but when I search inside Platforms array using platform id, I get only one record back which is first document. I am using following C# code for the search:
Document:
|
| Comments |
| Comment by Alpesh [ 08/Dec/15 ] |
|
Hi Craig, Just found the culprit. Its reflection. prop.GetValue doesn't return exact type and so mongodb saves the document with discriminator. I have tried converting value to its original type before saving and it worked. Thanks for your time. Really appreciate your help. Alpesh |
| Comment by Alpesh [ 08/Dec/15 ] |
|
It uses discriminator once the document is updated. (http://mycodeonline.com/peterkneale/blog/mongo-type-discriminators) I have copied my generic add/update methods here. public class Application: LookupEntity } public class LookupEntity: MongoEntity { public string Name { get; set; } public string Description { get; set; }} public class MongoEntity { [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } } public class Lookup public string Name { get; set; } }
UpdateDefinition<T> update = new BsonDocumentUpdateDefinition<T>(new BsonDocument()); foreach (var prop in props.Where(p => p.CustomAttributes.All(a => a.AttributeType != typeof(NoUpdateAttribute)))) { update = update.Set(prop.Name, prop.GetValue(document)); }await _collection.UpdateOneAsync(d => d.Id == document.Id, update);//.ContinueWith(x=>UpdateParentRecords(document)); return document; |
| Comment by Craig Wilson [ 08/Dec/15 ] |
|
Could you provide the code for the Application class? It should never save the class in different forms, which is what is happening above. |
| Comment by Alpesh [ 08/Dec/15 ] |
|
Hi Craig, Apologies for posting it here and thanks for your answer. After spending lots of time, I have realized that _t is called discriminator. These documents were added/updated by the same application. For C# both the documents are same and they both get deserialized same way, resulting in similar objects. Its just mongodb or may be c# driver doesn't understand this representation while searching. Anyway I will see if I can figure out a way around. Thanks |
| Comment by Craig Wilson [ 08/Dec/15 ] |
|
Hi Alpesh, We generally reserve JIRA for bug tickets and feature requests. Questions like this should be posed at the mongodb-user's list: https://groups.google.com/forum/#!forum/mongodb-user. To answer your question, the two documents you posted are not the same. In one, it's Platforms._id, and in the other it's Platforms._v._id. If these were saved from different applications, then the applications are not saving in the same format. Craig |