Vladimir
Vladimir

Reputation: 2553

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!

Upvotes: 5

Views: 2524

Answers (2)

Beno&#238;t Gu&#233;rout
Beno&#238;t Gu&#233;rout

Reputation: 1997

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.

Upvotes: 1

yves amsellem
yves amsellem

Reputation: 7244

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.

Upvotes: 2

Related Questions