Coder Social home page Coder Social logo

mongo-mapper's Introduction

mongo-mapper Build Status

Mapping POJO for MongoDB has not been easier. Thanks to the new codecs feature in MongoDB Java 3.0 driver. Simply mark your entities with annotation, create EntityCodec and that's it! Then use standard methods for storing and accessing data from MongoDB.

Why us it?

  • Simple and easy to use.
  • Use standard (MongoDB) way for object manipulation.
  • Works for synchronous as well as asynchronous version of MongoDB Java Driver.
  • You can extend with your own custom codecs.
  • It's fast and small - only 13kB dependency covered by unit and integration tests.

Installation

Mongo mapper is on Maven Central. Add following into your pom.xml.

Maven
<dependency>
    <groupId>eu.dozd</groupId>
    <artifactId>mongo-mapper</artifactId>
    <version>1.x.x</version>
</dependency>

Maven Central

Gradle
compile 'eu.dozd:mongo-mapper:1.x.x'

Usage

  1. Annotate your entities with Entity. Make sure every entity has exactly one String annotated with Id. All properties must have correct getter and setter methods according Java Bean specification.

    import eu.dozd.mongo.annotation.Entity;
    import eu.dozd.mongo.annotation.Id;
    
    @Entity
    public class Person {
        @Id
        String id;
        String name;
        int age;
    
        public String getId() { return id; }
        public void setId(String id) { this.id = id; }
        
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        
        public int getAge() { return age; }
        public void setAge(int age) { this.age = age; }
    }
  2. Create mapper CodecProvider by calling MongoMapper.getProviders.

    CodecRegistry codecRegistry = CodecRegistries.fromProviders(MongoMapper.getProviders());
    • Usage for standard driver:

          MongoClientOptions settings = MongoClientOptions.builder().codecRegistry(codecRegistry).build();
      
          MongoClient client = new MongoClient(new ServerAddress("localhost", 27017), settings);
    • Usage for asynchronous driver:

          ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Arrays.asList(new ServerAddress("localhost", 27017))).build();
          MongoClientSettings settings = MongoClientSettings.builder().codecRegistry(codecRegistry)
                                              .clusterSettings(clusterSettings).build();
          
          MongoClient client = MongoClients.create(settings);
  3. Access and store data like normal POJO.

        MongoCollection<Person> collection = db.getCollection("persons", Person.class);
    
        Person person = new Person();
        person.setName("Foo Bar");
    
        // Store person normally.
        collection.insertOne(person);
    
        // Access data.
        Person person2 = collection.find.first()

Features

  • Entity reference - make sure all entities classes are annotated with Entity.
  • Embedded entities - entities annotated with Embedded does not need to have an ID.
  • @java.beans.Transient - annotated getter with it.
  • Feel free to create issue or pull request if you missing some functionality.

Custom codecs

  • You can create other Codecs for you special classes.
  • Added BigDecimalCodec and BigDecimalCodecProvider as an example.
  • Don't forget to call MongoMapper.addProvider(yourCustomCodecProvider).

Licence

Copyright 2016 Zdenek Dolezal

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

See also

mongo-mapper's People

Contributors

dozd avatar mmithril avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.