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. ...
随机推荐
- 一些jar包的作用
发项目的过程中,经常需要用到一写jar包,现在对其中的一写jar的作用,做下总结,也算是避免少引或者多引jar包吧,欢迎大家补充.谢谢. 1.apache的一写jar commons-collecti ...
- CodeForces 91A Newspaper Headline
题目链接:CodeForces - 91A Newspaper Headline 官方题解: In this problem letters from s1 should be taken gree ...
- 三.Shell脚本提取文件名称和所在的目录
一·简介 提取文件名称或者目录,一般都会使用到#,##,%和%%,但是他们的区别很容易记混淆了.在一下4种方式中,目标匹配字符是不在结果中. #:表示从左开始算起,并且截取第一个匹配的字符 ##:表示 ...
- UVa 1151 - Buy or Build(最小生成树)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 计算机网络概述4_性能指标之时延,时延带宽积,往返时间RTT,利用率
发送时延(传输时延):从发送分组的第一个比特算起,到该分组的最后一个比特发送完毕所需的时间. 总结:
- master分支合并
一.Sourcetree简单介绍 通过Git可以进行对项目的版本管理,但是如果直接使用Git的软件会比较麻烦,因为是通过一条一条命令进行操作的. Sourcetree则可以与Git结合,提供图形界面 ...
- CVPR 2016 paper reading (6)
1. Neuroaesthetics in fashion: modeling the perception of fashionability, Edgar Simo-Serra, Sanja Fi ...
- The Linux Kernel
- 用js写水仙花数
...js //输入一个三位数,水仙花数就是个位的三次方+十为的三次方+百位的三次方之和等于本身 console.log('请输入一个三位数:'); let a = readline.questi ...
- css左固定右自适应常用方法
下面是几种方法的公用部分(右自适应也是一样的,换一下方向) html: <div class="demo"> <div class="sidebar&q ...