C # tutorial on MongoDB when accessing a cluster deployment

Source: Internet
Author: User
Tags mongoclient mongodb

Objective

The common MongoDB deployment has a master, a standby, the program to connect MongoDB requires the address of the primary server and port, when the cluster deployment will have multiple server addresses need to connect, the MongoDB C # API can support simultaneous connection to multiple servers.

Single Station

mongoclient mc = new Mongoclient ("mongodb://oec2003:20720");
Mongoserver ms = MC. Getserver ();
Mongodatabase MD = Ms. Getdatabase ("dbname");
Cluster

list<mongoserveraddress> list = new list<mongoserveraddress> ();
List. ADD (New Mongoserveraddress ("192.168.16.207", 40000));
List. ADD (New Mongoserveraddress ("192.168.16.208", 40000));
List. ADD (New Mongoserveraddress ("192.168.16.209", 40000));
Mongoclientsettings MCS = new Mongoclientsettings ();
MCs. Servers = list;
mongoclient mc = new Mongoclient (MCS);
Mongoserver ms = MC. Getserver ();

Mongodatabase MD = Ms. Getdatabase ("dbname");

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.