import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern; import org.bson.Document; import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase; public class MongoZyh { public static void main(String[] args) { try {
// 连接到MongoDB服务,ServerAddress()两个参数分别为 服务器地址 和 端口
ServerAddress serverAddress = new ServerAddress("localhost", 27017);
List<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add(serverAddress); // 三个参数分别为 用户名 数据库名称 密码
MongoCredential credential = MongoCredential
.createScramSha1Credential("zyh", "admin",
"zyh".toCharArray());
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential); // 通过连接认证获取MongoDB连接
MongoClient mongoClient = new MongoClient(addrs, credentials); // 连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("zyhdb"); // 新建集合,执行后会在数据库里新建一个空的集合
// mongoDatabase.createCollection("student");
// System.out.println("新建集合成功"); // 获取集合,并往集合中插入数据
MongoCollection<Document> mongoCollection = mongoDatabase
.getCollection("student"); // 插入一条数据
// Document document = new Document();
// document.append("name", "zhangsan");
// document.append("age", 28);
// mongoCollection.insertOne(document);
// System.out.println("插入一条数据成功"); // 插入多条数据
// List<Document> documentList = new ArrayList<Document>();
// Document document1 = new Document();
// document1.append("name", "lisi");
// document1.append("age", 28);
// document1.append("sex", "男");
// Document document2 = new Document();
// document2.append("name", "wangwu");
// document2.append("age", 31);
// document2.append("sex", "男");
// documentList.add(document1);
// documentList.add(document2);
// mongoCollection.insertMany(documentList);
// System.out.println("插入多条数据成功"); // 查询数据
// 查询集合中所有的数据
// FindIterable<Document> findIterable = mongoCollection.find();
// MongoCursor<Document> mongoCursor = findIterable.iterator();
// while (mongoCursor.hasNext()) {
// System.out.println(mongoCursor.next());
// } // 根据条件查询
// Document query = new Document();
// query.put("age", new Document("$lt", 30));
// query.put("sex", "男");
// query.put("name", query); // 正则表达式查询
// Pattern pattern = Pattern.compile("^zhang");
// query.put("name", pattern); // 排序
// Document sort = new Document();
// sort.put("name", -1); // 1是正序,-1是倒序 // FindIterable<Document> findIterable = mongoCollection.find(query)
// .sort(sort);
// MongoCursor<Document> mongoCursor = findIterable.iterator();
// while (mongoCursor.hasNext()) {
// Document doc = mongoCursor.next();
// System.out.print("name:" + doc.get("name") + "...");
// System.out.print("age:" + doc.get("age") + "...");
// System.out.println("sex:" + doc.get("sex") + "...");
// } // mongoCollection.findOneAndUpdate(查询条件, 修改内容); // 查询出第一条数据并修改
// mongoCollection.findOneAndDelete(查询条件); // 查询出第一条数据并删除
// mongoCollection.findOneAndReplace(查询条件, 替换内容); // 查询出第一条数据并替换 // 修改数据
// Document query = new Document();
// query.put("age", 28);
// Document update = new Document();
// Document d = new Document();
// d.put("birthday", new Date());
// d.put("name", "zhangsan");
// update.put("$set", d);
// mongoCollection.updateOne(query, update); // 修改查询到的第一条数据
// mongoCollection.updateMany(查询条件, 修改内容);// 修改查询到的所有数据 // 删除数据
// Document query = new Document();
// query.put("age", 28);
// mongoCollection.deleteOne(query); // 删除查询到的第一条数据
// mongoCollection.deleteMany(查询条件); // 删除查询到的所有数据 // mongoCollection.drop(); // 删除集合 } catch (Exception e) {
e.printStackTrace();
} }
}

