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. springMvc-reset风格和对静态资源的管理

    1.所谓rest风格及比较优雅的,没有一大堆后缀的风格 2.对静态资源的管理,及样式.图片等不需要springMvc过滤 代码: 1.在springMvc的配置文件中添加mvc标签 <?xml ...

  2. 将ts文件合并为mp4命令

    cmd: copy/b D:\*.ts D:\new.ts 参考:http://blog.sina.com.cn/s/blog_66b4f1180102uzxs.html

  3. leetcode——1

    1. 题目  Two Sum Given an array of integers, find two numbers such that they add up to a specific targ ...

  4. a low memory warning should only destroy the layer’s bitmap

    https://stablekernel.com/view-controller-in-ios-6/ Some of you may have noticed that your view contr ...

  5. 5.1 Object类型

    创建Object实例的方式有两种 ① 使用new操作符跟Object构造函数 var person = new Object(); person.name = "Tom"; pei ...

  6. 使用PinYin4j,获取汉字的拼音字母

    需要导入的文件 <!-- 引入pinyin4J的依赖 --> <dependency> <groupId>com.belerweb</groupId> ...

  7. 适配iOS10和Xcode8

    1.权限设置 iOS10,访问系统权限需要在info.plist中注册,否则直接crash! 注意,Value值不可为空,否则会被Appstore拒掉! 2.Notification,学习资料 喵神总 ...

  8. linux 下nginx除了首页404的问题

    今天在部署tp5的时候除了首页能访问.其他都是not found 原因是 Nginx服务器默认不支持pathinfo,index.php后面的参数都没带上   在需要pathinfo支持的程序中 则无 ...

  9. win10下安装mysql-5.7.23-winx64

    Step1 官方下载地址 https://dev.mysql.com/downloads/mysql/ 选择手动下载版本 解压到自己指定的路径 上图中的my.ini及data文件夹在压缩包里是没有的, ...

  10. jquery 省市区联动插件

    使用方式: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...