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
MBS( "MongoDB.SetURI"; MongoDBRef; URI )   More

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" ) ]

See also

Example Databases

Blog Entries

This function checks for a license.

Created 22nd May 2022, last changed 6th August 2023


MongoDB.SetSSLOptions - MongoDB.SetUserName