Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions source/fundamentals/crud/read-operations/geo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~

Expand Down Expand Up @@ -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
~~~~~

Expand Down
24 changes: 24 additions & 0 deletions source/includes/fundamentals/code-snippets/Geo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down