|
| 1 | +================== |
| 2 | +Kotlin Sync Driver |
| 3 | +================== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: twocols |
| 12 | + |
| 13 | + |
| 14 | +Introduction |
| 15 | +------------ |
| 16 | + |
| 17 | +Welcome to the documentation site for the Kotlin Sync Driver, the |
| 18 | +official MongoDB driver for synchronous Kotlin applications. |
| 19 | + |
| 20 | +- `API Documentation <{+java-api+}/apidocs/mongodb-driver-kotlin-sync/>`__ |
| 21 | + |
| 22 | +- `Source Code <https://github.com/mongodb/mongo-java-driver/tree/master/driver-kotlin-sync/>`__ |
| 23 | + |
| 24 | +If you need to make asynchronous calls between your application and MongoDB with |
| 25 | +coroutines, use the :driver:`MongoDB Kotlin Coroutine Driver </kotlin/coroutine/current/>` |
| 26 | +instead of the Sync Driver. |
| 27 | + |
| 28 | +Installation |
| 29 | +------------ |
| 30 | + |
| 31 | +The recommended way to get started using the driver in your project is with |
| 32 | +a dependency management system. |
| 33 | + |
| 34 | +If you are using `Gradle <https://gradle.org/>`__, add the following to your |
| 35 | +``build.gradle.kts`` dependencies list: |
| 36 | + |
| 37 | +.. code-block:: xml |
| 38 | + :caption: build.gradle.kts |
| 39 | + |
| 40 | + dependencies { |
| 41 | + implementation("org.mongodb:mongodb-driver-kotlin-sync:{+java-version-full+}") |
| 42 | + } |
| 43 | + |
| 44 | +If you are using `Maven <https://maven.apache.org/>`__, add the following to |
| 45 | +your ``pom.xml`` dependencies list: |
| 46 | + |
| 47 | +.. code-block:: xml |
| 48 | + :caption: pom.xml |
| 49 | + |
| 50 | + <dependencies> |
| 51 | + <dependency> |
| 52 | + <groupId>org.mongodb</groupId> |
| 53 | + <artifactId>mongodb-driver-kotlin-sync</artifactId> |
| 54 | + <version>{+java-version-full+}</version> |
| 55 | + </dependency> |
| 56 | + </dependencies> |
| 57 | + |
| 58 | +.. _connect-atlas-kotlin-sync-driver: |
| 59 | + |
| 60 | +Connect to MongoDB Atlas |
| 61 | +------------------------ |
| 62 | + |
| 63 | +.. include:: /includes/atlas-connect-blurb.rst |
| 64 | + |
| 65 | +.. code-block:: kotlin |
| 66 | + |
| 67 | + import com.mongodb.ConnectionString |
| 68 | + import com.mongodb.kotlin.client.sync.MongoClient |
| 69 | + import com.mongodb.kotlin.client.sync.MongoDatabase |
| 70 | + import com.mongodb.MongoClientSettings |
| 71 | + |
| 72 | + // Replace the placeholder with your Atlas connection string |
| 73 | + val uri = "<connection string>" |
| 74 | + |
| 75 | + val settings = MongoClientSettings.builder() |
| 76 | + .applyConnectionString(ConnectionString(uri)) |
| 77 | + .retryWrites(true) |
| 78 | + .build() |
| 79 | + |
| 80 | + // Create a new client and connect to the server |
| 81 | + val mongoClient = MongoClient.create(settings) |
| 82 | + val database = mongoClient.getDatabase("test") |
| 83 | + |
| 84 | +.. include:: /includes/serverless-compatibility.rst |
| 85 | + |
| 86 | +{+stable-api+} |
| 87 | +-------------- |
| 88 | + |
| 89 | +You can use the {+stable-api+} feature starting with MongoDB Server version |
| 90 | +5.0 and Kotlin Sync Driver. When you use the |
| 91 | +{+stable-api+} feature, you can update your driver or server without worrying |
| 92 | +about backward compatibility issues with any commands covered by the |
| 93 | +{+stable-api+}. |
| 94 | + |
| 95 | +.. include:: /includes/stable-api-notice.rst |
| 96 | + |
| 97 | +To use this feature, construct a MongoDB client instance, specifying a version |
| 98 | +of the {+stable-api+}: |
| 99 | + |
| 100 | +.. literalinclude:: /includes/stable-api-snippets/kotlin-sync-client.kt |
| 101 | + :language: kotlin |
| 102 | + |
| 103 | +Connect to a MongoDB Server on Your Local Machine |
| 104 | +------------------------------------------------- |
| 105 | + |
| 106 | +.. include:: /includes/localhost-connection.rst |
| 107 | + |
| 108 | +To test whether you can connect to your server, replace the connection |
| 109 | +string in the :ref:`Connect to MongoDB Atlas <connect-atlas-kotlin-sync-driver>` |
| 110 | +code example and run it. |
| 111 | + |
| 112 | +Compatibility |
| 113 | +------------- |
| 114 | + |
| 115 | +MongoDB Compatibility |
| 116 | +~~~~~~~~~~~~~~~~~~~~~ |
| 117 | + |
| 118 | +The following compatibility table specifies the recommended version(s) of |
| 119 | +the MongoDB Kotlin Sync driver for use with a specific version of MongoDB. |
| 120 | + |
| 121 | +The first column lists the driver version(s). |
| 122 | + |
| 123 | +.. include:: /includes/mongodb-compatibility-table-kotlin-sync.rst |
| 124 | + |
| 125 | +.. include:: /includes/older-server-versions-unsupported.rst |
| 126 | + |
| 127 | +Language Compatibility |
| 128 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 129 | + |
| 130 | +The following compatibility table specifies the recommended version(s) of the |
| 131 | +MongoDB Kotlin Sync driver for use with a specific version of Kotlin. |
| 132 | + |
| 133 | +The first column lists the driver version(s). |
| 134 | + |
| 135 | +.. include:: /includes/language-compatibility-table-kotlin-sync.rst |
| 136 | + |
| 137 | +.. include:: /includes/about-driver-compatibility.rst |
| 138 | + |
| 139 | +.. include:: /includes/help-links-kotlin.rst |
| 140 | + |
0 commit comments