mongodb 元素“Id”与类的任何字段或属性都不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11557912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Element 'Id' does not match any field or property of class
提问by EasonBlack
I got the result from the collection in MongoDB, the structure is the same as below
我从MongoDB中的集合中得到结果,结构如下
[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
它调用 Element 'Id' does not match any field or property of the class Model.Child
I think it just takes 'Id' as something else.
我认为它只是将“Id”作为其他东西。
How can I deal with it? Thank you
我该如何处理?谢谢
回答by Naga
I resolved similar issue by adding [BsonIgnoreExtraElements]on top of the class declaration. ObjectId is internally maintained by MongoDB which is not needed at the App level, hence in the class declaration I don't require this field. Hope this would help someone
我通过在类声明的顶部添加[BsonIgnoreExtraElements]解决了类似的问题。ObjectId 由 MongoDB 内部维护,在应用程序级别不需要它,因此在类声明中我不需要这个字段。希望这会帮助某人
回答by Abhilash Pokhriyal
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.
这工作得很好![BsonIgnoreExtraElements] 也运行良好,但是,如果您想添加任何其他 ConventionRegistry,例如 CamelCaseElementNameConvention,那么它会覆盖 Attribute one,并且会发生相同的异常。不确定是否也可以使用其他一些属性来实现。
回答by Tom Glenn
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.
我正在使用动态列表 (List) 来构建过滤器并收到类似的错误。我将这些行添加到我的数据类中以解决问题。
[BsonId]
public ObjectId Id { get; set; }
回答by Mehmet Top?u
I faced to same problem.Same error occured at var data = query.ToList();
我遇到了同样的问题。同样的错误发生在 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
我添加id
到产品类如下产品类
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; }
}
回答by Yook Feng
this work for my case: add label
这适用于我的情况:添加标签
[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; }}
回答by Nuk Nuk San
Your Child class should probably specify it inherits Father
您的 Child 类可能应该指定它继承父亲
public class Child: Father { ... }
公共类孩子:父亲{...}
Your Father class should probably add known type attribute (for WCF ).
您的父亲类可能应该添加已知类型属性(对于 WCF )。
[DataContract]
[KnownType(typeof(Child))]
public class Father
If this is a MongoCollection("fathers") that you save / fetch through, then you may need to register a class map for each expected child type.
如果这是您保存/获取的 MongoCollection("fathers"),那么您可能需要为每个预期的子类型注册一个类映射。
if (!BsonClassMap.IsClassMapRegistered(typeof(Child)))
{
BsonClassMap.RegisterClassMap<Child>(
cm => { cm.AutoMap(); });
}
As @alexjamesbrown mentioned, you are not required to name the id field on your poco object '_id'. The idea with inheritance is to inherit. So using the Father's "id" field (whatever it is named) should suffice. It's not clear why your Father class has both Id and _id properties. One of them is probably not necessary.
正如@alexjamesbrown 所提到的,您不需要将 poco 对象上的 id 字段命名为“_id”。继承的想法是继承。所以使用父亲的“id”字段(无论它叫什么名字)就足够了。不清楚为什么您的父亲类同时具有 Id 和 _id 属性。其中之一可能不是必需的。
回答by Abdelhalim FELLAGUE CHEBRA
all you have to do is removing the [DataMember] on the ObjecId attribute and bind the Id to the ObjectId _id.
您所要做的就是删除 ObjecId 属性上的 [DataMember] 并将 Id 绑定到 ObjectId _id。
so your class should look like this :
所以你的班级应该是这样的:
[DataContract]
public class Father
{
[BsonId]
public MongoDB.Bson.ObjectId _id { get; set; }
[DataMember]
public string Id {
get { return _id.ToString(); }
set { _id = ObjectId.Parse(value); }
}
[DataMember]
public List<Child> childs { get; set; }
}
ps : in your case the child id must be generated manually, if you want it to be an objectId(mongo), you will have do the same trick finally, to deserialized the object, you should use the newtonsoft.json reference and do like this
ps:在您的情况下,必须手动生成子ID,如果您希望它成为objectId(mongo),您最终将执行相同的技巧,反序列化对象,您应该使用newtonsoft.json引用并做这个
Father = JsonConvert.DeserializeObject<Father>(response.Content);