C# MongoDB API存储GeoJSON Geometry至类的问题求助
Troubleshooting Your MongoDB API Issues with Polygon Storage and GeoWithin Queries
Hey there! Let's tackle your two MongoDB API problems head-on—since your Restaurant class is working smoothly, we can use that as a reference to fix the Neighborhood class hiccups.
1. Fixing Polygon Coordinate Storage in the Neighborhood Class
The most common issues here are mismatched field types or missing geospatial indexing. Here's what to check:
- Use the Correct GeoJSON Type: Don't store polygons as plain lists of coordinates. Instead, use your MongoDB driver's built-in GeoJSON polygon type (like
GeoJsonPolygonin Java/.NET, ormongoose.Schema.Types.Polygonin Mongoose). This ensures MongoDB recognizes the data as a valid geospatial object.
Example Java class snippet:public class Neighborhood { private String neighborhoodName; // Add the 2dsphere index annotation to enable geospatial queries @GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE) private GeoJsonPolygon boundary; // Constructor, getters, setters } - Properly Construct the Polygon: When creating a polygon, make sure the coordinate list is closed (the first and last points must be identical) and follows MongoDB's
[longitude, latitude]order (this is a super common mix-up!).
Example of building a valid polygon:// Create a closed list of points (long, lat) List<GeoJsonPoint> polygonPoints = Arrays.asList( new GeoJsonPoint(-73.9857, 40.7484), new GeoJsonPoint(-73.9855, 40.7482), new GeoJsonPoint(-73.9850, 40.7486), new GeoJsonPoint(-73.9857, 40.7484) // Close the polygon ); GeoJsonPolygon neighborhoodBoundary = new GeoJsonPolygon(Arrays.asList(polygonPoints)); Neighborhood myNeighborhood = new Neighborhood("Upper East Side", neighborhoodBoundary); // Save to MongoDB mongoTemplate.save(myNeighborhood); - Verify Index Creation: If you're not using driver annotations, manually create the 2dsphere index in the shell:
db.neighborhoods.createIndex({ boundary: "2dsphere" })
2. Getting the GeoWithin Function to Work
GeoWithin relies on proper indexing and correct query formatting. Let's fix this:
- Ensure the 2dsphere Index Exists: Without this index, geospatial queries like GeoWithin will fail. Double-check the index exists using
db.neighborhoods.getIndexes()in the shell. - Build a Valid GeoWithin Query: Use the driver's query builders to construct the query with a valid GeoJSON polygon. Here's a Java example:
// Define the area you want to query (a polygon) GeoJsonPolygon queryArea = new GeoJsonPolygon(Arrays.asList( new GeoJsonPoint(-74.01, 40.70), new GeoJsonPoint(-73.99, 40.70), new GeoJsonPoint(-73.99, 40.72), new GeoJsonPoint(-74.01, 40.72), new GeoJsonPoint(-74.01, 40.70) // Close the query polygon )); // Create the query using GeoWithin Query query = new Query(Criteria.where("boundary").within(queryArea)); List<Neighborhood> results = mongoTemplate.find(query, Neighborhood.class); - Check Coordinate Order: Again, make sure you're using
[longitude, latitude]in both the stored polygon and the query polygon. Swapping these will return incorrect results or no matches at all. - Test in the Shell First: If your code query isn't working, test the equivalent shell command to rule out driver-specific issues:
db.neighborhoods.find({ boundary: { $geoWithin: { $geometry: { type: "Polygon", coordinates: [ [ [-74.01, 40.70], [-73.99, 40.70], [-73.99, 40.72], [-74.01, 40.72], [-74.01, 40.70] ] ] } } } })
Quick Additional Checks
- Compare with Restaurant Class: Since your Restaurant class works, look for differences in how you're defining fields, adding indexes, or saving data. The discrepancy is likely there.
- Check MongoDB Logs: If you're still stuck, check MongoDB's log files for error messages related to geospatial storage or queries—they'll often point you directly to the issue.
- Validate Documents with Compass: Use MongoDB Compass to inspect the Neighborhood collection. Ensure the
boundaryfield is stored as aPolygontype (not a plain array) with valid GeoJSON structure.
内容的提问来源于stack exchange,提问作者Cannass
相关产品推荐
相关产品推荐

