Skip to content

Latest commit

 

History

History
92 lines (69 loc) · 2.75 KB

README.md

File metadata and controls

92 lines (69 loc) · 2.75 KB

🚀 MongoDB Module

Morphia License

This module provides a convenient encapsulation of MongoConfig based on Morphia. It simplifies the implementation of MongoDB operations.

📦 Features

  • Datastore Operations: Use Datastore directly for CRUD operations.
  • Index Creation: Utilize Morphia for creating indexes.
  • Aggregation: Perform aggregations using Morphia Aggregations.

🗃️ Cache

The first and second level caches are implemented in the cache module.

📋 Usage

Gradle Dependency

dependencies {
    compileOnly(files("libs/mongodb-1.0-SNAPSHOT.jar"))
}

Example Code

We recommend using MongoDBConnectionFactory to create connections instead of instantiating objects directly.

public class Example {
    public static void main(String[] args) {
        // Create test objects
        Person person = new Person();
        person.setUuid(UUID.randomUUID());
        person.setName("Alice");
        person.setAge(20);

        Person person2 = new Person();
        person2.setUuid(UUID.randomUUID());
        person2.setName("Alice");
        person2.setAge(45);

        // Create connection
        MongoDBConnectionConfig mongoConfig = MongoDBConnectionConfigFactory.create(
                "example", "mongodb://localhost:27017/", UuidRepresentation.STANDARD
        );

        // Get datastore
        Datastore datastore = mongoConfig.getDatastore();

        // Save objects
        datastore.save(person);
        datastore.save(person2);

        // Find objects (thread-safe)
        @Cleanup
        MorphiaCursor<Person> iterator = datastore.find(Person.class)
                .filter(Filters.and(
                        Filters.eq("_id", "2135ef6b-8915-4d4c-a677-b1f5482ed2aa"), // _id eq
                        Filters.eq("name", "Alice"), // name eq
                        Filters.gt("age", 35) // age > 35
                ))
                .iterator(); // Get iterator

        // Print results
        for (Person person1 : iterator.toList()) {
            System.out.println(person1.getAge());
        }
    }
}

@Data
@Entity("persons")
class Person {
    @Id
    private UUID uuid;
    private String name;
    private int age;
}

🔗 Related Links

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.