MongoDB快速入门学习笔记8 MongoDB的java驱动操作的更多相关文章

  1. MongoDB快速入门学习笔记7 MongoDB的用户管理操作

    1.修改启动MongoDB时要求用户验证加参数 --auth 即可.现在我们把MongoDB服务删除,再重新添加服务 mongod --dbpath "D:\work\MongoDB\dat ...

  2. MongoDB快速入门学习笔记6 MongoDB的文档删除操作

    db.集合名称.remove({query}, justOne)query:过滤条件,可选justOne:是否只删除查询到的第一条数据,值为true或者1时,只删除一条数据,默认为false,可选. ...

  3. MongoDB快速入门学习笔记5 MongoDB的文档修改操作

    db.集合名称.update({query},{update},upsert, multi})query:过滤条件update:修改内容upsert:如果不存在查询条件查出的记录,是否插入一条数据,默 ...

  4. MongoDB快速入门学习笔记3 MongoDB的文档插入操作

    1.文档的数据存储格式为BSON,类似于JSON.MongoDB插入数据时会检验数据中是否有“_id”,如果没有会自动生成.shell操作有insert和save两种方法.当插入一条数据有“_id”值 ...

  5. MongoDB快速入门学习笔记2 MongoDB的概念及简单操作

    1.以下列举普通的关系型数据库和MongoDB数据库简单概念上的区别: 关系型数据库 MongoDB数据库 说明 database database 数据库 table collection 数据库表 ...

  6. MongoDB快速入门学习笔记4 MongoDB的文档查询操作

    先把student删除,再重新插入数据 > db.student.drop() true > db.student.insert([{ "_id" : 1, " ...

  7. MongoDB快速入门学习笔记1 windows安装MongoDB

    1.安装MongoDB 从MongoDB官网上下载MongoDB,我下载的版本是64位的3.2.6.下载完以后直接安装,我的安装目录是D:\work\MongoDB. 2.配置MongoDB的环境变量 ...

  8. 【原创】SpringBoot & SpringCloud 快速入门学习笔记(完整示例)

    [原创]SpringBoot & SpringCloud 快速入门学习笔记(完整示例) 1月前在系统的学习SpringBoot和SpringCloud,同时整理了快速入门示例,方便能针对每个知 ...

  9. Sass简单、快速上手_Sass快速入门学习笔记总结

    Sass是世界上最成熟.稳定和强大的专业级css扩展语言 ,除了Sass是css的一种预处理器语言,类似的语言还有Less,Stylus等. 这篇文章关于Sass快速入门学习笔记. 资源网站大全 ht ...

随机推荐

  1. RStudio Server-0.99.902 (OpenLogic CentOS 7.2)

    RStudio Server-0.99.902 (OpenLogic CentOS 7.2) 0 评论 平台: CentOS 类型: 虚拟机镜像 软件包: r-3.2.3 rstudio-server ...

  2. 实战:ADFS3.0单点登录系列-集成SharePoint

    这是本系列第四篇了,终于轮到SharePoint上场了,但是本文不会过多讲解SharePoint安装等话题,而是直入主题,讲解如何进行配置,让其于ADFS配合完成SSO的工作. 注意:本文使用的Sha ...

  3. Python参数基础

    Python参数基础 位置参数 ​ 通过位置进行匹配,把参数值传递给函数头部的参数名称,顺序从左到右 关键字参数 ​ 调用的时候使用参数的变量名,采用name=value的形式 默认参数 ​ 为没有传 ...

  4. IOS NSNotificationCenter(通知 的使用)监听文本框的文字改变

    监听文本框的文字改变 * 一个文本输入框的文字发生改变时,文本输入框会发出一个UITextFieldTextDidChangeNotification通知 * 因此通过监听通知来监听文本输入框的文字改 ...

  5. js 封装父页面子页面交互接口

    定义标准接口 Interface= {}; Interface.ParentWin = {}; Interface.ChildWin = {}; /** * 父页面提供的标准接口函数名称 */ Int ...

  6. fopen, fdopen, freopen - 打开流

    SYNOPSIS (总览) #include <stdio.h> FILE *fopen(const char *path, const char *mode); FILE *fdopen ...

  7. 使用pip 提示UnicodeDecodeError: 'ascii' codec can't decode解决方法

    python目录 Python27\Lib\site-packages 建一个文件sitecustomize.py 内容写: import sys sys.setdefaultencoding('gb ...

  8. java基础编程——获取栈中的最小元素

    题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)).   题目代码 /** * Created by YuKai Fan on 2018/9 ...

  9. 自建ssr(谷歌云免费试用一年)

    近期我一个朋友的VPN到期了,他也不想再去续费,同时发现谷歌云第一年申请时是免费的,所以他就自己搭建了一个自己专属的VPN 以下是他的搭建教程:  本教程难点在于申请免费试用资格 谷歌云+ssr搭建免 ...

  10. java基础面试题:写clone()方法时,通常都有一行代码,是什么?

    clone()方法 与new constructor()构造器创建对象不同 是克隆一个新的对象 package com.swift; public class Clone_Test { public ...