-
Notifications
You must be signed in to change notification settings - Fork 10
DOCSP-41121 Connect to Mongo Client #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lindseymoore
merged 16 commits into
mongodb:master
from
lindseymoore:DOCSP-41121-create-mongoclient
Aug 15, 2024
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1bc27b8
DOCSP-41121 Connect to MongoDB
lindseymoore 41338e5
reword
lindseymoore 5bd5db6
fix markdown errors
lindseymoore b8cfd03
fix
lindseymoore f1d1edc
change title
lindseymoore ff56653
review comments
lindseymoore 031cf0f
fix
lindseymoore 520ff3e
fix pt 2
lindseymoore 58ba4b8
add api doc section
lindseymoore c70619f
fix connection string link
lindseymoore ab30e3d
new option for connecting
lindseymoore 60f3b74
revision
lindseymoore 737405f
connection string link
lindseymoore 9be90ae
fix toc
lindseymoore 1e73d86
merge conflicts
lindseymoore 3c63a76
fix toc
lindseymoore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
.. _kotlin-sync-connection-options: | ||
|
||
========================== | ||
Specify Connection Options | ||
========================== | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 1 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: connection string, URI, server, Atlas, settings, configure | ||
|
||
Overview | ||
-------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
.. _kotlin-sync-mongoclient: | ||
|
||
==================== | ||
Create a MongoClient | ||
==================== | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: connection string, URI, server, Atlas, settings, client | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
Overview | ||
-------- | ||
|
||
To connect to a MongoDB deployment, you need two things: | ||
|
||
- A **connection URI**, also known as a *connection string*, which tells the {+driver-short+} | ||
which MongoDB deployment to connect to. | ||
- A **MongoClient** object, which creates the connection to and performs | ||
operations on the MongoDB deployment. | ||
|
||
You can also use ``MongoClientSettings`` to customize the way the {+driver-short+} behaves | ||
while connected to MongoDB. | ||
|
||
This guide shows you how to create a connection string and use a ``MongoClient`` object | ||
to connect to MongoDB. | ||
|
||
.. _kotlin-sync-connection-uri: | ||
|
||
Connection URI | ||
-------------- | ||
|
||
A standard connection string includes the following components: | ||
|
||
.. TODO, add this as last sentence for ``username:password`` description once a kotlin auth page is made: | ||
.. For more information about the ``authSource`` connection option, see :ref:`kotlin-sync-auth`. | ||
|
||
.. list-table:: | ||
:widths: 20 80 | ||
:header-rows: 1 | ||
|
||
* - Component | ||
- Description | ||
|
||
* - ``mongodb://`` | ||
|
||
- Required. A prefix that identifies this as a string in the | ||
standard connection format. | ||
|
||
* - ``username:password`` | ||
|
||
- Optional. Authentication credentials. If you include these, the client | ||
authenticates the user against the database specified in ``authSource``. | ||
|
||
* - ``host[:port]`` | ||
|
||
- Required. The host and optional port number where MongoDB is running. If you don't | ||
include the port number, the driver uses the default port, ``27017``. | ||
|
||
* - ``/defaultauthdb`` | ||
|
||
- Optional. The authentication database to use if the | ||
connection string includes ``username:password@`` | ||
authentication credentials but not the ``authSource`` option. If you don't include | ||
this component, the client authenticates the user against the ``admin`` database. | ||
|
||
* - ``?<options>`` | ||
|
||
- Optional. A query string that specifies connection-specific | ||
options as ``<name>=<value>`` pairs. See | ||
:ref:`kotlin-sync-connection-options` for a full description of | ||
jyemin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
these options. | ||
|
||
For more information about creating a connection string, see | ||
:manual:`Connection Strings </reference/connection-string>` in the | ||
MongoDB Server documentation. | ||
|
||
Atlas Connection Example | ||
------------------------ | ||
|
||
To connect to a MongoDB deployment on Atlas, you must first create a client. | ||
|
||
You can pass a connection URI as a string to the ``MongoClient.create()`` method | ||
to connect to a MongoDB instance: | ||
|
||
.. literalinclude:: /includes/connect/mongoclient2.kt | ||
:start-after: start-connect-to-atlas-w-uri | ||
:end-before: end-connect-to-atlas-w-uri | ||
:language: kotlin | ||
:copyable: | ||
:dedent: | ||
|
||
You can also create a client with your desired configurations by passing a | ||
``MongoClientSettings`` object to the ``MongoClient.create()`` method. | ||
|
||
To instantiate a ``MongoClientSettings`` object, use the builder method to | ||
jyemin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
specify your connection string, using the ``applyConnectionString()`` method, | ||
and any other client options. Once you have your desired configuration, | ||
call the ``build()`` method. | ||
|
||
You can set the Stable API version client option to avoid breaking changes when | ||
you upgrade to a new server version. To learn more about the Stable API feature, | ||
see the :ref:`Stable API page <kotlin-sync-stable-api>`. | ||
|
||
The following code shows how you can specify the connection string and the | ||
Stable API client option when connecting to a MongoDB deployment on Atlas | ||
and verify that the connection is successful: | ||
|
||
.. literalinclude:: /includes/connect/mongoclient.kt | ||
:start-after: start-connect-to-atlas | ||
:end-before: end-connect-to-atlas | ||
:language: kotlin | ||
:copyable: | ||
:dedent: | ||
|
||
API Documentation | ||
----------------- | ||
|
||
For more information about creating a ``MongoClient`` object with the | ||
{+driver-short+}, see the following API documentation: | ||
|
||
- `MongoClient <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-client/index.html>`__ | ||
- `MongoClientSettings <{+core-api+}com/mongodb/MongoClientSettings.html>`__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import com.mongodb.* | ||
import com.mongodb.kotlin.client.MongoClient | ||
import org.bson.BsonInt64 | ||
import org.bson.Document | ||
|
||
fun main() { | ||
|
||
// start-connect-to-atlas | ||
// start-connect-to-atlas-w-uri | ||
// Replace the placeholder with your Atlas connection string | ||
val uri = "<connection string>" | ||
val mongoClient1 = MongoClient.create(uri) | ||
// end-connect-to-atlas-w-uri | ||
|
||
// Construct a ServerApi instance using the ServerApi.builder() method | ||
val serverApi = ServerApi.builder() | ||
.version(ServerApiVersion.V1) | ||
.build() | ||
val settings = MongoClientSettings.builder() | ||
.applyConnectionString(ConnectionString(uri)) | ||
.serverApi(serverApi) | ||
.build() | ||
|
||
// Create a new client and connect to the server | ||
val mongoClient = MongoClient.create(settings) | ||
val database = mongoClient.getDatabase("sample_mflix") | ||
|
||
try { | ||
// Send a ping to confirm a successful connection | ||
val command = Document("ping", BsonInt64(1)) | ||
val commandResult = database.runCommand(command) | ||
println("Pinged your deployment. You successfully connected to MongoDB!") | ||
} catch (me: MongoException) { | ||
System.err.println(me) | ||
} | ||
// end-connect-to-atlas | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import com.mongodb.* | ||
import com.mongodb.kotlin.client.MongoClient | ||
|
||
fun main() { | ||
|
||
// start-connect-to-atlas-w-uri | ||
// Replace the placeholder with your Atlas connection string | ||
val uri = "<connection string>" | ||
|
||
// Create a new client and connect to the server | ||
val mongoClient = MongoClient.create(uri) | ||
val database = mongoClient.getDatabase("sample_mflix") | ||
// end-connect-to-atlas-w-uri | ||
|
||
try { | ||
// Send a ping to confirm a successful connection | ||
val command = Document("ping", BsonInt64(1)) | ||
val commandResult = database.runCommand(command) | ||
println("Pinged your deployment. You successfully connected to MongoDB!") | ||
} catch (me: MongoException) { | ||
System.err.println(me) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.