Mariadb   发布时间:2022-05-23  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了MongoDB快速入门笔记(八)之MongoDB的java驱动操作代码讲解大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_751_0@mongoDB的Java驱动是线程安全的,对于一般的应用,只要一个Mongo实例即可,Mongo有个内置的连接池(池大小默认为10个)。

下面代码给大家介绍MongoDB的java驱动操作,具体代码如下所示:

div class="codecode">
l.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 addrs = new ArrayList();
addrs.add(serverAddress);
// 三个参数分别为 用户名 数据库名称 密码
MongoCredential credential = MongoCredential
.createScramSha1Credential("zyh","admin","zyh".tochararray());
List credentials = new ArrayList();
credentials.add(credential);
// 通过连接认证获取MongoDB连接
MongoClIEnt mongoClIEnt = new MongoClIEnt(addrs,credentials);
// 连接到数据库
MongoDatabase mongoDatabase = mongoClIEnt.getDatabase("zyhdb");
// 新建集合,执行后会在数据库新建一个空的集合
// mongoDatabase.createCollection("student");
// System.out.println("新建集合成功");
// 获取集合,并往集合中插入数据
MongoCollection mongoCollection = mongoDatabase
.getCollection("student");
// 插入一条数据
// document document = new document();
// document.append("name","zhangsan");
// document.append("age",28);
// mongoCollection.insertOne(document);
// System.out.println("插入一条数据成功");
// 插入多条数据
// List documentList = new ArrayList();
// 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 findIterable = mongoCollection.find();
// Mongocursor 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 findIterable = mongoCollection.find(query)
// .sort(sort);
// Mongocursor 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快速入门笔记(八)之MongoDB的java驱动操作代码讲解全部内容,希望文章能够帮你解决MongoDB快速入门笔记(八)之MongoDB的java驱动操作代码讲解所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:非常不错