简体   繁体   English

MongoDB 删除所有数据库,除了 admin config 和 local

[英]MongoDB deletes all databases, besides admin config and local

I have a biiig problem.我有一个问题。 I want to use MongoDB with my Java Stuff, but i don't know why it keeps deleting all other databases besides admin, config and local.我想将 MongoDB 与我的 Java Stuff 一起使用,但我不知道为什么它会不断删除除 admin、config 和 local 之外的所有其他数据库。 I am using it currently on my local server.我目前在我的本地服务器上使用它。 I already checked my code, but there is no delete in there.我已经检查了我的代码,但那里没有删除。

I am making a minecraft plugin, which connects to the database and creates 2 collections.我正在制作一个 minecraft 插件,它连接到数据库并创建 2 个 collections。

Okay, I have found the problem.好的,我找到了问题所在。 The database gets created, but gets instantly deleted because its empty.数据库已创建,但由于其为空而立即被删除。 But Im wondering why, because, as you see, i am creating the two collections in it.但我想知道为什么,因为如您所见,我正在其中创建两个 collections。

I don't know if it matters, but im using the asynchronous mongodb java driver.我不知道这是否重要,但我使用异步 mongodb java 驱动程序。

    private final String hostName;
private final String port;

private MongoClient client;
private MongoDatabase database;

private MongoCollection<Document> playerCollection, statsCollection;

public MongoManager(String hostName, String port) {
    this.hostName = hostName;
    this.port = port;
}

public void connect() {
    this.client = MongoClients.create(new ConnectionString(MessageFormat.format("mongodb://{0}:{1}", hostName, port)));

    this.database = this.client.getDatabase("prod");
    this.playerCollection = this.database.getCollection("players");
    this.statsCollection = this.database.getCollection("stats");
}

The collection name specified on getCollection method may or may not exist on the mongodb.getCollection方法上指定的集合名称可能存在也可能不存在于 mongodb 上。 If the collection doesn't exist, MongoDB creates it as part of write operations.如果该集合不存在,MongoDB 将创建它作为写入操作的一部分。

In MongoDB, a database is created when you create a collection or have some data inserted into a collection.在 MongoDB 中,当您创建集合或将一些数据插入到集合中时,会创建一个数据库。 Here is some code to demonstrate that.这是一些代码来证明这一点。

(1) The getDatabase method doesn't create a database, but "gets access" to a database named testDB1 , whether it exists are not. (1) getDatabase方法不创建数据库,而是“访问”名为testDB1的数据库,不管它是否存在。 If the database doesn't exists it is not created.如果数据库不存在,则不会创建它。 If it exists you get access to any existing collections in it.如果存在,您可以访问其中的任何现有 collections。 Assuming there is no database called as "testDB1", the following code creates a database and a collection.假设没有名为“testDB1”的数据库,以下代码将创建一个数据库和一个集合。

try(MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017/")) {

    MongoDatabase database = mongoClient.getDatabase("testDB1");
    database.createCollection("testColl");
}

(2) Create a new database by inserting documents into a collection in that database. (2) 通过将文档插入该数据库中的集合来创建一个新数据库。

MongoDatabase database = mongoClient.getDatabase("testDB2");
MongoCollection<Document> coll = database.getCollection("testColl");

Document newDoc = Document.parse("{ 'name': 'Mongo' }");
coll.insertOne(newDoc);
System.out.println(coll.find().first().toJson());

NOTE:笔记:

As of MongoDB Java Driver version 3.9, MongoDB Async Java Driver Documentation says that the callback-based Async Java Driver has been deprecated in favor of the MongoDB Reactive Streams Java Driver. As of MongoDB Java Driver version 3.9, MongoDB Async Java Driver Documentation says that the callback-based Async Java Driver has been deprecated in favor of the MongoDB Reactive Streams Java Driver.

Okay people.好的人。 I am really thankful for all of you who have tried to answer my question.我真的很感谢所有试图回答我问题的人。 After I have read the answer of @prasad_ my brain finally started to work again.在我阅读了@prasad_ 的答案后,我的大脑终于又开始工作了。

I remembered that there is a HUUUGE difference between the mongo-db synchronous and asynchronous API.我记得mongo-db同步和异步API之间有一个巨大的区别。

When you execute getDatabase() in the synchronous API it will automatically create it for you if it returns null.当您在同步 API 中执行 getDatabase() 时,如果它返回 null,它将自动为您创建它。

Synchronous API:同步 API:

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>LATEST</version>
        <scope>compile</scope>
    </dependency>

Asynchronous API:异步 API:

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver-async</artifactId>
        <version>LATEST</version>
        <scope>compile</scope>
    </dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM