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. ...
随机推荐
- JQuery获取和设置Select选项的常用方法总结
1.获取select 选中的 text: $("#cusChildTypeId").find("option:selected").text(); $(&q ...
- oracle数据库恢复归档脚本
cat restorearch1.sh rman target / << EOFRUN {SET ARCHIVELOG DESTINATION TO '/archlog1/db2_arch ...
- MyBatis框架(6)动态sql
本次全部学习内容:MyBatisLearning 什么是动态sql: mybatis的核心,对sql进行灵活的操作,通过对表达式的判断,对sql灵活的拼接 在之前小案例的基础上我们先进行简 ...
- 第五章.MyBatis高级映射
5.1 新建数据库准备 CREATE TABLE `finacial_products` ( `product_id` ) NOT NULL AUTO_INCREMENT, `name` ) NO ...
- ESlint 语法检测配置说明
部分vue-cli脚手架创建的默认eslint规则: 代码末尾不能加分号 ; 代码中不能存在多行空行 tab键不能使用,必须换成两个空格 代码中不能存在声明了但未使用的变量 关闭eslint 这里只说 ...
- git忽略文件留存
##ignore this file##/target/ .classpath.project.settings /.settings/ ##filter databfile.sln file##*. ...
- 使用CSV Data Set Config实现参数化登录
在使用Jemeter做压力测试的时候,往往需要参数化用户名,密码以到达到多用户使用不同的用户名密码登录的目的.这个时候我们就可以使用CSV Data Set Config实现参数化登录: 首先通过Te ...
- Vue教程:指令与事件(二)
一.插值 v-once 通过使用 v-once 指令,你也能执行一次性地插值,当数据改变时,插值处的内容不会更新.但请留心这会影响到该节点上所有的数据绑定: span v-once>这个将不会改 ...
- STM32之关闭JTAG
1.有些时候不想用JTAG口(而用SWJ在线调试),把JTAG暂用的IO通过remap出来使用 2.比如48 pin的STM32F103CBT6单片机: GPIO_PinRemapConfig(GPI ...
- 一、hadoop 及 hadoop的环境搭建
一.Hadoop引言 Hadoop是在2006年雅虎从Nutch(给予Java爬虫框架)工程中剥离一套分布式的解决方案.该方案参考了Goggle的GFS(Google File System)和Map ...