Skip to content

Commit 8d1fc76

Browse files
authored
DOCSP-49745: C# example for $geoNear (#12328) (#12350)
* DOCSP-49745: C# example for geoNear * wip * fix formatting * MM small PR fixes
1 parent 6a93bc8 commit 8d1fc76

File tree

4 files changed

+423
-289
lines changed

4 files changed

+423
-289
lines changed

content/manual/manual/source/includes/driver-examples/aggregation/BuildersExamples.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,36 @@ public static void FacetStage()
154154
.Facet(bucketFacet, countFacet);
155155
// end facet
156156
}
157-
157+
158+
public static void GeoNearStage()
159+
{
160+
// start geoNear
161+
var pipeline = new EmptyPipelineDefinition<Theater>()
162+
.GeoNear(
163+
GeoJson.Point(GeoJson.Geographic(-74.1, 40.95)),
164+
new GeoNearOptions<Theater, Theater>
165+
{
166+
DistanceField = "distance",
167+
MaxDistance = 8000,
168+
Key = "location.geo",
169+
Query = Builders<Theater>.Filter.Eq(t => t.Location.Address.State, "NJ"),
170+
});
171+
// end geoNear
172+
173+
// start geoNear min
174+
var pipeline = new EmptyPipelineDefinition<Theater>()
175+
.GeoNear(
176+
GeoJson.Point(GeoJson.Geographic(-74.1, 40.95)),
177+
new GeoNearOptions<Theater, Theater>
178+
{
179+
DistanceField = "distance",
180+
MinDistance = 8000,
181+
Key = "location.geo",
182+
Query = Builders<Theater>.Filter.Eq(t => t.Location.Address.State, "NJ"),
183+
})
184+
.Limit(4);
185+
// end geoNear min
186+
}
158187
public static void GraphLookupStage()
159188
{
160189
// start graphLookupBasic
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public class Theater
2+
{
3+
public ObjectId Id { get; set; }
4+
5+
[BsonElement("theaterId")]
6+
public int TheaterId { get; set; }
7+
8+
[BsonElement("location")]
9+
public Location Location { get; set; }
10+
11+
[BsonElement("distance")]
12+
public double? Distance { get; set; }
13+
}
14+
15+
public class Location
16+
{
17+
[BsonElement("address")]
18+
public Address Address { get; set; }
19+
20+
[BsonElement("geo")]
21+
public GeoJsonPoint<GeoJson2DGeographicCoordinates> Geo { get; set; }
22+
}
23+
24+
[BsonIgnoreExtraElements]
25+
public class Address
26+
{
27+
[BsonElement("city")]
28+
public string City { get; set; }
29+
30+
[BsonElement("state")]
31+
public string State { get; set; }
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
The C# examples on this page use the ``sample_mflix.theaters`` collection
2+
from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a
3+
free MongoDB Atlas cluster and load the sample datasets, see
4+
:driver:`Get Started </csharp/current/quick-start/>` in the MongoDB .NET/C#
5+
Driver documentation.
6+
7+
The following ``Theater``, ``Location``, and ``Address`` classes model
8+
the documents in the ``sample_mflix.theaters`` collection:
9+
10+
.. literalinclude:: /includes/driver-examples/aggregation/Theater.cs
11+
:language: csharp

0 commit comments

Comments
 (0)