mongo_open()

Read(3055) Label: connect, mongodbserver,

Description:
Connect to the MongoDB server.

Syntax:
mongo_open(url,keyStory:keyStoryPasswd)

Note:

The external library function (See External Library Guide) establishes a connection to the MongoDB server. If parameter username is absent, parameter password can be omitted. When using SSL/TLS to connect the server, ssl=true can be omitted if parameters keyStory and keyStoryPasswd are present.

Parameter:

url 

mongodb://[username:password@]host1[:port1]  [,host2[:port2],...[,hostN[:portN]]]/database[?options/?rows]/?maxPoolSize

usernamepassword

Username and password; can be omitted

host

The host name or IP of to-be-connected MongoDB server; at least one value needs to be specified; to connect a replica set, specify multiple host machine IPs

port

Port number

database

Database name

options

MongoDB connection in standard format; can bet omitted

rows

Number of rows for sampling, which is used to count fields in a table and whose default is 100

maxPoolSize

The size of connection pool, whose value is 10 by default

keyStory

Set keyStory when using SSL/TLS to connect MongoDB server, meaning it is a trusted certificate

keyStoryPasswd

Password of keyStory

Return value:
MongoDB connection object

Example:

 

A

 

1

=mongo_open("mongodb://root:sa@localhost:27017/col")

With username root, password sa, host name localhost, and port number 27017, connect to database col on MongoDB server.

2

=mongo_open("mongodb://127.0.0.1:27017/mydb")

With IP: 127.0.0.1 and port number 27017, connect to database mydb on MongoDB server; no username and password.

3

=mongo_open("mongodb://127.0.0.1/mydb?safe=true; wtimeoutMS=2000")

Connect to database mydb on MongoDB server via IP: 127.0.0.1 in the safe mode; no port number; timeout value is 2 seconds.

4

=mongo_open("mongodb://127.0.0.1:27017/mydb?ssl=true","mongoStore.ts":"123456")

Use SSL/TLS to connect to MongoDB; the trusted certificate is mongoStore.tsf and the password is 123456.

5

=mongo_open("mongodb://127.0.0.1:27017/mydb","mongoStore.ts":"123456")

As both keyStory and keyStoryPasswd are present, ssl=ture is omitted.