有MySQL了,为什么还硬要搭个MongoDB集群?( 三 )


在mongodb/bin目录下,建一个mongod.cfg文件,写入
systemLog:
destination: file
logAppend: true
path: D:Program Filesmongodbdatalogsmongod.log
storage:
dbPath: D:Program Filesmongodbdatadb
执行mongod --config "D:Program Filesmongodbbinmongod.cfg" --install 命令,安装MongoDB 。
通过mongod --version检查MongoDB版本 。
D:Program Filesmongodbbin>mongod --version
db version v5.0.14
Build Info: {
"version": "5.0.14",
"gitVersion": "1b3b0073a0b436a8a502b612f24fb2bd572772e5",
"modules": [],
"allocator": "tcmalloc",
"environment": {
"distmod": "windows",
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
2、mongodb由于目标计算机积极拒绝,无法连接

有MySQL了,为什么还硬要搭个MongoDB集群?

文章插图
突然间,mongodb无法连接了?mongod.exe --dbpath "D:Program Filesmongodbdata完美解决 。
注意一点,在重新启动时,执行mongod.exe --dbpath "D:Program Filesmongodbdata的窗口不要关闭 。
有MySQL了,为什么还硬要搭个MongoDB集群?

文章插图
3、由于找不到vcruntime140_1.dll,无法继续执行代码
有MySQL了,为什么还硬要搭个MongoDB集群?

文章插图
下载vcruntime140_1.dll文件;
将vcruntime140_1.dll文件拷贝到C:WindowsSystem32即可 。
有MySQL了,为什么还硬要搭个MongoDB集群?

文章插图
七、Java整合MongoDB,实现农民工增删改查
加入POM
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.8.2</version>
</dependency>
MongoDBUtil工具类
package com.example.demo.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.bson.Document;
import org.bson.conversions.Bson;
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;
import com.mongodb.client.model.Filters;
public class MongoDBUtil {
private static MongoClient mongoClient;
private static MongoClient mongoClientIdentify;
/**
* 不通过认证获取连接数据库对象
*/
public static MongoDatabase getNoIdentifyConnect(String host, int port, String dbaseName) {
// 连接mongodb服务
MongoDBUtil.mongoClient = new MongoClient(host, port);
// 连接数据库
MongoDatabase mongoDatabase = MongoDBUtil.mongoClient.getDatabase(dbaseName);
// 返回连接数据库对象
return mongoDatabase;
}
/**
* 通过连接认证获取MongoDB连接
*/
public static MongoDatabase getIdentifyConnect(String host, int port, String dbaseName, String userName, String password) {
List<ServerAddress> adds = new ArrayList<ServerAddress>();
ServerAddress serverAddress = new ServerAddress(host, port);
adds.add(serverAddress);
List<MongoCredential> credentials = new ArrayList<>();
MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(userName, dbaseName, password.toCharArray());
credentials.add(mongoCredential);
// 通过连接认证获取MongoDB连接
MongoDBUtil.mongoClientIdentify = new MongoClient(adds, credentials);
MongoDatabase mongoDatabase = MongoDBUtil.mongoClientIdentify.getDatabase(dbaseName);
return mongoDatabase;
}
/**
* 关闭连接
*/
public static void closeNoIdentifyConnect () {
MongoDBUtil.mongoClient.close();
}
/**
* 关闭连接
*/
public static void closeIdentifyConnect () {
MongoDBUtil.mongoClientIdentify.close();
}
/**
* 插入一个文档
*/
public static void insertOne (Map<String, Object> data, MongoDatabase mongoDatabase, String col) {
//获取集合
MongoCollection<Document> collection = mongoDatabase.getCollection(col);
//创建文档
Document document = new Document();
for (Map.Entry<String, Object> m : data.entrySet()) {
document.append(m.getKey(), m.getValue()).append(m.getKey(), m.getValue());
}
//插入一个文档
collection.insertOne(document);
}
/**
* 插入多个文档
*/
public static void insertMany (List<Map<String, Object>> listData, MongoDatabase mongoDatabase, String col) {


推荐阅读