MongoDBカウント


単純にLoopしてカウントを取ってみたが2726583ミリ秒
40分以上かかる感じに。

検索条件の図形も制約ありそうだし
Can’t extract geo keys from object, malformed geometry?

まあ適当なコードだが、現状だと面扱うならSQL ServerかPostGISかな。
大量データ処理?

string siku = null;
Stopwatch spWatch = new Stopwatch();
spWatch.Start();

try
{
        
  MongoClient client = new MongoClient(); // connect to localhost
  MongoServer server = client.GetServer();
  MongoDatabase db = server.GetDatabase("test");

  var col = db.GetCollection<LazyBsonDocument>("cb");

  string[] txt = File.ReadAllLines(@"C:\Users\Administrator\Desktop\json.txt", Encoding.GetEncoding("SHIFT-JIS"));
  foreach (var line in txt)
  {
    try
    {
      dynamic geoJosn = DynamicJson.Parse(line);

      string json = geoJosn.Shape.ToString();

      IMongoQuery query = null;

      if (geoJosn.Shape.type == "Polygon")
      {
        var gon = BsonSerializer.Deserialize(json, typeof(GeoJsonPolygon<GeoJson2DCoordinates>)) as
        GeoJsonPolygon<GeoJson2DCoordinates>;

        query = Query.Within<GeoJson2DCoordinates>("SHAPE", gon);
      }
      else if (geoJosn.Shape.type == "MultiPolygon")
      {
        var gon = BsonSerializer.Deserialize(json, typeof(GeoJsonMultiPolygon<GeoJson2DCoordinates>)) as
        GeoJsonMultiPolygon<GeoJson2DCoordinates>;

        query = Query.GeoIntersects<GeoJson2DCoordinates>("SHAPE", gon);
      }

      siku = geoJosn.SIKUCHOSON;

      var cnt = col.Count(query);

      Console.WriteLine("{0}, {1}", siku, cnt);
    }
    catch (Exception ex)
    {
      Console.WriteLine("{0}  :::::::: {1} \n\r {2}" ,ex.Message, siku, ex.StackTrace);
    }
  }
}
catch (Exception ex)
{
  Console.WriteLine("{0}  :::::::: {1} \n\r {2}" ,ex.Message, siku, ex.StackTrace);
}
spWatch.Stop();
Console.WriteLine("{0} ミリ秒", spWatch.ElapsedMilliseconds);
カテゴリー: 開発, 設計 タグ: パーマリンク