Java操作MongoDB 之 MongoCollection
1.引入依赖
1.1 普通Maven项目依赖
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.4</version>
</dependency>
1.2 SpringBoot 项目依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
1.3 引入Jar包项目
下载地址:https://repo1.maven.org/maven2/org/mongodb/mongo-java-driver/3.4.3/
2.创建数据库服务连接对象 MongoClient (方式二选一)
2.1 通过认证方式进行连接
1.设置数据库连接信息 ServerAddress(数据库IP,数据库端口号)
List<ServerAddress> adds = new ArrayList<>();
ServerAddress serverAddress = new ServerAddress("localhost", 27017);
adds.add(serverAddress);
2.设置数据库验证信息 MongoCredential.createScramSha1Credential(用户名, 数据库名称, 密码)
List<MongoCredential> credentials = new ArrayList<>();
MongoCredential mongoCredential = MongoCredential.createScramSha1Credential("username", "databaseName", "password".toCharArray());
credentials.add(mongoCredential);
3.连接数据库获取连接对象
MongoClient mongoClient = new MongoClient(adds, credentials);
2.2 不通过认证方式进行连接
1.设置数据库服务连接信息 MongoClient(数据库地址,数据库端口号)
MongoClient mongoClient = new MongoClient("localhost", 27017);
注: MongoClient(host,port)两个参数都可以省略
若省略端口号 new MongoClient(“localhost”);
-------------> 使用默认端口号:27017
若两个都省略 new MongoClient( );
-------------> 使用默认IP:localhost 和 默认端口号:27017
3.创建数据库连接对象 MongoDatabase
1.设置数据库连接信息 mongoClient.getDatabase(数据库名)
MongoDatabase mongoDatabase = mongoClient.getDatabase("demo_db_name");
注: 如果数据库名不存在,会在第一次插入文档时创建
4.获取要操作的集合对象 MongoCollection
1.通过数库连接对象获取集合 mongoDatabase.getCollection(集合名称);
MongoCollection<Document> collection = mongoDatabase.getCollection("demo_collcetion_name");
注: 如果集合名不存在,会在第一次插入文档时创建
5.对集合进行增删改查操作
5.0.基础
文档:MongoDB中存储数据的一个单元
Document document = new Document("name","王某某")
.append("gender", "男")
.append("age", 10);
筛选器:作为过滤器筛选出需要的文档
Bson filter = Filters.eq("age",18);
5.1.插入文档
5.1.1插入一个文档 insertOne()
Document document = new Document("name","王某某").append("gender", "男").append("age", 10);
collection.insertOne(document);
5.1.2插入多个文档 insertMany()
List<Document> list = new ArrayList<>();
for(int i = 0; i < 5; i++){
list.add( new Document("number", i).append("name","王某某"));
}
collection.insertMany(list);
5.2.删除文档
5.2.1删除符合条件的第一个文档 deleteOne()
Bson filter = Filters.eq("age",3);
collection.deleteOne(filter);
5.2.2删除符合条件的全部文档 deleteMany()
Bson filter = Filters.eq("age",3);
collection.deleteMany(filter);
5.3.修改文档
5.3.1修改单个文档 updateOne()
updateOne() 方法,该方法接收两个参数,第一个数据类型为 Bson 的过滤器筛选出需要修改的文档,第二个参数数据类型为 Bson 指定如何修改筛选出的文档。然后修改过滤器筛选出的第一个文档。
//筛选条件
Bson filter = Filters.eq("name", "王某某");
//需改内容
Document document = new Document("$set", new Document("age", 100));
//执行修改
collection.updateOne(filter, document);
5.3.2修改多个文档 updateMany()
updateMany() 方法,该方法接收两个参数,第一个数据类型为 Bson 的过滤器筛选出需要修改的文档,第二个参数数据类型为 Bson 指定如何修改筛选出的文档。然后修改过滤器筛选出的所有文档。
//筛选条件
Bson filter = Filters.eq("name", "王某某");
//需改内容
Document document = new Document("$set", new Document("age", 100));
//执行修改
collection.updateMany(filter, document);
5.4.查询文档
5.4.1查找集合中的所有文档
FindIterable findIterable = collection.find();
MongoCursor cursor = findIterable.iterator();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
5.4.2查找集合中的指定文档
Bson filter = Filters.eq("age", 1);
FindIterable findIterable = collection.find(filter);
MongoCursor cursor = findIterable.iterator();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
5.4.3查找集合中的指定文档中的第一个文档
FindIterable findIterable = collection.find();
Document document = (Document) findIterable.first();
System.out.println(document)

这篇博客详细介绍了如何使用Java操作MongoDB,重点讲解了MongoCollection对象的使用,包括建立数据库连接、获取集合对象以及对集合进行增删改查操作的方法,如insertOne()、deleteOne()、updateOne()等。
1万+

被折叠的 条评论
为什么被折叠?



