MongoClient类参考文档
2016-03-28 00:09
435 查看
(MongoClient类的实例是)一个带有内部连接池的MongoDB客户端。对绝大部分应用来说,都应该有一个
下面各种创建实例的方法是等效的,并且全部都通过默认端口连到本地数据库的:
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 = new MongoClient(Arrays.asList(
new ServerAddress(“localhost”, 27017),
new ServerAddress(“localhost”, 27018),
new ServerAddress(“localhost”, 27019)));
你还可以使用同样的构造方法连接到分片集群上。
默认情况下,所有读写操作都发生在主数据库上,但也可以改变读的设置让读操作发生在从数据库: mongoClient.setReadPreference(ReadPreference.secondaryPreferred());
默认情况下,所有写操作都会等待服务器的响应,默认的写关注的是:WriteConcern.ACKNOWLEDGED
提示:
通常来说,使用这个类只要使用
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
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
相关文章推荐
- SENGO森果汇聚婚礼一切美好
- django 源码阅读二 命令check
- Django-Form表单(验证、定制、错误信息、Select)
- 2016/3/27 分页 共X条数据 本页x条 本页从x-y条 x/y页 首页 上一页 123456 下一页 末页 pagego echo $page->fpage(7,6,5,4,3,2,1,0);
- golang笔记——并发
- Django进阶(路由系统、中间件、缓存、Cookie和Session、Ajax发送数据
- 在Google Maps 上点击标签显示说明并保持不消失
- 在Google Maps 上点击标签后显示说明
- 如何在Google Maps 添加多个标记
- Google开源单元测试框架Google Test:VS2012 配置
- golang笔记——函数与方法
- HDU Let's go to play
- 如何在 Google 地图中添加标记和说明
- 启动Genymotion模拟器时,显示The virtual device got no IP address
- gone fishing
- HDU--1533--Going Home--KM算法
- Go语言学习(九)文件操作
- 【Algorithm】选择排序法
- 初识google多语言通信框架gRPC系列(二)编译gRPC
- Google Test