本文最后更新于:December 3, 2021 pm
MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统。是一个面向文档存储的数据库,操作起来比较简单和容易。在高负载的情况下,添加更多的节点,可以保证服务器性能。MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。
目录
创建一个Maven项目。导入依赖:
| <dependencies> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.12.10</version> </dependency> </dependencies>
|
1.直接连接(无认证连接)
| public static void main(String[] args) { MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase mongoDatabase = mongoClient.getDatabase("loong"); MongoCollection collection = mongoDatabase.getCollection("to");
System.out.println("OK"); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.tothefor;
import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase;
public class MongoDBUtils { private static MongoClient mongoClient = null; static { if(mongoClient==null){ mongoClient = new MongoClient("localhost", 27017); } } public static MongoDatabase getMongoDatabase(String dbName){ return mongoClient.getDatabase(dbName); } public static MongoCollection getCollection(String dbName,String setName){ MongoDatabase dbs = getMongoDatabase(dbName); return dbs.getCollection(setName); } }
|
主函数代码:
| public static void main(String[] args) { MongoCollection mongoCollection = MongoDBUtils.getCollection("loong","to"); System.out.println("OK"); }
|
2.池连方式(无认证连接)
工具类代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package com.tothefor;
import com.mongodb.MongoClient; import com.mongodb.MongoClientOptions; import com.mongodb.ServerAddress; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase;
public class MongoDBPoolUtils { private static MongoClient mongoClient = null; static { if(mongoClient==null){ MongoClientOptions.Builder builder = new MongoClientOptions.Builder(); builder.connectionsPerHost(10); builder.connectTimeout(5000); builder.socketTimeout(5000); ServerAddress address = new ServerAddress("localhost", 27017); mongoClient = new MongoClient(address,builder.build()); } } public static MongoDatabase getMongoDatabase(String dbName){ return mongoClient.getDatabase(dbName); } public static MongoCollection getCollection(String dbName, String setName){ MongoDatabase dbs = getMongoDatabase(dbName); return dbs.getCollection(setName); } }
|
主函数代码:
| public static void main(String[] args) { MongoCollection mongoCollection = MongoDBPoolUtils.getCollection("loong","to"); System.out.println("OK"); }
|
3.池连方式(认证连接)
同无认证连接一样。只不过有一个地方不同而已,其他相同。不同之处:
| static { if(mongoClient==null){ MongoClientOptions.Builder builder = new MongoClientOptions.Builder(); builder.connectionsPerHost(10); builder.connectTimeout(5000); builder.socketTimeout(5000); MongoCredential credential = MongoCredential.createCredential("username","database","password".toCharArray()); ServerAddress address = new ServerAddress("localhost", 27017); mongoClient = new MongoClient(address,credential,builder.build()); } }
|
4.直接连接(认证连接)
同无认证连接一样。只不过有一个地方不同而已,其他相同。不同之处:
| static { if(mongoClient==null){ MongoCredential credential = MongoCredential.createCredential("username","database","password".toCharArray()); ServerAddress address = new ServerAddress("localhost", 27017); mongoClient = new MongoClient(address, Arrays.asList(credential)); } }
|