Skip to content

Commit

Permalink
DOCSP-34191: kotlin insert documents code (#6289)
Browse files Browse the repository at this point in the history
* DOCSP-34191: kotlin insert examples

* staging test

* fix tab ids

* fix tab ids

* fix tab ids

* staging test

* dedent settings

* add back default domain

* staging
  • Loading branch information
rustagir authored Feb 21, 2024
1 parent 05a1ac9 commit 7ab31f5
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,8 @@ examples:
curl -SfL https://raw.githubusercontent.com/mongodb/mongo-swift-driver/master/Examples/Docs/Sources/AsyncExamples/main.swift -o ${DRIVERS_PATH}/swiftAsync.swift
curl -SfL https://raw.githubusercontent.com/mongodb/mongo-swift-driver/master/Examples/Docs/Sources/SyncExamples/main.swift -o ${DRIVERS_PATH}/swiftSync.swift

# kotlin-coroutine
curl -SfL https://raw.githubusercontent.com/mongodb/docs-kotlin/master/source/examples/ServerManualCodeExamples.kt -o ${DRIVERS_PATH}/kotlin_examples.kt

changelogs:
python3 changelogs/generatechangelogs.py
2 changes: 1 addition & 1 deletion snooty.toml
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ java-driver-api = "https://mongodb.github.io/mongo-java-driver/{+java-driver-ver
pymongo-api-docs = "https://pymongo.readthedocs.io/en/stable/api"
node-libmongocrypt-binding-docs = "https://github.com/mongodb/libmongocrypt/tree/master/bindings"
csharp-api-docs = "https://mongodb.github.io/mongo-csharp-driver/2.18/apidocs/html"
java-api-docs = "https://mongodb.github.io/mongo-java-driver/4.7/apidocs"
java-api-docs = "https://mongodb.github.io/mongo-java-driver/4.11/apidocs"
go-api-docs = "https://pkg.go.dev/go.mongodb.org/[email protected]"
enterprise-download-link = "https://www.mongodb.com/try/download/enterprise"
# C2C Product
Expand Down
9 changes: 9 additions & 0 deletions source/includes/driver-examples/driver-example-insert-1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@
:start-after: Start Example 1
:end-before: End Example 1

- id: kotlin-coroutine
content: |

.. literalinclude:: /driver-examples/kotlin_examples.kt
:language: kotlin
:dedent: 8
:start-after: Start Example 1
:end-before: End Example 1

- id: nodejs
content: |

Expand Down
9 changes: 9 additions & 0 deletions source/includes/driver-examples/driver-example-insert-2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@
:start-after: Start Example 2
:end-before: End Example 2

- id: kotlin-coroutine
content: |

.. literalinclude:: /driver-examples/kotlin_examples.kt
:language: kotlin
:dedent:
:start-after: Start Example 2
:end-before: End Example 2

- id: nodejs
content: |
.. literalinclude:: /driver-examples/node_insert.js
Expand Down
9 changes: 9 additions & 0 deletions source/includes/driver-examples/driver-example-insert-3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@
:start-after: Start Example 3
:end-before: End Example 3

- id: kotlin-coroutine
content: |

.. literalinclude:: /driver-examples/kotlin_examples.kt
:language: kotlin
:dedent: 8
:start-after: Start Example 3
:end-before: End Example 3

- id: nodejs
content: |
.. literalinclude:: /driver-examples/node_insert.js
Expand Down
9 changes: 9 additions & 0 deletions source/includes/driver-examples/driver-example-query-7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@
:start-after: Start Example 7
:end-before: End Example 7

- id: kotlin-coroutine
content: |

.. literalinclude:: /driver-examples/kotlin_examples.kt
:language: kotlin
:dedent:
:start-after: Start Example 7
:end-before: End Example 7

- id: nodejs
content: |
.. literalinclude:: /driver-examples/node_query.js
Expand Down
70 changes: 67 additions & 3 deletions source/tutorial/insert-documents.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Insert Documents

.. facet::
:name: programming_language
:values: shell, csharp, go, java, python, perl, php, ruby, scala, javascript/typescript
:values: shell, csharp, go, java, python, perl, php, ruby, scala, javascript/typescript, kotlin

.. meta::
:description: Examples of how to insert documents using MongoDB, including creating a collection upon first insert.
Expand Down Expand Up @@ -194,6 +194,19 @@ Insert a Single Document
ObjectId value to the new document. See
:ref:`write-op-insert-behavior`.

.. tab::
:tabid: kotlin-coroutine

`MongoCollection.insertOne <{+java-api-docs+}/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-collection/insert-one.html>`__
inserts a *single* :ref:`document<bson-document-format>` into
a collection.

The following example inserts a new document into the
``inventory`` collection. If the document does not specify
an ``_id`` field, the driver adds the ``_id`` field with an
ObjectId value to the new document. See
:ref:`write-op-insert-behavior`.

.. tab::
:tabid: nodejs

Expand Down Expand Up @@ -331,10 +344,28 @@ Insert a Single Document
.. tab::
:tabid: java-sync

`com.mongodb.client.MongoCollection.insertOne <{+java-api-docs+}/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#insertOne(TDocument)>`__ returns an
instance of `InsertOneResult
<{+java-api-docs+}/mongodb-driver-core/com/mongodb/client/result/InsertOneResult.html>`__.
You can access the ``_id`` field of the inserted document by
calling the `getInsertedId() <{+java-api-docs+}/mongodb-driver-core/com/mongodb/client/result/InsertOneResult.html#getInsertedId()>`__ method on the result.

.. tab::
:tabid: java-async

`com.mongodb.reactivestreams.client.MongoCollection.insertOne <http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/javadoc/com/mongodb/reactivestreams/client/MongoCollection.html#insertOne(TDocument)>`_
returns a `Publisher <http://www.reactive-streams.org/reactive-streams-1.0.1-javadoc/org/reactivestreams/Publisher.html>`_
object. The ``Publisher`` inserts the document into a collection when subscribers request data.

.. tab::
:tabid: kotlin-coroutine

`MongoCollection.insertOne <{+java-api-docs+}/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-collection/insert-one.html>`__ returns an
instance of `InsertOneResult
<{+java-api-docs+}/mongodb-driver-core/com/mongodb/client/result/InsertOneResult.html>`__.
You can access the ``_id`` field of the inserted document by
accessing the ``insertedId`` field of the result.

.. tab::
:tabid: nodejs

Expand Down Expand Up @@ -484,6 +515,18 @@ upper-right to set the language of the examples on this page.
``_id`` field, the driver adds the ``_id`` field with
an ObjectId value to each document. See :ref:`write-op-insert-behavior`.

.. tab::
:tabid: kotlin-coroutine

`MongoCollection.insertMany <{+java-api-docs+}/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-collection/insert-many.html>`__
inserts *multiple* :ref:`documents <bson-document-format>`
into a collection. Pass a list of documents as a parameter to the method.

The following example inserts three new documents into the
``inventory`` collection. If the documents do not specify an
``_id`` field, the driver adds an ObjectId value to each document.
See :ref:`write-op-insert-behavior`.

.. tab::
:tabid: nodejs

Expand Down Expand Up @@ -570,7 +613,6 @@ upper-right to set the language of the examples on this page.
``_id`` field, the driver adds the ``_id`` field with
an ObjectId value to each document. See :ref:`write-op-insert-behavior`.


.. include:: /includes/driver-examples/driver-example-insert-3.rst

.. tabs-drivers::
Expand Down Expand Up @@ -625,6 +667,17 @@ upper-right to set the language of the examples on this page.
To retrieve the inserted documents, :ref:`query the collection
<read-operations-query-document>`:

.. tab::
:tabid: kotlin-coroutine

`MongoCollection.insertMany() <{+java-api-docs+}/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-collection/insert-many.html>`_
returns an ``InsertManyResult`` instance. The ``insertedIds``
field of ``InsertManyResult`` contains the ``_id`` values of the
inserted documents.

To retrieve the inserted documents, :ref:`query the collection
<read-operations-query-document>`:

.. tab::
:tabid: nodejs

Expand Down Expand Up @@ -702,7 +755,6 @@ upper-right to set the language of the examples on this page.
To retrieve the inserted documents, :ref:`query the collection
<read-operations-query-document>`:


.. include:: /includes/driver-examples/driver-example-query-7.rst

.. _write-op-insert-behavior:
Expand Down Expand Up @@ -794,6 +846,18 @@ requested from MongoDB for write operations. For details, see

- `Java Reactive Streams Driver Quick Tour <http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/getting-started/quick-tour/>`_

.. tab::
:tabid: kotlin-coroutine

.. seealso::

- `MongoCollection.insertOne <{+java-api-docs+}/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-collection/insert-one.html>`__

- `MongoCollection.insertMany <{+java-api-docs+}/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-collection/insert-many.html>`__

- :driver:`Kotlin Driver Write Operation Examples
</kotlin/coroutine/current/fundamentals/crud/write-operations/insert/>`

.. tab::
:tabid: nodejs

Expand Down

0 comments on commit 7ab31f5

Please sign in to comment.