# Database Management

## Example

```java
public class ExampleMongoProvider implements DatabaseProvider<Example> {

    private final MongoClient client;
    private final String databaseName;
    private MongoCollection<BsonDocument> collection;

    public ExampleMongoProvider(String ip, int port, String username, String password, String databaseName) {
        MongoClientURI clientURI = !username.equals("") ? new MongoClientURI("mongodb://" + ip + ":" + port + "@" + username + ":" + password) : new MongoClientURI("mongodb://" + ip + ":" + port);
        this.client = new MongoClient(clientURI);
        this.databaseName = databaseName;
    }

    @Override
    public void create() {
        MongoDatabase mongoDatabase = this.client.getDatabase(this.databaseName);
        if (!mongoDatabase.listCollectionNames().into(new ArrayList<>()).contains("example"))
            mongoDatabase.createCollection("example");
        this.collection = mongoDatabase.getCollection("example", BsonDocument.class);
    }

    @Nonnull
    @Override
    public List<Example> getValues() {
        List<Example> examples = new ArrayList<>();
        this.collection.find().into(new ArrayList<>())
                .forEach(document -> examples.add(new example(document)));
        return examples;
    }

    @Nonnull
    @Override
    public Example getValue(@Nonnull String key, @Nonnull Object value) {
        String valueString = value.toString();
        BsonDocument document = this.collection.find(new BsonDocument(key, new BsonString(valueString))).first();
        if (document == null)
            throw new IllegalArgumentException("no example with key " + key + " and value " + valueString + " found.");
        return new Example(document);
    }

    @Override
    public void insert(@Nonnull Example example) {
        this.collection.insertOne(Example.toBsonDocument());
    }

    @Override
    public void update(@Nonnull Example example) {
        this.collection.updateOne(this.toSelectDocument(example), new BsonDocument("$set", example.toBsonDocument()));
    }

    @Override
    public void delete(@Nonnull Example example) {
        this.collection.deleteOne(this.toSelectDocument(example));
    }

    @Override
    public void insert(@Nonnull Collection<Example> examples) {
        if (examples.size() == 0)
            return;

        List<WriteModel<BsonDocument>> writes = new ArrayList<>();
        examples.forEach(example -> writes.add(new InsertOneModel<>(example.toBsonDocument())));
        this.collection.bulkWrite(writes);
    }

    @Override
    public void update(@Nonnull Collection<Example> examples) {
        if (examples.size() == 0)
            return;

        List<WriteModel<BsonDocument>> writes = new ArrayList<>();
        examples.forEach(example -> writes.add(new UpdateOneModel<>(this.toSelectDocument(examples), new BsonDocument("$set", example.toBsonDocument()))));
        this.collection.bulkWrite(writes);
    }

    @Override
    public void delete(@Nonnull Collection<Example> examples) {
        if (examples.size() == 0)
            return;

        List<WriteModel<BsonDocument>> writes = new ArrayList<>();
        examples.forEach(example -> writes.add(new DeleteOneModel<>(this.toSelectDocument(example))));
        this.collection.bulkWrite(writes);
    }


    /*
    Converter from Example.class to document.
     */
    private BsonDocument toSelectDocument(Example example) {
        return new BsonDocument(ExampleMongoField.UID.getPath(), ExampleMongoField.UID.getValue(example));
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hcore.gitbook.io/wiki/others/database-management.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
