[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:

var filter = Builders<Application>.Filter.Eq("Platforms.Id", platform.Id);
var documents = await _collection.Find(filter).ToListAsync();

Document:

{
  "_id" : ObjectId("5666aa08e592a021485b18b0"), 
  "Name" : "App1",
  "Platforms" : [{
      "_id" : "5665a0cde592942148a9ac2f",
      "Name" : "Windows"
    }, {
      "_id" : "5665a0d3e592942148a9ac30",
      "Name" : "iOS"
    }]
}
 
{
  "_id" : ObjectId("5666aa19e592a021485b18b1"),  
  "Name" : "App2",
  "Platforms" : {
    "_t" : "System.Collections.Generic.List`1[[MyPortal.Model.DTO.Lookup, MyPortal.Model]]",
    "_v" : [{
        "_id" : "5665a0d3e592942148a9ac30",
        "Name" : "iOS"
      }, {
        "_id" : "5665a0cde592942148a9ac2f",
        "Name" : "Windows x"
      }]
  }  
}



 Comments   
Comment by Alpesh [ 08/Dec/15 ]

Hi Craig,

Just found the culprit. Its reflection.
update = update.Set(prop.Name, prop.GetValue(document));

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 ICollection<Lookup> Platforms

{ get; set; }
}

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 Id

{ get; set; }
public string Name { get; set; }

}

  1. Add Method
    public async Task<T> Add(T document) { await _collection.InsertOneAsync(document); return document; }
  1. Update method
    public async Task<T> UpdateOne(T document)
    {
    var props = typeof(T).GetProperties();

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
Alpesh

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

Generated at Wed Feb 07 21:39:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.