@@ -434,7 +434,18 @@ public void TestNear()
434
434
var query = Query . Near ( "loc" , 1.1 , 2.2 ) ;
435
435
var selector = "{ '$near' : [1.1, 2.2] }" ;
436
436
Assert . AreEqual ( PositiveTest ( "loc" , selector ) , query . ToJson ( ) ) ;
437
- Assert . AreEqual ( NegativeTest ( "loc" , selector ) , Query . Not ( query ) . ToJson ( ) ) ;
437
+
438
+ var collection = Configuration . TestCollection ;
439
+ collection . Drop ( ) ;
440
+ collection . CreateIndex ( IndexKeys . GeoSpatial ( "loc" ) ) ;
441
+ collection . Insert ( new BsonDocument { { "_id" , 1 } , { "loc" , new BsonArray { 1 , 1 } } } ) ;
442
+ collection . Insert ( new BsonDocument { { "_id" , 2 } , { "loc" , new BsonArray { 2 , 2 } } } ) ;
443
+
444
+ query = Query . Near ( "loc" , 0.0 , 0.0 ) ;
445
+ var results = collection . Find ( query ) . ToList ( ) ;
446
+ Assert . AreEqual ( 2 , results . Count ) ;
447
+ Assert . AreEqual ( 1 , results [ 0 ] [ "_id" ] . ToInt32 ( ) ) ;
448
+ Assert . AreEqual ( 2 , results [ 1 ] [ "_id" ] . ToInt32 ( ) ) ;
438
449
}
439
450
440
451
[ Test ]
@@ -443,7 +454,17 @@ public void TestNearWithMaxDistance()
443
454
var query = Query . Near ( "loc" , 1.1 , 2.2 , 3.3 ) ;
444
455
var expected = "{ 'loc' : { '$near' : [1.1, 2.2], '$maxDistance' : 3.3 } }" . Replace ( "'" , "\" " ) ;
445
456
Assert . AreEqual ( expected , query . ToJson ( ) ) ;
446
- Assert . AreEqual ( NegateArbitraryQuery ( expected ) , Query . Not ( query ) . ToJson ( ) ) ;
457
+
458
+ var collection = Configuration . TestCollection ;
459
+ collection . Drop ( ) ;
460
+ collection . CreateIndex ( IndexKeys . GeoSpatial ( "loc" ) ) ;
461
+ collection . Insert ( new BsonDocument { { "_id" , 1 } , { "loc" , new BsonArray { 1 , 1 } } } ) ;
462
+ collection . Insert ( new BsonDocument { { "_id" , 2 } , { "loc" , new BsonArray { 2 , 2 } } } ) ;
463
+
464
+ query = Query . Near ( "loc" , 0.0 , 0.0 , 2.0 ) ;
465
+ var results = collection . Find ( query ) . ToList ( ) ;
466
+ Assert . AreEqual ( 1 , results . Count ) ;
467
+ Assert . AreEqual ( 1 , results [ 0 ] [ "_id" ] . ToInt32 ( ) ) ;
447
468
}
448
469
449
470
[ Test ]
@@ -452,7 +473,18 @@ public void TestNearWithSphericalTrue()
452
473
var query = Query . Near ( "loc" , 1.1 , 2.2 , 3.3 , true ) ;
453
474
var expected = "{ 'loc' : { '$nearSphere' : [1.1, 2.2], '$maxDistance' : 3.3 } }" . Replace ( "'" , "\" " ) ;
454
475
Assert . AreEqual ( expected , query . ToJson ( ) ) ;
455
- Assert . AreEqual ( NegateArbitraryQuery ( expected ) , Query . Not ( query ) . ToJson ( ) ) ;
476
+
477
+ var collection = Configuration . TestCollection ;
478
+ collection . Drop ( ) ;
479
+ collection . CreateIndex ( IndexKeys . GeoSpatial ( "loc" ) ) ;
480
+ collection . Insert ( new BsonDocument { { "_id" , 1 } , { "loc" , new BsonArray { 1 , 1 } } } ) ;
481
+ collection . Insert ( new BsonDocument { { "_id" , 2 } , { "loc" , new BsonArray { 2 , 2 } } } ) ;
482
+
483
+ var radiansPerDegree = 2 * Math . PI / 360.0 ;
484
+ query = Query . Near ( "loc" , 0.0 , 0.0 , 2.0 * radiansPerDegree , true ) ;
485
+ var results = collection . Find ( query ) . ToList ( ) ;
486
+ Assert . AreEqual ( 1 , results . Count ) ;
487
+ Assert . AreEqual ( 1 , results [ 0 ] [ "_id" ] . ToInt32 ( ) ) ;
456
488
}
457
489
458
490
[ Test ]
@@ -462,27 +494,62 @@ public void TestNearWithGeoJson()
462
494
var query = Query . Near ( "loc" , point ) ;
463
495
var selector = "{ '$near' : { '$geometry' : { 'type' : 'Point', 'coordinates' : [40.0, 18.0] } } }" ;
464
496
Assert . AreEqual ( PositiveTest ( "loc" , selector ) , query . ToJson ( ) ) ;
465
- Assert . AreEqual ( NegativeTest ( "loc" , selector ) , Query . Not ( query ) . ToJson ( ) ) ;
497
+
498
+ var collection = Configuration . TestCollection ;
499
+ collection . Drop ( ) ;
500
+ collection . CreateIndex ( IndexKeys . GeoSpatial ( "loc" ) ) ;
501
+ collection . Insert ( new BsonDocument { { "_id" , 1 } , { "loc" , new BsonArray { 1 , 1 } } } ) ;
502
+ collection . Insert ( new BsonDocument { { "_id" , 2 } , { "loc" , new BsonArray { 2 , 2 } } } ) ;
503
+
504
+ query = Query . Near ( "loc" , 0.0 , 0.0 ) ;
505
+ var results = collection . Find ( query ) . ToList ( ) ;
506
+ Assert . AreEqual ( 2 , results . Count ) ;
507
+ Assert . AreEqual ( 1 , results [ 0 ] [ "_id" ] . ToInt32 ( ) ) ;
508
+ Assert . AreEqual ( 2 , results [ 1 ] [ "_id" ] . ToInt32 ( ) ) ;
466
509
}
467
510
468
511
[ Test ]
469
512
public void TestNearWithGeoJsonWithMaxDistance ( )
470
513
{
471
514
var point = GeoJson . Point ( GeoJson . Geographic ( 40 , 18 ) ) ;
472
515
var query = Query . Near ( "loc" , point , 42 ) ;
473
- var selector = "{ '$near' : { '$geometry' : { 'type' : 'Point', 'coordinates' : [40.0, 18.0] }, '$maxDistance' : 42.0 } }" . Replace ( "'" , "\" " ) ;
474
- Assert . AreEqual ( PositiveTest ( "loc" , selector ) , query . ToJson ( ) ) ;
475
- Assert . AreEqual ( NegativeTest ( "loc" , selector ) , Query . Not ( query ) . ToJson ( ) ) ;
516
+ var expected = "{ 'loc' : { '$near' : { '$geometry' : { 'type' : 'Point', 'coordinates' : [40.0, 18.0] }, '$maxDistance' : 42.0 } } }" . Replace ( "'" , "\" " ) ;
517
+ Assert . AreEqual ( expected , query . ToJson ( ) ) ;
518
+
519
+ var collection = Configuration . TestCollection ;
520
+ collection . Drop ( ) ;
521
+ collection . CreateIndex ( IndexKeys . GeoSpatialSpherical ( "loc" ) ) ;
522
+ collection . Insert ( new BsonDocument { { "_id" , 1 } , { "loc" , GeoJson . Point ( GeoJson . Geographic ( 1 , 1 ) ) . ToBsonDocument ( ) } } ) ;
523
+ collection . Insert ( new BsonDocument { { "_id" , 2 } , { "loc" , GeoJson . Point ( GeoJson . Geographic ( 2 , 2 ) ) . ToBsonDocument ( ) } } ) ;
524
+
525
+ var circumferenceOfTheEarth = 40075000 ; // meters at the equator, approx
526
+ var metersPerDegree = circumferenceOfTheEarth / 360.0 ;
527
+ query = Query . Near ( "loc" , GeoJson . Point ( GeoJson . Geographic ( 0 , 0 ) ) , 2.0 * metersPerDegree ) ;
528
+ var results = collection . Find ( query ) . ToList ( ) ;
529
+ Assert . AreEqual ( 1 , results . Count ) ;
530
+ Assert . AreEqual ( 1 , results [ 0 ] [ "_id" ] . ToInt32 ( ) ) ;
476
531
}
477
532
478
533
[ Test ]
479
534
public void TestNearWithGeoJsonWithSpherical ( )
480
535
{
481
536
var point = GeoJson . Point ( GeoJson . Geographic ( 40 , 18 ) ) ;
482
537
var query = Query . Near ( "loc" , point , 42 , true ) ;
483
- var selector = "{ '$nearSphere' : { '$geometry' : { 'type' : 'Point', 'coordinates' : [40.0, 18.0] }, '$maxDistance' : 42.0 } }" . Replace ( "'" , "\" " ) ;
484
- Assert . AreEqual ( PositiveTest ( "loc" , selector ) , query . ToJson ( ) ) ;
485
- Assert . AreEqual ( NegativeTest ( "loc" , selector ) , Query . Not ( query ) . ToJson ( ) ) ;
538
+ var expected = "{ 'loc' : { '$nearSphere' : { '$geometry' : { 'type' : 'Point', 'coordinates' : [40.0, 18.0] }, '$maxDistance' : 42.0 } } }" . Replace ( "'" , "\" " ) ;
539
+ Assert . AreEqual ( expected , query . ToJson ( ) ) ;
540
+
541
+ var collection = Configuration . TestCollection ;
542
+ collection . Drop ( ) ;
543
+ collection . CreateIndex ( IndexKeys . GeoSpatialSpherical ( "loc" ) ) ;
544
+ collection . Insert ( new BsonDocument { { "_id" , 1 } , { "loc" , GeoJson . Point ( GeoJson . Geographic ( 1 , 1 ) ) . ToBsonDocument ( ) } } ) ;
545
+ collection . Insert ( new BsonDocument { { "_id" , 2 } , { "loc" , GeoJson . Point ( GeoJson . Geographic ( 2 , 2 ) ) . ToBsonDocument ( ) } } ) ;
546
+
547
+ var circumferenceOfTheEarth = 40075000 ; // meters at the equator, approx
548
+ var metersPerDegree = circumferenceOfTheEarth / 360.0 ;
549
+ query = Query . Near ( "loc" , GeoJson . Point ( GeoJson . Geographic ( 0 , 0 ) ) , 2.0 * metersPerDegree , true ) ;
550
+ var results = collection . Find ( query ) . ToList ( ) ;
551
+ Assert . AreEqual ( 1 , results . Count ) ;
552
+ Assert . AreEqual ( 1 , results [ 0 ] [ "_id" ] . ToInt32 ( ) ) ;
486
553
}
487
554
488
555
[ Test ]
0 commit comments