Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jongo vs (DBObject)JSON.parse

I'm trying to figure out the advantage of Jongo over simply unmarshalling the json command using (DBObject)JSON.parse(...) and using the DBObject in the below fashion.

Is there a performance advantage?

    @Override
public List<T> getEntityList(Integer limit, String query) throws Exception {
    log.entering(DaoImpl.class.toString(), "getEntityList, with criteria of " + query);
    DBObject criteriaObject = null;
    ArrayList<T> list = new ArrayList<T>();

    if (query != null)
        criteriaObject = (DBObject)JSON.parse(query);

    DBCursor cursor = null;

    try {
        if (criteriaObject != null) {
            log.log(Level.FINEST, "getting the objects using a search criteria: " + criteriaObject);
            cursor = MongoDB.getInstance().getCollection(collection).find(criteriaObject);
        } else {
            log.log(Level.FINEST, "getting the objects without a criteria");
            cursor = MongoDB.getInstance().getCollection(collection).find();
        }

        ............etc, etc, etc

Thanks!

like image 906
Vladimir Avatar asked Oct 22 '22 19:10

Vladimir


2 Answers

Jongo .3 unmarshalls Mongo query with the same JSON.parse(query). The advantage is the way you get the results from the database. In your example, you have to iterate through a cursor, adapting every property and sub property by yourself.

DBObject dbo = JSON.parse("{age: 18}");
DBCursor results = users.find(dbo);
for (DBObject result : results) {
    User user = new User();
    user.setUsername((String) result.get("username"));
    user.setAge((Integer) result.get("age"));
    user.setAddress(new Address(..));
}

With Jongo you directly manipulate objects:

Iterable<User> users = collection.find("{age: 18}").as(User.class);

Jongo's performance is nearly equal to the driver's.

like image 121
yves amsellem Avatar answered Nov 02 '22 06:11

yves amsellem


Here is a list of few advantages to use jongo:

  • Almost all queries can be templated :

    friends.find("{name:#, age:#}", "Joe", 18)
    
  • Binded parameters can be BSON Primitives or any complex type :

    friends.find("{address: #}", new Address(..)); 
    
  • Querying and unmarshalling is as fast as the driver. No Jackson process extra cost
  • Use Jackson features to map you Pojo: polymorphism, JsonView...

BTW your GSON un/marshaller can be integrated into Jongo by implementing a Mapper.

like image 39
Benoît Guérout Avatar answered Nov 02 '22 07:11

Benoît Guérout