diff --git a/source/fundamentals/crud/read-operations/geo.txt b/source/fundamentals/crud/read-operations/geo.txt index a0f5f400e..2075b79df 100644 --- a/source/fundamentals/crud/read-operations/geo.txt +++ b/source/fundamentals/crud/read-operations/geo.txt @@ -85,6 +85,23 @@ To learn more about the shapes you can use in MongoDB, see the .. external resource +Insert a Document Containing GeoJSON Data +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +To insert a document that stores GeoJSON data, create a document that contains a GeoJSON +value and pass the document to the ``insertOne()`` method. + +The following example inserts a document that includes a ``location.geo`` field, +which contains GeoJSON data: + +.. literalinclude:: /includes/fundamentals/code-snippets/Geo.java + :language: java + :dedent: + :start-after: begin insertGeoJSONExample + :end-before: end insertGeoJSONExample + +To learn more about inserting documents, see the :ref:`java-fundamentals-insert` guide. + Index ~~~~~ @@ -118,6 +135,24 @@ Legacy coordinate pairs have the following structure: Your field should contain an array of two values in which the first represents the ``x`` axis value and the second represents the ``y`` axis value. +Insert a Document Containing Legacy Coordinates +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +To insert a document that stores a legacy coordinate pair, create a document that +assigns a coordinate pair value to a field. Then, pass the document to the +``insertOne()`` method. + +The following example inserts a document that includes a ``coordinates`` field, +which contains a legacy coordinate pair: + +.. literalinclude:: /includes/fundamentals/code-snippets/Geo.java + :language: java + :dedent: + :start-after: begin insertLegacyExample + :end-before: end insertLegacyExample + +To learn more about inserting documents, see the :ref:`java-fundamentals-insert` guide. + Index ~~~~~ diff --git a/source/includes/fundamentals/code-snippets/Geo.java b/source/includes/fundamentals/code-snippets/Geo.java index 8ac60c433..3e11a2d46 100644 --- a/source/includes/fundamentals/code-snippets/Geo.java +++ b/source/includes/fundamentals/code-snippets/Geo.java @@ -43,6 +43,30 @@ public void go() { } + private void insertGeoJSONExample() { + // begin insertGeoJSONExample + + // Add your MongoCollection setup code here + Point point = new Point(new Position(-74.0065, 40.7085)); + + Document theater = new Document("theaterId", 1203) + .append("location", new Document("geo", point)); + + InsertOneResult result = collection.insertOne(theater); + // end insertGeoJSONExample + } + + private void insertLegacyExample() { + // begin insertLegacyExample + + // Add your MongoCollection setup code here + Document theater = new Document("theaterId", 1204) + .append("coordinates", Arrays.asList(-73.9862, 40.7311)); + + InsertOneResult result = collection.insertOne(theater); + // end insertLegacyExample + } + private void nearExample() { // begin findExample