mongoDB在java上面的应用
1、实际应用过程中肯定不会直接通过Linux的方式来连接和使用数据库,而是通过其他驱动的方式来使用mongoDB
2、本教程只针对于Java来做操作,主要是模拟mongoDB数据库在开发过程中的应用
3、在官网下载对应的jar包,来做mongoDB的驱动支持,当然也可以利用pom.xml文件自己下载
jar下载地址:https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongodb-driver/3.4.2/
这个是3.4.2版本的,有需要可以下载其他版本

也可以在pom.xml文件中加入
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.4.2</version>
</dependency>
</dependencies>
4、连接数据库,连接数据库的方式有两种。一种是没有用户名密码,另外一种是有的
1)没有密码
public static void main(String[] args) {
try{
// 连接到 mongodb 服务
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
// 连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
System.out.println("Connect to database successfully");
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
2)有密码
public static void main(String[] args) {
try {
//连接到MongoDB服务 如果是远程连接可以替换“localhost”为服务器所在IP地址
//ServerAddress()两个参数分别为 服务器地址 和 端口
ServerAddress serverAddress = new ServerAddress("localhost",27017);
List<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add(serverAddress);
//MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
MongoCredential credential = MongoCredential.createScramSha1Credential("username", "databaseName", "password".toCharArray());
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
//通过连接认证获取MongoDB连接
MongoClient mongoClient = new MongoClient(addrs,credentials);
//连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("databaseName");
System.out.println("Connect to database successfully");
} catch (Exception e) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
5、创建集合
public static void main(String[] args) {
try{
// 连接到 mongodb 服务
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
// 连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
System.out.println("Connect to database successfully");
mongoDatabase.createCollection("test");
System.out.println("集合创建成功");
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
6、获取集合
public static void main(String[] args) {
try{
// 连接到 mongodb 服务
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
// 连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
System.out.println("Connect to database successfully");
MongoCollection<Document> collection = mongoDatabase.getCollection("test");
System.out.println("集合 test 选择成功");
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
7、插入文档(BSON格式)
public static void main(String[] args) {
try{
MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); // 连接到 mongodb 服务
MongoDatabase mongoDatabase = mongoClient.getDatabase("test"); // 连接到数据库
System.out.println("Connect to database successfully");
MongoCollection<Document> collection = mongoDatabase.getCollection("test"); //获取集合
System.out.println("集合 test 选择成功");
Document document = new Document("title", "MongoDB").append("description", "database").append("likes", 100); //新建文档
List<Document> documents = new ArrayList<Document>();
documents.add(document);
collection.insertMany(documents); //添加文档(对应的BSON数据)
System.out.println("文档插入成功");
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
8、检索文档,查找对用数据
public static void main(String[] args) {
try{
MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); // 连接到 mongodb 服务
MongoDatabase mongoDatabase = mongoClient.getDatabase("test"); // 连接到数据库
System.out.println("Connect to database successfully");
MongoCollection<Document> collection = mongoDatabase.getCollection("test"); //获取集合
System.out.println("集合 test 选择成功");
FindIterable<Document> findIterable = collection.find(); //获取迭代器
MongoCursor<Document> mongoCursor = findIterable.iterator(); //获取游标
while(mongoCursor.hasNext()){ //循环获取数据
System.out.println(mongoCursor.next());
}
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
9、更新文档数据
public static void main( String args[] ){
try{
MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); // 连接到 mongodb 服务
MongoDatabase mongoDatabase = mongoClient.getDatabase("mycol"); // 连接到数据库
System.out.println("Connect to database successfully");
MongoCollection<Document> collection = mongoDatabase.getCollection("test");
System.out.println("集合 test 选择成功");
collection.updateMany(Filters.eq("likes", 100), new Document("$set",new Document("likes",200))); //更新文档 将文档中likes=100的文档修改为likes=200
FindIterable<Document> findIterable = collection.find(); //检索查看结果
MongoCursor<Document> mongoCursor = findIterable.iterator();
while(mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
}
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
10、删除文档
public static void main( String args[] ){
try{
MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); // 连接到 mongodb 服务
MongoDatabase mongoDatabase = mongoClient.getDatabase("test"); // 连接到数据库
System.out.println("Connect to database successfully");
MongoCollection<Document> collection = mongoDatabase.getCollection("test");
System.out.println("集合 test 选择成功");
collection.deleteOne(Filters.eq("likes", 200)); //删除符合条件的第一个文档
collection.deleteMany (Filters.eq("likes", 200)); //删除所有符合条件的文档
//检索查看结果
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
while(mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
}
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
11、基本的操作就是这样,在实际应用过程中,可以通过一定的数据格式要求来做一些修改和添加,数据的应用可以通过Document来具体获取获取进行处理
12、为了方便数据库的管理和学习也可以参考其他的学习方法,来实现对应的结果
13、推荐一个学习mongoDB的网站,适合新手!老鸟略过。。。
网址:http://www.runoob.com/mongodb/mongodb-tutorial.html
mongoDB在java上面的应用的更多相关文章
- 在Eclipse中设置Java类上面的注释(包含作者、日期等)
希望在Eclipse中,让Java类上面的注释像下面这样,改如何设置呢? 在Eclipse中,点击菜单中的Window-->Preferences,打开如下窗口:
- 从上面的集合框架图可以看到,Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射
从上面的集合框架图可以看到,Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射.Collection 接口又有 3 ...
- mongodb与java整合
mongodb与java整合需要用到mongodb驱动,如果是maven环境,则添加如下倚赖: <dependency> <groupId>org.mongodb</gr ...
- MongoDB的Java驱动使用整理 (转)
MongoDB Java Driver 简单操作 一.Java驱动一致性 MongoDB的Java驱动是线程安全的,对于一般的应用,只要一个Mongo实例即可,Mongo有个内置的连接池(池大小默认为 ...
- 在MongoDB的MapReduce上踩过的坑
太久没动这里,目前人生处于一个新的开始.这次博客的内容很久前就想更新上来,但是一直没找到合适的时间点(哈哈,其实就是懒),主要内容集中在使用Mongodb时的一些隐蔽的MapReduce问题: 1.R ...
- 【MongoDB for Java】Java操作MongoDB
上一篇文章: http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html介绍到了在MongoDB的控制台完成MongoDB的数据操作,通过 ...
- edtftpj让Java上传FTP文件支持断点续传
在用Java实现FTP上传文件功能时,特别是上传大文件的时候,可以需要这样的功能:程序在上传的过程中意外终止了,文件传了一大半,想从断掉了地方继续传:或者想做类似迅雷下载类似的功能,文件太大,今天传一 ...
- [转]MongoDB for Java】Java操作MongoDB
原文地址: MongoDB for Java]Java操作MongoDB 开发环境: System:Windows IDE:eclipse.MyEclipse 8 Database:mongoDB 开 ...
- mongodb在java驱动包下的操作(转)
推荐几章很有用的文章 java操作参考文档 http://www.cnblogs.com/hoojo/archive/2011/06/02/2068665.html http://blog.csdn. ...
随机推荐
- 通过JAVA从MQ读取消息的时候报错及解决
如果是通过JAVA将消息写入到MQ,再通过JAVA去读取消息,采用MQMessage读消息的方法readUTF()去读取的时候,就不会报错,可以正常读出来.如果采用在MQ资源管理器中插入测试消息或者是 ...
- 使用 Open Live Writer 创建我的第一个博文
希望能在此记录我的技术开发过程. 请记住我的博客首页为:https://www.cnblogs.com/unrulife/ 期待在博客园遇到志同道合的朋友! 希望在博客园开启技术生涯的新篇章!
- python 中if-else的多种简洁的写法
因写多了判断语句,看着短短的代码却占据来好几行,于是便搜下if-else简洁的写法,结果也是发现新大陆 4种: 第1种:__就是普通写法 a, b, c = 1, 2, 3 if a>b: c ...
- 通过nginx 499 来判断服务端超时数量
这个其实不能算一篇文章,因为内容太少了,就当记点笔记吧. (1)什么是 nginx 499 499 其实是 nginx 下特有的 http 状态码,代表客户端主动断开了连接,导致服务器无法返回 htt ...
- poj 1159 Palindrome 【LCS】
任意门:http://poj.org/problem?id=1159 解题思路: LCS + 滚动数组 AC code: #include <cstdio> #include <io ...
- springmvc小结(下)
1.@ModelAttribute 1.给共享的数据设置model数据设置,贴在形参上,也可以贴在方法上,设置一个model的key值 2.当controller方法返回一个对象的时候,,缺省值会把当 ...
- java _this关键字的用法
1:This关键字可以用于从一个构造方法调用另一个构造方法,可以用于避免重复代码 2:this的第二个用于this.xxx表示成员变量,成员变量的作用范围是 类 避免产生歧义 package c ...
- Ajax全局加载框(Loading效果)的配置
在Ajax进行后台数据请求的过程中,我们有时候会希望用户能知道页面后台还在做一些事情,这时候就需要给用户一个非常明确的提示,也就是我们所谓的进度条 废话完成~ 实现原理: Jquery可以对ajax进 ...
- 修改office文档修改日期
修改“创建日期”可采用如下方法: 首先把系统日期调整到您所希望的时间,然后到MS-DOS方式下,对该文件输入如下命令:COPY /B filename +,, (一个加号.两个逗号),当询问您是否确认 ...
- 提示AttributeError: 'module' object has no attribute 'HTTPSHandler'解决方法
今天在新机器上安装sqlmap,运行提示AttributeError: 'module' object has no attribute 'HTTPSHandler' 网上找了找资料,发现一篇文章ht ...