MongoDB快速入门学习笔记8 MongoDB的java驱动操作
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驱动操作的更多相关文章
- MongoDB快速入门学习笔记7 MongoDB的用户管理操作
1.修改启动MongoDB时要求用户验证加参数 --auth 即可.现在我们把MongoDB服务删除,再重新添加服务 mongod --dbpath "D:\work\MongoDB\dat ...
- MongoDB快速入门学习笔记6 MongoDB的文档删除操作
db.集合名称.remove({query}, justOne)query:过滤条件,可选justOne:是否只删除查询到的第一条数据,值为true或者1时,只删除一条数据,默认为false,可选. ...
- MongoDB快速入门学习笔记5 MongoDB的文档修改操作
db.集合名称.update({query},{update},upsert, multi})query:过滤条件update:修改内容upsert:如果不存在查询条件查出的记录,是否插入一条数据,默 ...
- MongoDB快速入门学习笔记3 MongoDB的文档插入操作
1.文档的数据存储格式为BSON,类似于JSON.MongoDB插入数据时会检验数据中是否有“_id”,如果没有会自动生成.shell操作有insert和save两种方法.当插入一条数据有“_id”值 ...
- MongoDB快速入门学习笔记2 MongoDB的概念及简单操作
1.以下列举普通的关系型数据库和MongoDB数据库简单概念上的区别: 关系型数据库 MongoDB数据库 说明 database database 数据库 table collection 数据库表 ...
- MongoDB快速入门学习笔记4 MongoDB的文档查询操作
先把student删除,再重新插入数据 > db.student.drop() true > db.student.insert([{ "_id" : 1, " ...
- MongoDB快速入门学习笔记1 windows安装MongoDB
1.安装MongoDB 从MongoDB官网上下载MongoDB,我下载的版本是64位的3.2.6.下载完以后直接安装,我的安装目录是D:\work\MongoDB. 2.配置MongoDB的环境变量 ...
- 【原创】SpringBoot & SpringCloud 快速入门学习笔记(完整示例)
[原创]SpringBoot & SpringCloud 快速入门学习笔记(完整示例) 1月前在系统的学习SpringBoot和SpringCloud,同时整理了快速入门示例,方便能针对每个知 ...
- Sass简单、快速上手_Sass快速入门学习笔记总结
Sass是世界上最成熟.稳定和强大的专业级css扩展语言 ,除了Sass是css的一种预处理器语言,类似的语言还有Less,Stylus等. 这篇文章关于Sass快速入门学习笔记. 资源网站大全 ht ...
随机推荐
- No module named 'revoscalepy'问题解决
SqlServer2017开始支持Python,前段时间体验了下,按照微软的入门例子操作的:https://microsoft.github.io/sql-ml-tutorials/python/re ...
- docker使用alpine系统构建tomcat镜像
FROM frolvlad/alpine-oraclejdk8 #此镜像使用alpine-glibc系统,精简jdk,只运行java程序,无法编译/构建 MAINTAINER huqiang:2018 ...
- windows mysql忘记密码解决方案
因为mysql很久之前装的,今天突然想用的时候发现密码不记得,怎一个尴尬了得,所以没办法,只能修改一个新的密码. 在此过程中遇到了几个问题 1.没法进入数据库: 2.修 ...
- 小记:iOS 中一般对于 view 不依赖 model 的的两种代码书写形式
一. 前言 对于在 MVC 的定义中,view 层是不引用 model 层,view 和 model 是不相往来的 一般开发中,我们都写过 在自定义 view 中增加一个 model 的属性,外接直接 ...
- Handling Exceptions
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Exceptions/Tasks/Handling ...
- HTTP 请求方法介绍
浏览器从 web 服务器(或者叫应用服务器)上使用 HTTP 协议下载网站,HTTP 协议是基于一种 请求-响应(request-response)模型的.客户端(你的浏览器)从运行在物理机器上的 w ...
- [论文理解]MetaAnchor: Learning to Detect Objects with Customized Anchors
MetaAnchor: Learning to Detect Objects with Customized Anchors Intro 本文我其实看了几遍也没看懂,看了meta以为是一个很高大上的东 ...
- 查看数据库表存储引擎MyISAM/InnoDB
Mysql: show table status *MyISAM不支持PDO的事务
- opencv使用 findContours
http://www.jb51.net/article/132217.htm https://www.jianshu.com/p/4bc3349b4611 https://blog.csdn.net/ ...
- springboot集成shiro的session污染问题
问题起因是这样的,有两套系统,系统a和系统b.两套系统均使用shiro做的权限管理,之前部署在两台机器上.使用浏览器打开a系统后另开页签打开b系统,互不干扰都能正常使用,后因业务迁移,两套系统部署到了 ...