您的位置:首页 > 编程语言 > Go语言

MongoClient类参考文档

2016-03-28 00:09 435 查看
(MongoClient类的实例是)一个带有内部连接池的MongoDB客户端。对绝大部分应用来说,都应该有一个
MongoClient
实例(不知怎翻译)。

下面各种创建实例的方法是等效的,并且全部都通过默认端口连到本地数据库的:

MongoClient mongoClient1 = new MongoClient();

MongoClient mongoClient1 = new MongoClient(“localhost”);

MongoClient mongoClient2 = new MongoClient(“localhost”, 27017);

MongoClient mongoClient4 = new MongoClient(new ServerAddress(“localhost”));

MongoClient mongoClient5 = new MongoClient(new ServerAddress(“localhost”), new MongoClientOptions.Builder().build());

你也可以连接到备份服务器集合,通过传递一个备份服务器地址列表作为参数到
MongoClient
的构造方法中。例如:

MongoClient mongoClient = new MongoClient(Arrays.asList(

new ServerAddress(“localhost”, 27017),

new ServerAddress(“localhost”, 27018),

new ServerAddress(“localhost”, 27019)));

你还可以使用同样的构造方法连接到分片集群上。

MongoClient
会自动检测这些数据库服务器是备份服务器还是集群服务器。

默认情况下,所有读写操作都发生在主数据库上,但也可以改变读的设置让读操作发生在从数据库: mongoClient.setReadPreference(ReadPreference.secondaryPreferred());

默认情况下,所有写操作都会等待服务器的响应,默认的写关注的是:WriteConcern.ACKNOWLEDGED

提示:
MongoClient
类取代了
Mongo
类。同时它也继承自
Mongo
类,但不同的是,它所有的写操作默认都是等待服务器响应。另外,它的构造方法接受
MongoClientOptions
MongoClientURI
类的实例,这两者的默认写关注也是一样的。

通常来说,使用这个类只要使用
MongoClientOptions
提供的默认值就行了。特别注意一下,每个服务器默认的连接数已经从过去使用
Mongo
的10增加到了100.

A MongoDB client with internal connection pooling. For most applications, you should have one MongoClient instance for the entire JVM.

The following are equivalent, and all connect to the local database running on the default port:

MongoClient mongoClient1 = new MongoClient();

MongoClient mongoClient1 = new MongoClient(“localhost”);

MongoClient mongoClient2 = new MongoClient(“localhost”, 27017);

MongoClient mongoClient4 = new MongoClient(new ServerAddress(“localhost”));

MongoClient mongoClient5 = new MongoClient(new ServerAddress(“localhost”), new MongoClientOptions.Builder().build());

You can connect to a replica set using the Java driver by passing a ServerAddress list to the MongoClient constructor. For example:

MongoClient mongoClient = new MongoClient(Arrays.asList(

new ServerAddress(“localhost”, 27017),

new ServerAddress(“localhost”, 27018),

new ServerAddress(“localhost”, 27019)));

By default, all read and write operations will be made on the primary, but it’s possible to read from secondaries by changing the read preference:

mongoClient.setReadPreference(ReadPreference.secondaryPreferred());

By default, all write operations will wait for acknowledgment by the server, as the default write concern is WriteConcern.ACKNOWLEDGED.

Note: This class supersedes the Mongo class. While it extends Mongo, it differs from it in that the default write concern is to wait for acknowledgment from the server of all write operations. In addition, its constructors accept instances of MongoClientOptions and MongoClientURI, which both also set the same default write concern.

In general, users of this class will pick up all of the default options specified in MongoClientOptions. In particular, note that the default value of the connectionsPerHost option has been increased to 100 from the old default value of 10 used by the superseded Mongo class.

Since:2.10.0See Also:ReadPreference.primary()com.mongodb.WriteConcern.ACKNOWLEDGEDMongoClientOptionsMongoClientURI
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: