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上面的应用的更多相关文章

  1. 在Eclipse中设置Java类上面的注释(包含作者、日期等)

    希望在Eclipse中,让Java类上面的注释像下面这样,改如何设置呢? 在Eclipse中,点击菜单中的Window-->Preferences,打开如下窗口:

  2. 从上面的集合框架图可以看到,Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射

    从上面的集合框架图可以看到,Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射.Collection 接口又有 3 ...

  3. mongodb与java整合

    mongodb与java整合需要用到mongodb驱动,如果是maven环境,则添加如下倚赖: <dependency> <groupId>org.mongodb</gr ...

  4. MongoDB的Java驱动使用整理 (转)

    MongoDB Java Driver 简单操作 一.Java驱动一致性 MongoDB的Java驱动是线程安全的,对于一般的应用,只要一个Mongo实例即可,Mongo有个内置的连接池(池大小默认为 ...

  5. 在MongoDB的MapReduce上踩过的坑

    太久没动这里,目前人生处于一个新的开始.这次博客的内容很久前就想更新上来,但是一直没找到合适的时间点(哈哈,其实就是懒),主要内容集中在使用Mongodb时的一些隐蔽的MapReduce问题: 1.R ...

  6. 【MongoDB for Java】Java操作MongoDB

    上一篇文章: http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html介绍到了在MongoDB的控制台完成MongoDB的数据操作,通过 ...

  7. edtftpj让Java上传FTP文件支持断点续传

    在用Java实现FTP上传文件功能时,特别是上传大文件的时候,可以需要这样的功能:程序在上传的过程中意外终止了,文件传了一大半,想从断掉了地方继续传:或者想做类似迅雷下载类似的功能,文件太大,今天传一 ...

  8. [转]MongoDB for Java】Java操作MongoDB

    原文地址: MongoDB for Java]Java操作MongoDB 开发环境: System:Windows IDE:eclipse.MyEclipse 8 Database:mongoDB 开 ...

  9. mongodb在java驱动包下的操作(转)

    推荐几章很有用的文章 java操作参考文档 http://www.cnblogs.com/hoojo/archive/2011/06/02/2068665.html http://blog.csdn. ...

随机推荐

  1. 阅读SessionFactory源码

    一.阅读类注释 ①.SessionFactory的主要任务是创建Session的实例.通常一个应用程序只有一个单一的SessionFactory对象,而且线程从这个SessionFactory中获取S ...

  2. 牛客网多校训练第一场 F - Sum of Maximum(容斥原理 + 拉格朗日插值法)

    链接: https://www.nowcoder.com/acm/contest/139/F 题意: 分析: 转载自:http://tokitsukaze.live/2018/07/19/2018ni ...

  3. UVa 10820 - Send a Table(欧拉函数)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. PHP中的 array_filter 函数

    <?php function test_odd($var) { return($var & 1); } $a1=array("a","b",2,3 ...

  5. win8安装wampserver报403错误解决方法

    看着别人开始体验win8了,前几天我也安装了win8系统,总体来说还不错,但是今天安装完Wampserver后,浏览器输入localhost,竟然报了403错误,我以为我安装出错了,后来研究了半天,发 ...

  6. Linux下的压缩解压缩命令

    *.tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— *.gz ...

  7. XML解析方式

    两种解析方式概述 dom解析 (1)是W3C组织推荐的处理XML的一种解析方式. (2)将整个XML文档使用类似树的结构保存在内存中,在对其进行操作. (3)可以方便的对XML进行增删改查的操作 (4 ...

  8. 时间、时间戳相关小结 - iOS

    项目中难免会与时间打交道,故此次围绕时间展开做了一些日常使用的小结;如下 code 中也是围绕一些日常开发中较为常用的点展开小的方法封装. 具体方法的使用如下: // 2019-02-21 17:30 ...

  9. c#一个日志类(log4net)

    这个类就是对log4net的使用,就不多说了,但是看见网上的一个封装,自己用了下,感觉还不错,直接记录在这里.把自己使用的类直接贴出来. using log4net; using log4net.Co ...

  10. #leetcode刷题之路7- 整数反转

    给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1:输入: 123输出: 321 示例 2:输入: -123输出: -321 示例 3:输入: 120输出: 21 #i ...