mongo_open()

Read(755) Label: connect, mongodbserver,

Description:
 
Connect to the MongoDB server.

Syntax:
 
mongo_open(url)

Note:

The external library 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/?rows]

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

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

4

=mongo_open("mongodb://127.0.0.1:27017/mydb?rows=1000")

Connect to database mydb on mong server to get 1000 sampling records through IP:127.0.0.1 and port number 27017 and without a username and a password