Skip to content
This repository has been archived by the owner on Jul 24, 2019. It is now read-only.

Simple ODM for MongoDB java driver based on MongoDB Codec API

Notifications You must be signed in to change notification settings

pksprojects/mongodb-codec-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongodb-codec-generator Build Status

Simple MongoDB Codec generator allowing easy object-document mapping.

Include in your project through Maven:

<dependency>
  <groupId>org.pksprojects.mongodb</groupId>
  <artifactId>mongodb-codec-generator</artifactId>
  <version>1.0-RC0</version>
</dependency>

Configure MongoClient as follows:

// project root directory
String path = "com.example.myproject";

// Get generated CodecRegistry
CodecRegistry generatedRegistry = new CodecGenerator(path).getCodecRegistry();

// Add generated codec registry to default codec registry
CodecRegistry registry = CodecRegistries.fromRegistries(
                MongoClients.getDefaultCodecRegistry(),
                registry);

//Setting for MongoDB Cluster
ClusterSettings clusterSettings = ClusterSettings.builder()
                .hosts(asList(new ServerAddress("localhost")))
                .description("Mongo Server")
                .build();

// Creating MongoDB Client Settings
MongoClientSettings settings = MongoClientSettings.builder()
                .clusterSettings(clusterSettings)
                .codecRegistry(codecRegistry) // register generated codec with settings
                .build();

// Create MongoDB Client
MongoClient client = MongoClients.create(settings);

Example usage with class

// Class to store in MongoDB.
@Document
class User{
    @Id // field will be stored as "_id"
    String id;
    String name;
    @Ignore // field will not be stored in mongo
    String ignored;
    
    User(String name){
        this.name = name;
    }
}

Example mongo operations.

// Get Database
MongoDatabase db = client.getDatabase("mydb");

//Get Collection
MongoCollection<User> collection = db.getCollection("users", User.class);

User testUser = new User("test");

// Save User
collection.insertOne(testUser);

// Get User
User user = collection.find(Filters.eq("name": "test")).first();

About

Simple ODM for MongoDB java driver based on MongoDB Codec API

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages