Skip to content

A library to parse mongo document to java object and vice versa

License

Notifications You must be signed in to change notification settings

halab4dev/mongo2j

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub Workflow Status Coverage Status GitHub last commit

Overview & Installation

Mongo2J is a light weight and easy to use library to map mongo document to java object and vice versa.

To start using Mongo2J, add dependency to pom.xml file:

<dependency>
  <groupId>com.github.halab4dev</groupId>
  <artifactId>mongo2j</artifactId>
  <version>2.2.0</version>
</dependency>

Guideline

  • Entity class:
public class Developer {

    @BsonId
    private String id;
    private String name;
    private int age;
    private Address address;
    @BsonProperty("skills")
    private List<String> languages;
...
}


public class Address {

    private int no;
    private String street;
    private String city;
...
}
  • Annotations

    • @BsonId: mark a class attribute as the document id.
    • @BsonProperty: tell the parser to parse this attribute to specific document field (here is skills)
  • Mapping

    //Create java objects
    Address address = new Address();
    address.setNo(808);
    address.setStreet("Nguyen Khuyen");
    address.setCity("Hanoi");

    Developer developer = new Developer();
    developer.setId("507f191e810c19729de860ea");
    developer.setName("halab");
    developer.setAge(25);
    developer.setAddress(address);
    developer.getLanguages().add("Java");
    developer.getLanguages().add("Javasccript");

    //Declare mapper
    Mapper mapper = new DefaultMapper();

    //Map an object to mongo document
    Document document = mapper.toDocument(developer);

The result should be:

{
    "_id" : ObjectId("507f191e810c19729de860ea"),
    "name" : "halab",
    "age" : 25,
    "address" : {
        "no" : 808,
        "street" : "Nguyen Khuyen",
        "city" : "Hanoi"
    },
    "skills" : [ 
        "Java", 
        "Javasccript"
    ]
}

To map a document to java object, just simple:

Developer dev = mapper.toObject(document, Developer.class);

About

A library to parse mongo document to java object and vice versa

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Languages