如何通过 java 在 mongoDB 集合中插入 GeoJson 对象作为原型 $geometry

How to insert GeoJson object as prototype $geometry in mongoDB collection through java

目前我正在使用 java 创建 mongoDB 数据库,但我无法在集合中插入地理位置。我的导师告诉我使用 GeoJSON 对象作为 $geometry 原型来插入 Geo Location。我的意思是,我想按以下原型插入数据 -

$geometry: {
   type: "Polygon",
   coordinates: [ <coordinates> ],
   crs: {
      type: "name",
      properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
   }
}

我这样做了 -

double lat_lng_values[] = {144.6682361, -37.8978304};
List<BasicDBObject> loc = new ArrayList();
BasicDBObject obj = new  BasicDBObject();
obj.put("location",lat_lng_values);
PInfo.insert(obj);    //(Here Pinfo is DBCollection Object)

请帮助我如何使用 $geometry 原型。 先感谢您。对不起我的英语。

您的原型没有传达给 GeoJson 标准,您应该遵循以下架构:

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  },
  "properties": {
    "name": "Dinagat Islands"
  }
} 

因此,如果您有任何属性,例如 'crs',它应该包含在 GeoJson 模式的属性部分中。关于在 mangoDB 中插入 GeoJson 文档,我不是这方面的专家,但你可以使用类似的东西:

    Mongo mongo = new Mongo('your paramaters');
    DB db = mongo.getDB("yourdb");

    DBCollection collection = db.getCollection("dummyColl");
    String json = "{
        'type': 'Feature',
        'geometry': {
          'type': 'Point',
          'coordinates': [125.6, 10.1]
        },
       'properties': {
          'name': 'Dinagat Islands'
       }
   }";

    DBObject dbObject = (DBObject)JSON.parse(json);

    collection.insert(dbObject);