Guest User

Untitled

a guest
Jan 22nd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using MongoDB.Bson;
  7. using MongoDB.Bson.IO;
  8. using MongoDB.Driver;
  9.  
  10. namespace TestGeospaticalHaystackIndexing {
  11. public static class Program {
  12. public static void Main(string[] args) {
  13. try {
  14. var server = MongoServer.Create("mongodb://localhost/?safe=true");
  15. var database = server["test"];
  16. var foo = database["foo"];
  17. foo.Drop();
  18.  
  19. foo.Insert(new BsonDocument {
  20. { "pos", new BsonDocument { { "long", 34.2 }, { "lat", 33.3 } } },
  21. { "type", "restaurant" }
  22. });
  23. foo.Insert(new BsonDocument {
  24. { "pos", new BsonDocument { { "long", 34.2 }, { "lat", 37.3 } } },
  25. { "type", "restaurant" }
  26. });
  27. foo.Insert(new BsonDocument {
  28. { "pos", new BsonDocument { { "long", 59.1 }, { "lat", 87.2 } } },
  29. { "type", "office" }
  30. });
  31.  
  32. var keys = new IndexKeysDocument {
  33. { "pos", "geoHaystack" },
  34. { "type", 1 }
  35. };
  36. var options = new IndexOptionsDocument {
  37. { "bucketSize", 1 }
  38. };
  39. foo.EnsureIndex(keys, options);
  40.  
  41. var command = new CommandDocument {
  42. { "geoSearch", "foo" },
  43. { "near", new BsonArray { 33, 33 } },
  44. { "maxDistance", 6 },
  45. { "search", new BsonDocument { { "type", "restaurant" } } },
  46. { "limit", 30 }
  47. };
  48. var result = database.RunCommand(command);
  49. Console.WriteLine(result.Response.ToJson(new JsonWriterSettings { Indent = true }));
  50. } catch (Exception ex) {
  51. Console.WriteLine("Unhandled exception:");
  52. Console.WriteLine(ex);
  53. }
  54.  
  55. Console.WriteLine("Press Enter to continue");
  56. Console.ReadLine();
  57. }
  58. }
  59. }
Add Comment
Please, Sign In to add comment