Components | All | New | MacOS | Windows | Linux | iOS | ||||
Examples | Mac & Win | Server | Client | Guides | Statistic | FMM | Blog | Deprecated | Old |
MongoDB.SetURI
Sets the URI.
Component | Version | macOS | Windows | Linux | Server | iOS SDK |
MongoDB | 12.3 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |
Parameters
Parameter | Description | Example |
---|---|---|
MongoDBRef | The reference number for the mongo connection. | $MongoDB |
URI | The URI to use. | "mongodb://localhost/" |
Result
Returns OK or error.
Description
Sets the URI.Parses a string containing a MongoDB style URI connection string.
Returns an error in case of a parsing error.
Examples of some valid MongoDB connection strings can be seen below.
- "mongodb://localhost/"
- "mongodb://localhost/?replicaSet=myreplset"
- "mongodb://myuser:mypass@localhost/"
- "mongodb://kerberosuser%40EXAMPLE.COM@example.com/?authMechanism=GSSAPI"
- "mongodb://[::1]:27017/"
- "mongodb://10.0.0.1:27017,10.0.0.1:27018,[::1]:27019/?tls=true"
- "mongodb://%2Ftmp%2Fmongodb-27017.sock"
- "mongodb://user:pass@%2Ftmp%2Fmongodb-27017.sock"
- "mongodb://localhost,[::1]/mydb?authSource=mydb"
If you like to use a SSH tunnel, you can create a tunnel with our SSH functions. Use SSH.Tunnel.Run to connect a local port with your remote server and the destination (usually port 27017). Then connect to localhost with the local port.
Examples
Connect to local server:
Set Variable [ $r ; Value: MBS( "MongoDB.SetURI"; $Mongo; "mongodb://localhost/" ) ]
Connect to local server with replication set and timeout:
Set Variable [ $r ; Value: MBS( "MongoDB.SetURI"; $Mongo; "mongodb://localhost/?replicaSet=rs0&connectTimeoutMS=300000" ) ]
Query options from URL:
Set Variable [ $Mongo ; Value: MBS( "MongoDB.New" ) ]
#
# set URL with options
Set Variable [ $r ; Value: MBS( "MongoDB.SetURI"; $Mongo; "mongodb://test@1.2.3.4/?ssl=true&tlsCAFile=" & GetAsURLEncoded ( "/Users/cs/cacert.pem" )) ]
#
# query and show options
Set Variable [ $options ; Value: MBS( "MongoDB.GetOptions"; $Mongo ) ]
Show Custom Dialog [ "Options" ; $options ]
#
# success
Exit Script [ Text Result: $Mongo ]
Connect to online server for mongodb with certificate network:
Set Variable [ $r ; Value: MBS( "MongoDB.SetURI"; $Mongo; "mongodb+srv://test.6gtstkv.mongodb.net/?authSource=%24external&authMechanism=MONGODB-X509&retryWrites=true&w=majority&tlsCAFile=%2FUsers%2Ftest%2FMongoDB%20online%2Fcacert.pem&tlsCertificateKeyFile=%2FUsers%2Ftest%2FMongoDB%20online%2FX509-cert.pem" ) ]
Connect to mongodb and allow invalid certificates:
Set Variable [ $r ; Value: MBS( "MongoDB.SetURI"; $Mongo; "mongodb+srv://testmbs.6gswtkv.mongodb.net/?" &
"authSource=%24external&" &
"authMechanism=MONGODB-X509&" &
"retryWrites=true&" &
"tlsAllowInvalidCertificates=true&" &
"w=majority&" &
"tls=true&" &
"tlsCertificateKeyFile=" & GetAsURLEncoded("/Users/cs/X509-cert-6862180682313299640.pem") ) ]
Connect to MongoDB with certificate:
Set Variable [ $r ; Value: MBS( "MongoDB.SetSSLOptions"; $Mongo; "/Users/cs/X509-cert-6862180682313299640.pem"; ""; "/Users/cs/cacert.pem") ]
Set Variable [ $r ; Value: MBS( "MongoDB.SetURI"; $Mongo; "mongodb+srv://testmbs.6gswtkv.mongodb.net/" ) ]
Set Variable [ $r ; Value: MBS( "MongoDB.SetAuthMechanism"; $Mongo; "MONGODB-X509" ) ]
Set Variable [ $r ; Value: MBS( "MongoDB.SetAuthSource"; $Mongo; "$external" ) ]
Connect with user name:
Set Variable [ $r ; Value: MBS( "MongoDB.SetSSLOptions"; $Mongo; ""; ""; "/Users/cs/cacert.pem") ]
Set Variable [ $r ; Value: MBS( "MongoDB.SetURI"; $Mongo; "mongodb+srv://namecluster.xxxx.mongodb.net/?retryWrites=true&w=majority" ) ]
Set Variable [ $r ; Value: MBS( "MongoDB.SetUserName"; $Mongo; "user" ) ]
Set Variable [ $r ; Value: MBS( "MongoDB.SetPassword"; $Mongo; "pass" ) ]
Set Variable [ $r ; Value: MBS( "MongoDB.SetAppName"; $Mongo; "namecluster" ) ]
See also
- MongoDB.GetTLS
- MongoDB.GetURI
- MongoDB.InsertOne
- MongoDB.New
- MongoDB.SetAppName
- MongoDB.SetAuthMechanism
- MongoDB.SetSSLOptions
- MongoDB.UpdateMany
- MongoDB.UpdateOne
- SSH.Tunnel.Run
Example Databases
Blog Entries
This function checks for a license.
Created 22nd May 2022, last changed 24th June 2024