Skip to content

Commit fd068c9

Browse files
authored
DOCSP-38823: Insert docs with geospatial data (#545)
* DOCSP-38823: Insert docs with geospatial data * MW feedback
1 parent 856ef41 commit fd068c9

File tree

2 files changed

+59
-0
lines changed
  • source
    • fundamentals/crud/read-operations
    • includes/fundamentals/code-snippets

2 files changed

+59
-0
lines changed

source/fundamentals/crud/read-operations/geo.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,23 @@ To learn more about the shapes you can use in MongoDB, see the
8585

8686
.. external resource
8787

88+
Insert a Document Containing GeoJSON Data
89+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
90+
91+
To insert a document that stores GeoJSON data, create a document that contains a GeoJSON
92+
value and pass the document to the ``insertOne()`` method.
93+
94+
The following example inserts a document that includes a ``location.geo`` field,
95+
which contains GeoJSON data:
96+
97+
.. literalinclude:: /includes/fundamentals/code-snippets/Geo.java
98+
:language: java
99+
:dedent:
100+
:start-after: begin insertGeoJSONExample
101+
:end-before: end insertGeoJSONExample
102+
103+
To learn more about inserting documents, see the :ref:`java-fundamentals-insert` guide.
104+
88105
Index
89106
~~~~~
90107

@@ -118,6 +135,24 @@ Legacy coordinate pairs have the following structure:
118135
Your field should contain an array of two values in which the first represents
119136
the ``x`` axis value and the second represents the ``y`` axis value.
120137

138+
Insert a Document Containing Legacy Coordinates
139+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
140+
141+
To insert a document that stores a legacy coordinate pair, create a document that
142+
assigns a coordinate pair value to a field. Then, pass the document to the
143+
``insertOne()`` method.
144+
145+
The following example inserts a document that includes a ``coordinates`` field,
146+
which contains a legacy coordinate pair:
147+
148+
.. literalinclude:: /includes/fundamentals/code-snippets/Geo.java
149+
:language: java
150+
:dedent:
151+
:start-after: begin insertLegacyExample
152+
:end-before: end insertLegacyExample
153+
154+
To learn more about inserting documents, see the :ref:`java-fundamentals-insert` guide.
155+
121156
Index
122157
~~~~~
123158

source/includes/fundamentals/code-snippets/Geo.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ public void go() {
4343

4444
}
4545

46+
private void insertGeoJSONExample() {
47+
// begin insertGeoJSONExample
48+
49+
// Add your MongoCollection setup code here
50+
Point point = new Point(new Position(-74.0065, 40.7085));
51+
52+
Document theater = new Document("theaterId", 1203)
53+
.append("location", new Document("geo", point));
54+
55+
InsertOneResult result = collection.insertOne(theater);
56+
// end insertGeoJSONExample
57+
}
58+
59+
private void insertLegacyExample() {
60+
// begin insertLegacyExample
61+
62+
// Add your MongoCollection setup code here
63+
Document theater = new Document("theaterId", 1204)
64+
.append("coordinates", Arrays.asList(-73.9862, 40.7311));
65+
66+
InsertOneResult result = collection.insertOne(theater);
67+
// end insertLegacyExample
68+
}
69+
4670
private void nearExample() {
4771
// begin findExample
4872

0 commit comments

Comments
 (0)