MongoClient类参考文档

(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类,但不同的是,它所有的写操作默认都是等待服务器响应。另外,它的构造方法接受MongoClientOptionsMongoClientURI类的实例,这两者的默认写关注也是一样的。

通常来说,使用这个类只要使用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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/430162.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

反序列化对象列表发生异常_通过反序列化漏洞,黑客能做什么呢?

在之前的文章中讲解了一个反序列化的例子,我们已经知道,通过反序列化漏洞,黑客可以调用到Runtime.exec()来进行命令执行。换一句话说,黑客已经能够在服务器上执行任意的命令,这就相当于间接掌控了你的服务器&#xff0…

nodeJS理解

nodejs是一个基于Chrome V8 引擎的JS运行环境,也就是让javascript运行在服务器(server)端, NodeJS使用了一个事件驱动,非阻塞式的I/O模型,使得其轻量又高效。 Nodejs包管理器npm是全球最大的开源生态系统。…

pythonwhile输出每一个余数_Python 基础 - day02-3

Python 基础 - day02-3循环目标程序的三大流程while 循环基本使用break 和 continuewhile 循环嵌套01. 程序的三大流程在程序开发中, 一共有三种流程方式:顺序 -- 从上向下, 顺序执行代码分支 -- 根据条件判断, 决定执行代码的 分支循环 -- 让 特定代码 重复 执行02. while 循环…

Spring MVC表单验证

看登录控制器代码: RequestMapping(methodRequestMethod.POST,path"/login")public void login(Validated UserEntity user,BindingResult result,HttpServletResponse response) throws BindException{if(result.hasErrors()){response.setHeader("…

小程序开发语言python_小程序是用什么语言开发的?5种最佳语言分享

你应该学习哪种编程语言来探索AI的深度?当然,你会需要一个拥有许多优秀机器学习和深度学习库的语言。它还应具有良好的运行时性能,良好的工具支持,大量程序员社区以及健康的支持包生态系统。这仍然留下了很多不错的选择。下面是我…

tcl之内容

转载于:https://www.cnblogs.com/chip/p/5847126.html

自定义hibernate validation注解

效果和优点 先看最后效果: public class UserEntity {Password private String password;Emailprivate String email;} 上面使用了两个自定义的注解来验证password和email,这样做的好处是:一处定义,处处使用,要修改…

《JS权威指南学习总结--9.5 类和类型》

内容要点: 介绍了三种用以检测任意对象的类的技术,instanceof运算符、constructor属性,以及构造函数的名字。 但每种技术都不甚完美,本节总结了鸭式辩型,这种编程哲学更加关注对象可以完成什么工作(它包含什…

js密码强度正则表达式_知道这20个前端正则表达式,能让你做项目时少写1000行甚至一万行

正则表达式,一个十分古老而又强大的文本处理工具,仅仅用一段非常简短的表达式语句,便能够快速实现一个非常复杂的业务逻辑。熟练地掌握正则表达式的话,能够使你的开发效率得到极大的提升。正则表达式经常被用于字段或任意字符串的…

MongoDB数据库设计备忘

1、sex存为01还是男女? 存为01主要是为了国际化和节省存储空间。存为01很容易转换成不同语言表示的男女。 2、文件存储方案 MongoDB有GirdFS用来将文件直接保存到数据库,如果使用nginx,可以直接使用http读取GridFS里面的文件。 所以&…

react js 按条数 展开/折叠

//2条const MAX_SHOW_NUM 2;class HotDiscuss extends Component { static propTypes { //验证 repliedCommentList: PropTypes.array, }; constructor(props) { super(props); this.state { //子评论折叠 isCl…

python怎么获取时间_Python:如何从datetime.timedelta对象中获取时间?

在我看来,MySQL中的TIME类型旨在表示时间间隔,就像Python中的datetime.timedelta那样。从您引用的文档中:TIME values may range from -838:59:59 to 838:59:59. The hours part may be so large because the TIME type can be used not only…

腾讯视频客户端导出MP4格式

腾讯视频下载下来之后是.qlv格式,这种格式只能使用客户端播放。感觉挺蛋疼的,Mp4好不容易让各种设备都可以统一播放视频了,这样一搞感觉又倒退了几十年,关键的是,有些资源是腾讯独家的,不装客户端就看不了&…

Java上传文件到MongoDB GirdFS

上传有两种方式,一种是使用UploadFromStream,另外一种是OpenUploadStream方式,这里使用的是第二种方式: public ObjectId save(){GridFSBucket gfsbk GridFSBuckets.create(db, "user_photo");byte[] data "Data…

js后退页面不重新加载_快应用:支持加载单独JS文件的规范思考

当前快应用的项目中,支持加载其它JS文件(通过:require(./foo.js)),然后通过webpack工具处理依赖,最终完成页面JS的构建,其中页面JS包含了引入的所有JS内容;本文讨论的主要是&#xf…

Linux Linux程序练习七

题目&#xff1a;实现两个程序mysignal、mycontrl&#xff0c;mycontrl给mysignal发送SIGINT信号&#xff0c;控制mysignal是否在屏幕打印“hello”字符串。 //捕捉信号#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h>…

python读取每一行文字二十四_python接口自动化(二十四)--unittest断言——中(详解)...

简介上一篇通过简单的案例给小伙伴们介绍了一下unittest断言&#xff0c;这篇我们将通过结合和围绕实际的工作来进行unittest的断言。这里以获取城市天气预报的接口为例&#xff0c;设计了 2 个用例&#xff0c;一个是查询北京的天气&#xff0c;一个是查询南京为例&#xff0c…

spring MVC配置form支持PUT和DELETE方法

REST的关键原则之一就是“使用标准接口”&#xff08;the use of the Uniform Interface&#xff09;&#xff0c;也就是提倡根据不同的语义使用GET, PUT, POST和DELETE方法&#xff0c;而html的form标签只支持两种提交方法&#xff1a;POST和GET&#xff0c;所以&#xff0c;为…

如何学习编译原理?

编译原理很难学&#xff0c;但如果自己动手编&#xff0c;会简单一点&#xff0c;而且不要想一口吃成胖子&#xff0c;从词法分析开始写&#xff0c;一步步来&#xff0c;下个龙书电子版看看。慢慢来&#xff0c;一点一点地去消化它&#xff0c;让它成为你的知识。 转载于:http…

Spring MVC访问不到静态资源

运行Spring MVC项目&#xff0c;发现.css&#xff0c;js等文件加载不了&#xff0c;一般是spring MVC的拦截匹配导致的。 例如我在web.xml里这样配置&#xff1a; <servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.w…