简体   繁体   中英

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. 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.

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.

I don't know if it matters, but im using the asynchronous mongodb java driver.

    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. If the collection doesn't exist, MongoDB creates it as part of write operations.

In MongoDB, a database is created when you create a collection or have some data inserted into a collection. 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. If the database doesn't exists it is not created. If it exists you get access to any existing collections in it. Assuming there is no database called as "testDB1", the following code creates a database and a collection.

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.

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.

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.

I remembered that there is a HUUUGE difference between the mongo-db synchronous and asynchronous API.

When you execute getDatabase() in the synchronous API it will automatically create it for you if it returns null.

Synchronous API:

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

Asynchronous API:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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