Oto dokument JSON gdzie Mongo dostawcy LINQ nie:Dziwne zachowanie dostawcy MongoDB LINQ dla pól o nazwie "id"
{"results":
{"text":"@twitterapi http://tinyurl.com/ctrefg",
"to_user_id":396524,
"to_user":"TwitterAPI",
"from_user":"jkoum",
"metadata":
{
"result_type":"popular",
"recent_retweets": 109
},
"id":1478555574,
"from_user_id":1833773,
"iso_language_code":"nl",
"source":"<a href=\"http://twitter.com/\">twitter<\/a>",
"profile_image_url":"http://s3.amazonaws.com/twitter_production/profile_images/118412707/2522215727_a5f07da155_b_normal.jpg",
"created_at":"Wed, 08 Apr 2009 19:22:10 +0000",
"since_id":0,
"max_id":1480307926,
"refresh_url":"?since_id=1480307926&q=%40twitterapi",
"results_per_page":15,
"next_page":"?page=2&max_id=1480307926&q=%40twitterapi",
"completed_in":0.031704,
"page":1,
"query":"%40twitterapi"}
}
Uwaga "id" pole. Tutaj są związane C# definicje jednostki:
class Twitter
{
[BsonId]
public ObjectId Id { get; set; }
public Result results { get; set; }
}
private class Result
{
public string text { get; set; }
public int to_user_id { get; set; }
public string to_user { get; set; }
public string from_user { get; set; }
public Metadata metadata { get; set; }
public int id { get; set; }
public int from_user_id { get; set; }
public string iso_language_code { get; set; }
public string source { get; set; }
public string profile_image_url { get; set; }
public string created_at { get; set; }
public int since_id { get; set; }
public int max_id { get; set; }
public string refresh_url { get; set; }
public int results_per_page { get; set; }
public string next_page { get; set; }
public double completed_in { get; set; }
public int page { get; set; }
public string query { get; set; }
}
class Metadata
{
public string result_type { get; set; }
public int recent_retweets { get; set; }
}
Jeśli tworzę „Twitter” zbieranie i zapisać dokument powyżej, a następnie, gdy kwerendy go przy użyciu dostawcy Mongo LINQ, rzuca wyjątek FileFormatException: „«id»elementu nie znaleziono żadnych pole lub właściwość Mongo.Context.Tests.NativeTests klasy + Wynik”
Jednakże istnieją dwie alternatywne obejścia aby rozwiązać ten problem:
- Rename Wynik pola«iD», np do "idd" zarówno w dokumencie JSON doc, jak i klasie Result. Wtedy działa zapytanie LINQ.
- Zachowaj pole "id", ale dodatkowo dodaj pole "Id" do klasy Wynik i oznacz ją atrybutem [BsonId]. Teraz klasa Result zawiera pola "Id" i "id", ale zapytanie działa!
Używam API Mongo do sprawdzania kolekcji, wszystko działa dobrze, więc myślę, że to musi być błąd w dostawcy MongoDB LINQ. "id" w zagnieżdżonym elemencie JSON nie powinno być zarezerwowanym dziełem, czyż nie?
UPDATE: Oto wynik native wykonania kwerendy API:
> db.Twitter.find().limit(1);
{ "_id" : ObjectId("50c9d3a4870f4f17e049332b"),
"results" : {
"text" : "@twitterapi http://tinyurl.com/ctrefg",
"to_user_id" : 396524,
"to_user" : "TwitterAPI",
"from_user" : "jkoum",
"metadata" : { "result_type" : "popular", "recent_retweets" : 109 },
"id" : 1478555574,
"from_user_id" : 1833773, "
iso_language_code" : "nl",
"source" : "<a href=\"http://twitter.com/\">twitter</a>",
"profile_image_url" : "http://s3.amazonaws.com/twitter_production/profile_images/118412707/2522215727_a5f07da155_b_normal.jpg",
"created_at" : "Wed, 08 Apr 2009 19:22:10 +0000",
"since_id" : 0,
"max_id" : 1480307926,
"refresh_url" : "?since_id=1480307926&q=%40twitterapi", "results_per_page" : 15, "next_page" : "?page=2&max_id=1480307926&q=%40twitterapi",
"completed_in" : 0.031704,
"page" : 1,
"query" : "%40twitterapi"
}
}
+1 za obejście - dzięki. Co ciekawe, wygląda na to, że błąd powstaje tylko wtedy, gdy obiekt zawierający pojedynczy obiekt ma pole "id". Element podrzędny 'List' z' id' nie miał problemów. – StuartLC