简体   繁体   English

如何为不属于我的 MongoDB 集合创建一个类?

[英]How to create a class for a MongoDB collection that is not mine?

I'm new to MongoDB and having some issues.我是 MongoDB 的新手,遇到了一些问题。 Following this easy guide here ( http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-csharp-driver ) but I'm instead connecting to a database that I did not personally create.遵循这里的这个简单指南( http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-csharp-driver ),但我改为连接到我没有亲自创建的数据库。 So when the examples include something like:因此,当示例包括以下内容时:

var collection = database.GetCollection<Entity>("entities");

it doesn't work for the actual database i'm connecting to because I don't have the class.它不适用于我要连接的实际数据库,因为我没有课程。 it's full of types that I can't just guess.. for example:它充满了我无法猜测的类型……例如:

/* 0 */
{
  "_id" : ObjectId("54f0f990cea606d49fafd5h2"),
  "Time" : ISODate("2015-02-27T23:11:12.301Z"),
  "Timecode" : "15:11:12:18",
  "Round" : "1",
  "FighterID" : 5,
  "FighterName" : "John Doe",
  "TypeID" : 1,
  "Type" : "jab",
  "HandID" : 2,
  "Hand" : "left",
  "Force" : 0.0,
  "Velocity" : 8.0789527768068456,
  "Confidence" : 67.689217510307827,
  "ImpactType" : 1,
  "FightID" : "fea5dc60-b898-11e4-ac68-a5f571ea05d9"
}

i don't know how to make a class for that since the class members are rather complicated.我不知道如何为此创建一个类,因为类成员相当复杂。

how do i go about doing this?我该怎么做?

I am not aware of away to create the class out of mongodb just as we have with EF and linq-to-sql.我不知道要像使用 EF 和 linq-to-sql 一样从 mongodb 中创建类。

I suggest you do one of two things, and i believe the second solution will harder but closer to what you asking我建议你做两件事之一,我相信第二个解决方案会更难但更接近你的要求

1) Try use bsonextractelements attribute to catch all complicated fields 1) 尝试使用 bsonextractelements 属性来捕获所有复杂的字段

   public MyClass {
    // fields and properties
    [BsonExtraElements]
    public BsonDocument CatchAll { get; set; }
}

Or, 2)use reflection and store the name of properties as well as the data type of each property to txt file maybe.或者,2)使用反射并将属性名称以及每个属性的数据类型存储到txt文件中。 Then create your class manullay然后创建你的班级 manullay

Quite a late answer to the question, but I've been using this website to generate the C# classes for me.这个问题的答案很晚,但我一直在使用这个网站为我生成 C# 类。 It works by parsing JSON data provided to it and generating the relevant class(es) that can be used to deserialise said Json.它的工作原理是解析提供给它的 JSON 数据并生成可用于反序列化所述 Json 的相关类。

It has a couple of hangups when it comes to dealing with json taken from MongoDB:在处理取自 MongoDB 的 json 时,它有几个问题:

  • It doesn't handle "ObjectId" and you'll get an initial parse error when you hit generate if you just use the Json pulled from Mongo.它不处理“ObjectId”,如果您只使用从 Mongo 中提取的 Json,当您点击 generate 时,您将收到初始解析错误。 However, you can fix that easily by removing the "ObjectId" prefix (and accompanying parenthesis).但是,您可以通过删除“ObjectId”前缀(以及随附的括号)轻松解决该问题。

  • It doesn't handle Json field names with spaces in it all that well, but it still generates a class for you that you can quickly "fix" any spaced fields它不能很好地处理带有空格的 Json 字段名称,但它仍然为您生成一个类,您可以快速“修复”任何空格的字段

  • If you have a collection of Json that has fluctuating amount of fields within it (IE, some documents includes "Diety" field, but some don't), then you need to paste in an array of json objects to ensure the generated class(es) cover all combinations possible如果您有一个 Json 集合,其中包含的字段数量波动(IE,有些文档包含“Diety”字段,但有些不包含),那么您需要粘贴一组 json 对象以确保生成的类( es) 涵盖所有可能的组合

  • As with the above, if you've got some fields that can be null, unless you include Json that illustrates this, the parser can't know that it should accept nulls, so make sure you either include enough examples to cover all combinations, or be prepared to make a few changes to the generated code yourself (like sticking a question mark after a few field types ;))与上面一样,如果您有一些可以为空的字段,除非您包含说明这一点的 Json,否则解析器无法知道它应该接受空值,因此请确保您包含足够的示例以涵盖所有组合,或者准备自己对生成的代码进行一些更改(例如在几个字段类型后贴一个问号;))

As I say - very late answer, but this question is coming up in Google quite a bit, so thought I'd add this in the hopes it helps someone else later on正如我所说 - 很晚的答案,但这个问题在谷歌中出现了很多,所以我想我会添加这个希望它以后可以帮助其他人

Late answer, but maybe will help someone.迟到的答案,但也许会帮助某人。 Skipped few properties.跳过了几个属性。

public class Entity
{
    [BsonId]
    public MongoDB.Bson.ObjectId _id { get; set; }

    [BsonElement("id")]
    public int Id { get; set; }

    [BsonElement("Time")]
    public DateTime Time{ get; set; }

    [BsonElement("Round")]
    public string Round{ get; set; }

    [BsonElement("Force")]
    public double? Force{ get; set; }

    [BsonElement("FightID")]
    public string FightID{ get; set; }
}

Even later to the party but (inspired by Sk93's answer) I've found this website ( http://bson2csharp.com/ ) that knows about MongoDB BSON.甚至后来参加聚会,但(受 Sk93 的回答启发)我发现这个网站http://bson2csharp.com/ )了解 MongoDB BSON。

UPDATE: website no longer exists更新:网站不再存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM