mongo_open()

Read(289) Label: connect, mongodbserver,

Description:
 
Connect to the MongoDB server.

Syntax:
 
mongo_open(url)

Note:

The function establishes a connection to the MongoDB server. If parameter username is absent, parameter password can be omitted.

Parameters:

url 

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

usernamepassword

Username and password; can be omited

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, specifiy multiple host machine IPs

port

Port number

database

Database name

options

MongoDB connection in standard format; can bet omitted

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

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