MongoDB快速入门学习笔记8 MongoDB的java驱动操作

时间:2022-05-12 21:38:22
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, &quot ...

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

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

  8. 【原创】SpringBoot &amp&semi; SpringCloud 快速入门学习笔记&lpar;完整示例&rpar;

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

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

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

随机推荐

  1. 安装最新版本的PHPUnit后,不能使用

    我使用的是widows系统.本来3.7.8版本的Phpunit用的是非常顺畅的,最近重新安装phpunit,安装了最小版本,然后在使用的时候就会报很多各种错误.无奈之下只能降版本到3.7.8 首先要卸 ...

  2. git 远程仓库-github

    第1步:创建SSH Key.在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步.如果没有,打开Shell ...

  3. CSS 伪类 &lpar;Pseudo-classes&rpar;

    CSS 伪类用于向某些选择器添加特殊的效果. CSS 伪类 (Pseudo-classes)实例: 超链接 本例演示如何向文档中的超链接添加不同的颜色. 超链接 2 本例演示如何向超链接添加其他样式. ...

  4. Solution&colon; Win 10 和 Ubuntu 16&period;04 LTS双系统&comma; Win 10 不能从grub启动

    今年2月份在一台装了Windows的机器上装了Unbuntu 14.04 LTS (双系统, dual-boot, 现已升级到 16.04 LTS). 然而开机时要从grub启动 Windows (选 ...

  5. 第二百五十二天 how can I 坚持

    明天就要去旅游了...还不知道去哪呢,只知道要滑雪,要泡温泉,还要去西柏坡..哈哈. 其他没什么了吧.只是昨晚刷的鞋还没干,不知道明天会不会干,明天还得早走会,九点之前就得到. 还不知道坐车坐多长时间 ...

  6. cin、cin&period;get&lpar;&rpar;、cin&period;getline&lpar;&rpar;、getline&lpar;&rpar;、gets&lpar;&rpar;等函数的用法

    学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结,为了自己复习,也希望对后来者能有所帮助,如果有差错的地方还请各位多多指教(本文所有程序均通过VC 6.0运行)转载请保留作者信息:1.cin1 ...

  7. Ubuntu16&period;04安装搜狗输入法后有黑边问题的解决方法

    apt-get install compton compton -b

  8. mysql系列博客

    近期,打算开始我的sql之路了,计划写一些sql方面的博客,初步定的计划,先写mysql,如果有必要,再写oracle.mysql如下: 1.mysql的调优思路 2.mysql 的执行过程 http ...

  9. TCP&sol;IP&lowbar;网络基础知识

    今天看到k8s的网络,顿感网络知识不是特别扎实,立马回头补一下Tcp-ip知识,顺便记录下学习的过程: 计算机与网络发展的7个阶段: 批处理时代(计算机按照顺序处理,50年代)->分时系统时代( ...

  10. 浅谈 String 的 hashCode&lpar;&rpar; 方法

    Java 中 hash 值的含义 hash 值主要是用来在散列存储结构中确定对象的存储地址的,提高对象的查询效率,如HashMap.HashTable等: 如果两个对象相同,那么这两个对象的 hash ...