クラスターの Connect ダイアログ ボックスには、MongoDBクライアントライブラリを使用するアプリケーションでクラスターに接続するための詳細が表示されます。
➤ [言語の選択] ドロップダウン メニューを使用して、このページの例の言語を設定します。
前提条件
ドライバーのバージョン
ドライバーのバージョンは、MongoDB サーバーのバージョンと互換性がある必要があります。最新のデータベース機能を使用し、将来のバージョン アップグレードに備えるために、MongoDB サーバーのバージョンと互換性のある最新のドライバーを選択することをお勧めします。
ご使用のMongoDBサーバーのバージョンの完全な機能セットを含むドライバー バージョンのリストは、 ドライバーの クライアント ライブラリの互換性テーブル を確認してください。
重要
正式にサポートされているドライバー
Use officially supported MongoDB drivers at current versions for the best reliability and support experience. The Atlas SLA covers officially supported drivers only.
Atlas は、Community ドライバーとサポート終了ドライバーバージョンに対して、ベストエフォートの診断のみのサポートを提供します。Atlas は、Community ドライバーのテスト、パッチ、認証は行いません。サポートは、接続の問題がプラットフォームとドライバーのどちらに起因するかを判定するのに役立ちますが、ドライバーコードのデバッグは行いません。
さらに学ぶには、Atlas の Community ドライバーとサポートされていないドライバーのサポートポリシーはどのようになっていますか。を参照してください。
プライベートエンドポイントの背後にあるシャーディングされたクラスターの最適化された接続文字列
ドライバーと最適化された接続文字列を使用してシャーディングされたクラスターに接続できます。
TLS
Atlas クラスターに接続するには、クライアントが TLS をサポートしている必要があります。
Clients must support the SNI TLS extension to connect to an Atlas Free cluster (formerly known as M0) or Flex cluster.
IP アクセス リスト
クラスターにアクセスするには、Atlas プロジェクトの IP アクセス リストにある IP アドレスから接続する必要があります。IP アクセス リストに IP アドレスを追加する必要がある場合は、Connect ダイアログ ボックスで追加できます。[ネットワーク アクセス] タブから IP アドレスを追加することもできます。
データベースユーザー
To access a cluster, you must have a database user with access to the desired databases on your Atlas cluster. Database users are separate from Atlas users. Database users have access to MongoDB databases, while Atlas users have access to the Atlas application itself.
Connect ダイアログ ボックスで、Atlas クラスターにアクセスするためのデータベースユーザーを作成できます。クラスター ビューからデータベースユーザーを追加することもできます。Atlas は、最初のクラスターを作成すると、データベースユーザーを作成します。
アプリケーションの接続
Atlas で、プロジェクトの Clusters ページに移動します。
まだ表示されていない場合は、希望するプロジェクトを含む組織を選択しますナビゲーション バーのOrganizationsメニュー
まだ表示されていない場合は、ナビゲーション バーのProjectsメニューから目的のプロジェクトを選択します。
サイドバーで、 Database見出しの下のClustersをクリックします。
[ Clusters (クラスター) ] ページが表示されます。
接続セキュリティを選択します。
Choose Connection Type (使用可能なボタンのセットから選択)。
注意
機能が有効な場合のオプション表示
Atlas は、ピアリングのプライベート IP、プライベートエンドポイント、またはその両方を有効にすると、接続タイプのオプションを表示します。どちらの機能も有効にしていない場合、ボタンは表示されず、接続タイプのデフォルトは標準になります。
許可されたパブリック IP アドレスにはこの接続タイプを使用します。
ピアリングを有効にした場合は、次の接続タイプを使用します。
Google Cloud Platform または Azure の場合、ピアリングされたネットワークからドライバーに接続している場合、または
AWS の場合、カスタム DNS サービスを使用するピアリングされたネットワークからドライバーに接続します。
これらのいずれも当てはまらない場合は、IP アドレスを IP アクセス リストに追加し、標準接続文字列を使用します。オフィスまたは自宅のネットワークから Atlas に直接接続する場合は、このオプションが望ましいでしょう。
注意
ピアリングが利用可能である必要があります
ピアの 1 つが AVAILABLE としてマークされていない限り、このオプションを選択することはできません。ピアのステータスを確認する方法については、Atlas ネットワークピアリング接続の表示 を参照してください。
注意
マルチクラウドクラスター
アプリケーションがクラスターのプライマリと同じクラウドサービスプロバイダーでホストされていない場合、アプリケーションはセカンダリ読み取りのみを実行できます。
With multi-cloud clusters, consider adding the readPreference connection option to your connection string. Use one of the following values:
プライベートエンドポイント接続を介してドライバーに接続する場合は、ドライバーが以下のいずれかの理由で適切なインターフェイスエンドポイントの接続文字列を使用してください。
クラウドプロバイダー ネットワーク内で実行されます、または
クラウドプロバイダー ネットワークへの推移的なネットワーク アクセスを持ちます。
最適化された接続文字列 を使用したいと考えています。
これらのいずれにも当てはまらない場合は、IP アドレスを IP アクセス リストに追加し、標準接続文字列を使用します。オフィスや自宅のネットワークから Atlas に直接接続する場合は、このオプションが望ましいでしょう。
注意
構成された PrivateLink 接続が使用可能状態になっていない限り、このオプションを選択することはできません。AWS PrivateLink のステータスを確認する方法については、プライベートエンドポイント接続の問題のトラブルシューティング を参照してください。
クラスターへの接続を制限する方法を選択します。
Add a Connection IP Address
重要
Atlas が Setup connection security ステップで、クラスターに IP アクセス リスト エントリがすでに設定されていることを示している場合は、このステップをスキップします。IP アクセス リストを管理するには、「アクセス リストへのエントリの追加」を参照してください。
Atlas はプロジェクトの IP アクセス リストのエントリからクラスターへの標準クライアント接続を許可します。プロジェクトの IP アクセス リストは、API アクセス リスト とは異なります。APIアクセスリストは、特定の IP アドレスまたは CIDR アドレスへの API アクセスを制限します。
IP アクセス リストが空の場合、Atlas はプロジェクトの IP アクセス リストに IP アドレスを追加するように求めます。次のいずれかを実行できます。
現在の IP アドレスからのアクセスを許可するには、 [Add Your Current IP Address] をクリックします。
単一の IP アドレスまたは CIDR 表記のアドレス範囲を追加するには、[Add an IP Address] をクリックします。
新しく追加された IP アドレスまたは CIDR 範囲の説明を任意で入力します。Add IP Addressをクリックして、アドレスを IP アクセス リストに追加します。
Add a Connection IP Address
重要
Atlas がSetup connection securityステップで、クラスターに IP アクセス リスト エントリーがすでに設定されていることを示している場合は、このステップをスキップします。IP アクセス リストを管理するには、 「IP アクセス リストへのエントリの追加」を参照してください。
Atlas はプロジェクトの IP アクセス リストのエントリからクラスターへの標準クライアント接続を許可します。プロジェクトの IP アクセス リストは、API アクセス リスト とは異なります。APIアクセスリストは、特定の IP アドレスまたは CIDR アドレスへの API アクセスを制限します。
IP アクセス リストが空の場合、Atlas はプロジェクトの IP アクセス リストに IP アドレスを追加するように求めます。単一の IP アドレスまたは CIDR 表記のアドレス範囲を追加するには、[Add a Different IP Address] をクリックします。
新しく追加された IP アドレスまたは CIDR 範囲の説明を任意で入力します。Add IP Addressをクリックして、アドレスを IP アクセス リストに追加します。
Choose Connection Type で Private Endpoint を選択します。
Private Link Typeオプションが表示された場合は、次のいずれかのオプションを選択します。
Optimized SRV Connection 負荷分散された接続用。
Legacy SRV Connection 負荷分散されていない接続の場合。
詳しくは、プライベートエンドポイントを使用してシャーディングされたクラスターの接続パフォーマンスを最適化するにはどうすればよいですか?をご覧ください。
[ Choose Private Endpoint] で、使用するエンドポイントを選択します。
データベースユーザーを作成します。
重要
Atlas がSetup connection security の手順でプロジェクトに少なくとも 1 人のデータベースユーザーが設定されていることを示している場合は、この手順をスキップします。既存のデータベースユーザーを管理するには、「データベースユーザーの構成」を参照してください。
クラスターにアクセスするには、プロジェクト内の目的のデータベースまたはクラスター上のデータベースにアクセスできる MongoDB ユーザーが必要です。プロジェクトに MongoDB ユーザーがいない場合、Atlas は Atlas 管理者ロールを持つ新しいユーザーを作成するように要求します。
新しいユーザーの Username を入力します。
この新しいユーザーの Password を入力するか、Autogenerate Secure Password をクリックします。
Create Database User をクリックしてユーザーを保存します。
次のステップでは、このユーザーを使用してクラスターに接続します。
IP アクセス リストに IP アドレスを追加し、データベースユーザーを追加したら、Choose Your Connection Method をクリックします。
ドライバーとバージョンを選択します。
ドロップダウン メニューからドライバーとバージョンを選択します。接続文字列を含むコード サンプルが表示されます。<password> を、データベースユーザーの作成時に指定したパスワードに置き換えます。
Use this connection string in your application では、接続文字列はデフォルトで SRV 形式 (mongodb+srv://) を使用します。標準(SRV 以外)の接続文字列(mongodb://)を表示するには、SRV Connection String トグルをオフにします。
注意
パスワード、データベース、または接続文字列に予約済みの URI 文字が含まれている場合は、それらの文字をエスケープする必要があります。たとえば、パスワードが @bc123 の場合、接続文字列でパスワードを指定するときは、%40bc123 などに指定して、@ の文字をエスケープする必要があります。詳細については、「接続文字列パスワードの特殊文字」を参照してください。
詳細については、「ドライバーの互換性」を参照してください。
ドライバーの例
In the following example, you authenticate and connect to an Atlas cluster by using a URI connection string. Replace the placeholders in the example with your credentials and deployment details.
注意
Atlas M0 無料クラスターまたはフレックス クラスターに接続するには、MongoDB 7.0 以降をサポートする C ドライバー バージョンを使用する必要があります。C ドライバーと MongoDB の互換性に関する完全なドキュメントについては、「MongoDB 互換性マトリックス」を参照してください。最新バージョンのドライバーにアップグレードすることをお勧めします。
client = mongoc_client_new ("mongodb+srv://<db_username>:<db_password>@<clusterName>.mongodb.net/?retryWrites=true&w=majority"); db = mongoc_client_get_database (client, "<databaseName>");
MongoDB バージョン | 推奨ドライバーのバージョン |
|---|---|
すべて | See the MongoDB compatibility matrix for the latest recommended driver versions. |
MongoDB 7.0以降 | バージョン 1.11 以降 |
動作
注意
以下の構成オプションは、C ドライバーをシングルスレッド モードで実行する場合にのみ適用されます。
MongoDB ドライバーは、クラスターの選択またはフェイルオーバー イベントの後に自動的にサーバーの選択を試みます。デフォルトでは、C ドライバーは、サーバーを選択する最初の試行が失敗するとすぐにエラーを発生させます。以下の設定により、Atlas クラスタへのアプリケーションの接続性を向上させることができますが、サーバ選択ループに多くの時間を費やすことになります。
Set serverSelectionTryOnce to
falseto direct the C driver to perform server selection up to the time limit defined byserverSelectionTimeoutMS.Lower the serverSelectionTimeoutMS to
15000from the default of30000. MongoDB elections typically take 10 seconds, but can be as fast as 5 seconds on Atlas. Setting this value to 15 seconds (15000milliseconds) covers the upper bound of election plus additional time for latency.
注意
To connect to an Atlas M0 Free cluster or a Flex cluster, you must use a C++ driver version that supports MongoDB 7.0 and later. For complete documentation on compatibility between the C++ driver and MongoDB, see the MongoDB compatibility matrix. We recommend that you upgrade to the latest version of the driver.
//... mongocxx::instance inst{}; mongocxx::client conn{mongocxx::uri{"mongodb+srv://<db_username>:<db_password>@<clusterName>.mongodb.net/?retryWrites=true&w=majority"}}; mongocxx::database db = conn["<databaseName>"];
MongoDB バージョン | 推奨ドライバーのバージョン |
|---|---|
すべて | See the MongoDB compatibility matrix for the latest recommended driver versions. |
注意
レガシー C++ ドライバーはEOL(End Of Life)に達したため、サポートは終了しました。
動作
注意
以下の構成オプションは、C++ ドライバーのシングルスレッド mongocxx::client クラスを使用して Atlas クラスターに接続する場合にのみ適用されます。
MongoDB ドライバーは、クラスターの選挙またはフェイルオーバー イベントの後に自動的にサーバーの選択を試行します。デフォルトでは、C++ ドライバーは、サーバーを選択する最初の試行が失敗するとすぐにエラーを発生させます。以下の構成設定により、Atlas クラスターに対するアプリケーションの接続性は向上するかもしれませんが、サーバーを選択するためのループにかかる時間は増加します。
Set serverSelectionTryOnce to
falseto direct the C++ driver to perform server selection up to the time limit defined byserverSelectionTimeoutMS.Lower the serverSelectionTimeoutMS to
15000from the default of30000. MongoDB elections typically take 10 seconds, but can be as fast as 5 seconds on Atlas. Setting this value to 15 seconds (15000milliseconds) covers the upper bound of election plus additional time for latency.
注意
To connect to an Atlas M0 Free cluster or a Flex cluster, you must use a C#/.Net driver version that supports MongoDB 7.0 and later. For complete documentation on compatibility between the C#/.Net driver and MongoDB, see the MongoDB compatibility matrix. We recommend that you upgrade to the latest version of the driver.
var client = new MongoClient("mongodb+srv://<db_username>:<db_password>@<clusterName>.mongodb.net/?retryWrites=true&w=majority"); var database = client.GetDatabase("<databaseName>");
MongoDB バージョン | 推奨ドライバーのバージョン |
|---|---|
すべて | See the MongoDB compatibility matrix for the latest recommended driver versions. |
MongoDB 7.0以降 | バージョン 2.7 以降 |
注意
Microsoft .NET Core ライブラリ バージョン 2.1 以降 では、Linuxおよび macOS で SNI TLS 拡張機能がサポートされます。これらのプラットフォームで以前のバージョンの.NET Core を使用しているアプリケーションは、Atlas 無料クラスターまたは Flex クラスターに接続できません。
この要件の詳細については、この.NET/corefx の問題を参照してください。
注意
Atlas M0 無料クラスターまたはフレックス クラスターに接続するには、MongoDB 7.0 以降をサポートする Go ドライバー バージョンを使用する必要があります。Go ドライバーと MongoDB の互換性に関する完全なドキュメントについては、「MongoDB 互換性マトリックス」を参照してください。最新バージョンのドライバーにアップグレードすることをお勧めします。
uri := "mongodb+srv://<db_username>:<db_password>@<clusterName>.mongodb.net/?retryWrites=true&w=majority" clientOpts := options.Client().ApplyURI(uri).SetTimeout(10 * time.Second) client, err := mongo.Connect(clientOpts) if err != nil { log.Fatalf("Failed to connect to MongoDB: %v", err) } defer func() { if err = client.Disconnect(); err != nil { log.Fatalf("Failed to disconnect from MongoDB: %v", err) } }() ctx := context.Background() // Ping the primary if err := client.Ping(ctx, readpref.Primary()); err != nil { log.Fatalf("Failed to ping MongoDB: %v", err) }
MongoDB バージョン | 推奨ドライバーのバージョン |
|---|---|
すべて | See the MongoDB compatibility matrix for the latest recommended driver versions. |
MongoDB 7.0以降 | バージョン 1.0.0以降 |
注意
To connect to an Atlas M0 cluster or a Flex cluster, you must use Java version 8 or later and use a Java driver version that supports MongoDB 7.0 or later. To learn more about compatibility between the Java driver and MongoDB, see the MongoDB compatibility matrix.
MongoClientURI uri = new MongoClientURI( "mongodb+srv://<db_username>:<db_password>@<clusterName>.mongodb.net/?retryWrites=true&w=majority"); MongoClient mongoClient = MongoClients.create(uri); MongoDatabase database = mongoClient.getDatabase("<databaseName>");
MongoDB バージョン | 推奨ドライバーのバージョン |
|---|---|
すべて | See the MongoDB compatibility matrix for the latest recommended driver versions. |
MongoDB 7.0以降 | バージョン 3.8 以降 |
注意
Atlas M0 無料クラスターまたはフレックス クラスターに接続するには、MongoDB 7.0 以降をサポートする Kotlin ドライバー バージョンを使用する必要があります。Kotlin ドライバーと MongoDB の互換性に関する完全なドキュメントについては、「MongoDB 互換性マトリックス」を参照してください。最新バージョンのドライバーにアップグレードすることをお勧めします。
val uri = "mongodb+srv://<db_username>:<db_password>@<clusterName>.mongodb.net/?retryWrites=true&w=majority" // 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("<databaseName>") 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) }
MongoDB バージョン | 推奨ドライバーのバージョン |
|---|---|
MongoDB 7.0以降 | バージョン 4.10 以降 |
注意
Atlas M0 無料クラスターまたはフレックス クラスターに接続するには、MongoDB 7.0 以降をサポートする Node.js ドライバーバージョンを使用する必要があります。Node.js ドライバーと MongoDB の互換性に関する完全なドキュメントについては、 MongoDB 互換性マトリックスを参照してください。最新バージョンのドライバーにアップグレードすることをお勧めします。
import mongodb from 'mongodb'; const MongoClient = mongodb.MongoClient; const uri = "mongodb+srv://<db_username>:<db_password>@<clusterName>.mongodb.net/?retryWrites=true&w=majority"; const client = new MongoClient(uri); client.connect(err => { const collection = client.db("<databaseName>").collection("<collectionName>"); // perform actions on the collection object client.close(); });
admin 以外のデータベースに接続し、admin データベースに対して認証を行うには、接続文字列の database コンポーネントを更新します。
mongodb://username:password@host1:port1,...,hostN:portN/database?authSource=admin&...
例
次の接続文字列は、cluster0 配置と test データベース コンポーネントを指定し、authSource=admin オプションを含みます。
var uriTestDb = "mongodb+srv://<db_username>:<db_password>@cluster0.mongodb.net/test?ssl=true&authSource=admin&w=majority"; MongoClient.connect(uriTestDb, function(err, db) { db.close(); });
MongoDB バージョン | 推奨ドライバーのバージョン |
|---|---|
すべて | See the MongoDB compatibility matrix for the latest recommended driver versions. |
MongoDB 7.0以降 | バージョン 3.1 以降 |
注意
To connect to an Atlas M0 Free cluster or a Flex cluster, you must use a PHP driver version that supports MongoDB 7.0 and later. For complete documentation on compatibility between the PHP driver and MongoDB, see the MongoDB compatibility matrix. We recommend that you upgrade to the latest version of the driver.
次の例では、MongoDB PHP ライブラリを使用します。このライブラリは低レベルの PHP ドライバーの上に高レベルの抽象化を提供します。
$client = new MongoDB\Client( 'mongodb+srv://<db_username>:<db_password>@<clusterName>.mongodb.net/?retryWrites=true&w=majority'); $db = $client-><databaseName>;
MongoDB バージョン | 推奨ドライバーのバージョン |
|---|---|
すべて | See the MongoDB compatibility matrix for the latest recommended driver versions. |
MongoDB 7.0以降 | PHPLIB 1.4 + mongodb-1.5. |
動作
MongoDB ドライバーは、クラスターの選挙またはフェイルオーバー イベントの後に自動的にサーバーの選択を試行します。デフォルトでは、サーバーを選択する最初の試行が失敗すると、ドライバーはすぐにエラーを発生させます。 以下の構成設定により、Atlas クラスターに対するアプリケーションの接続性は向上するかもしれませんが、サーバーを選択するためのループにかかる時間は増加します。
Set serverSelectionTryOnce to
falseto direct the driver to perform server selection up to the time limit defined byserverSelectionTimeoutMS.Lower the serverSelectionTimeoutMS to
15000from the default of30000. MongoDB elections typically take 10 seconds, but can be as fast as 5 seconds on Atlas. Setting this value to 15 seconds (15000milliseconds) covers the upper bound of election plus additional time for latency.
注意
To connect to an Atlas M0 cluster or a Flex cluster, you must use Python 2.7.9 or later and use a Python driver version that supports MongoDB 7.0 or later. To learn more about compatibility between the Python driver and MongoDB, see the MongoDB compatibility matrix.
import pymongo import dns # required for connecting with SRV client = pymongo.MongoClient("mongodb+srv://<db_username>:<db_password>@<clusterName>.mongodb.net/?retryWrites=true&w=majority") db = client.<databaseName>
MongoDB バージョン | 推奨ドライバーのバージョン |
|---|---|
すべて | See the MongoDB compatibility matrix for the latest recommended driver versions. |
MongoDB 7.0以降 | バージョン 3.7 以降 |
注意
macOS と Python 3.6 インストーラー
https://www.python.org の macOS 用Python 3.6 インストーラーは CA 証明書を自動的にインストールしません。CA 証明書がインストールされていない場合、Atlas への接続で証明書の検証が失敗します。
https://www.python.org からインストーラーを実行した後Python 3.6 をインストールするには、Atlas に接続する前に、次のスクリプトを実行して最新の CA バンドルをインストールする必要があります。
open "/Applications/Python 3.6/Install Certificates.command"
3.6macOS 用のPython インストーラーの詳細については、 https://www.python.org をご覧ください。https://bugs.python.org/issue29065#msg283984 を参照してください。Pythonの以前のバージョンとPython 3.6他の手段でインストールされた場合(例:Homebrew )は影響を受けません。
注意
Atlas M0 無料クラスターまたはフレックス クラスターに接続するには、MongoDB 7.0 以降をサポートする Ruby ドライバー バージョンを使用する必要があります。Ruby ドライバーと MongoDB の互換性に関する完全なドキュメントについては、「MongoDB 互換性マトリックス」を参照してください。最新バージョンのドライバーにアップグレードすることをお勧めします。
client = Mongo::Client.new('mongodb+srv://<db_username>:<db_password>@<clusterName>.mongodb.net/?retryWrites=true&w=majority')
admin 以外のデータベースに接続し、admin データベースに対して認証を行うには、接続文字列の database コンポーネントを更新します。
mongodb://username:password@host1:port1,...,hostN:portN/database?authSource=admin&...
例
次の接続文字列は、cluster0 配置と test データベースコンポーネントを指定し、authSource=admin オプションを含みます。
client = Mongo::Client.new('mongodb+srv://<db_username>:<db_password>@cluster0.mongodb.net/test?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin&w=majority')
MongoDB バージョン | 推奨ドライバーのバージョン |
|---|---|
すべて | See the MongoDB compatibility matrix for the latest recommended driver versions. |
MongoDB 7.0以降 | バージョン 2.6 以降 |
Mongoid の例
production: # Configure available database clients. (required) clients: # Defines the default client. (required) default: # Defines the name of the default database that Mongoid can connect to. # (required). database: 'myDatabaseName' # Provides the hosts the default client can connect to. Must be an array # of host:port pairs. (required) hosts: - mycluster0-shard-00-00.mongodb.net:27017 - mycluster0-shard-00-01.mongodb.net:27017 - mycluster0-shard-00-02.mongodb.net:27017 options: # The name of the user for authentication. user: <username> # The password of the user for authentication. password: <password> # The database or source to authenticate the user against. If the database # specified above is not admin, admin MUST be specified here. auth_source: admin # All Atlas servers use SSL. (default: false) ssl: true
MongoDB バージョン | ODM の最小バージョン |
|---|---|
MongoDB 7.0以降 |
ドライバーが使用するデフォルトの非同期ランタイムは tokio です。別のランタイムを使用するには、非同期ランタイムの構成 を参照してください。
注意
To connect to an Atlas M0 Free cluster or a Flex cluster, you must use a Rust driver version that supports MongoDB 7.0 and later. For complete documentation on compatibility between the Rust driver and MongoDB, see the MongoDB compatibility matrix. We recommend that you upgrade to the latest version of the driver.
use mongodb::{options::ClientOptions, Client}; async fn main() -> mongodb::error::Result<()> { let client_options = ClientOptions::parse( "mongodb+srv://<db_username>:<db_password>@<clusterName>.mongodb.net/?retryWrites=true&w=majority", ) .await?; let client = Client::with_options(client_options)?; let _database = client.database("<databaseName>"); // List the names of the databases in that cluster for db_name in client.list_database_names(None, None)? { println!("{}", db_name); } Ok(()) }
MongoDB バージョン | 推奨ドライバーのバージョン |
|---|---|
すべて | See the MongoDB compatibility matrix for the latest recommended driver versions. |
同期 API が有効になっていることを確認します。詳細については、同期 API の有効化を を参照してください。
注意
To connect to an Atlas M0 Free cluster or a Flex cluster, you must use a Rust driver version that supports MongoDB 7.0 and later. For complete documentation on compatibility between the Rust driver and MongoDB, see the MongoDB compatibility matrix. We recommend that you upgrade to the latest version of the driver.
use mongodb::{bson::doc, sync::Client}; fn main() -> mongodb::error::Result<()> { let client_options = ClientOptions::parse( "mongodb+srv://<db_username>:<db_password>@<clusterName>.mongodb.net/?retryWrites=true&w=majority", )?; // Ping the server to see if you can connect to the cluster client .database("admin") .run_command(doc! {"ping": 1}, None)?; println!("Connected successfully."); // List the names of the databases in that cluster for db_name in client.list_database_names(None, None)? { println!("{}", db_name); } Ok(()) }
MongoDB バージョン | 推奨ドライバーのバージョン |
|---|---|
すべて | See the MongoDB compatibility matrix for the latest recommended driver versions. |
注意
To connect to an Atlas M0 Free cluster or a Flex cluster, you must use a Scala driver version that supports MongoDB 7.0 and later. For complete documentation on compatibility between the Scala driver and MongoDB, see the MongoDB compatibility matrix. We recommend that you upgrade to the latest version of the driver.
val uri: String = "mongodb+srv://<db_username>:<db_password>@<clusterName>.mongodb.net/?retryWrites=true&w=majority" System.setProperty("org.mongodb.async.type", "netty") val client: MongoClient = MongoClient(uri) val db: MongoDatabase = client.getDatabase("<databaseName>")
MongoDB バージョン | 推奨ドライバーのバージョン |
|---|---|
すべて | See the MongoDB compatibility matrix for the latest recommended driver versions. |
注意
To connect to an Atlas M0 Free cluster or a Flex cluster, you must use a Swift driver version that supports MongoDB 7.0 and later. For complete documentation on compatibility between the Swift driver and MongoDB, see the MongoDB compatibility matrix. We recommend that you upgrade to the latest version of the driver.
import MongoSwift import NIO let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4) let uri = "mongodb+srv://<db_username>:<db_password>@<clusterName>.mongodb.net/?retryWrites=true&w=majority" let client = try MongoClient( uri, using: elg ) defer { // clean up driver resources try? client.syncClose() cleanupMongoSwift() // shut down EventLoopGroup try? elg.syncShutdownGracefully() } // print a list of database names to confirm connection print(try client.listDatabaseNames().wait())
MongoDB バージョン | 推奨ドライバーのバージョン |
|---|---|
すべて | See the MongoDB compatibility matrix for the latest recommended driver versions. |
注意
To connect to an Atlas M0 Free cluster or a Flex cluster, you must use a Swift driver version that supports MongoDB 7.0 and later. For complete documentation on compatibility between the Swift driver and MongoDB, see the MongoDB compatibility matrix. We recommend that you upgrade to the latest version of the driver.
import MongoSwiftSync let uri = "mongodb+srv://<db_username>:<db_password>@<clusterName>.mongodb.net/?retryWrites=true&w=majority" let client = try MongoClient(uri) // print a list of database names to confirm connection print (try client.listDatabaseNames())
MongoDB バージョン | 推奨ドライバーのバージョン |
|---|---|
すべて | See the MongoDB compatibility matrix for the latest recommended driver versions. |
トラブルシューティング
クラスターへの接続に問題がある場合は、「接続の問題のトラブルシューティング」を参照してください。
Tip