Element 'Id' does not match any field or property of class

Viewed 51575

I got the result from the collection in MongoDB, the structure is the same as below

[DataContract]
public class Father
{
    [BsonId]
    [DataMember]
    public MongoDB.Bson.ObjectId _id { get; set; }

    [DataMember]
    public string Id { get; set; }

    [DataMember]
    public List<Child> childs { get; set; }
}

[DataContract]
public class Child
{
    [DataMember]
    public string Id { get; set; }

    [DataMember]
    public int Name { get; set; }
}

When I try this:

List<Father> f = result.ToList();

It calls Element 'Id' does not match any field or property of the class Model.Child

I think it just takes 'Id' as something else.

How can I deal with it? Thank you

7 Answers

You can resolve the issue by adding [BsonIgnoreExtraElements] on top of the class declaration. ObjectId is internally maintained by MongoDB which may not be needed unless you want to get additional information such as timestamp from the object. Hope this helps.

var conventionPack = new ConventionPack { new IgnoreExtraElementsConvention(true) };
        ConventionRegistry.Register("IgnoreExtraElements", conventionPack, type => true);

This works just perfectly! [BsonIgnoreExtraElements] also worked well, but, if you want to add any other ConventionRegistry like CamelCaseElementNameConvention, then, it overrides the Attribute one and the same exception occurs. Not sure if that could also be achieved using some other attribute.

I was using a dynamic list (List) to build a filter and was receiving a similar error. I added these lines to my data class to fix the problem.

[BsonId]
public ObjectId Id { get; set; }

this work for my case: add the attribute

    [DataMember]
    [BsonElement("songName")]

onto the elements:

[BsonIgnoreExtraElements]
public class Music
{
   
    [BsonId]
    [DataMember]
    public MongoDB.Bson.ObjectId _id { get; set; }
    [DataMember]
    public string Id { get; set; }

    [DataMember]
    [BsonElement("songName")]
    public string SongName { get; set; }

    [DataMember]
    [BsonElement("artistName")]
    public string ArtistName { get; set; }}

I faced to same problem.Same error occured at var data = query.ToList();

       var collection = db.GetCollection<Product>("Products");

        var query =
            from e in collection.AsQueryable<Product>()
            where e.name == "kalem"
            select e;

        var data = query.ToList();

and my insert was this:

    var collection = db.GetCollection<Product>("Products");

    collection.InsertBatch(products);

I solved as below.

        ObjectId id = new ObjectId();
        var collection = db.GetCollection<Product>("Products");

        collection.InsertBatch(products);
        id = pr.Id;

and I added id to Product class as below Product Class

class Product
{
    public ObjectId Id { get; set; }
    public string name { get; set; }
    public string category { get; set; }
    public double price { get; set; }
    public DateTime enterTime { get; set; }
}
Related