5

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!

Vladimir
  • 2,481
  • 4
  • 31
  • 41

2 Answers2

2

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.

yves amsellem
  • 7,106
  • 5
  • 44
  • 68
  • I use gson to unmarshall the response, so I don't need to set every property myself. And Jongo also iterates through each result and unmarshalls it using JSON. So that's not an issue: new DomainConverter(clazz).getObject(entity.toString()) – Vladimir Jan 19 '13 at 15:51
  • You used GSON to unmarshall the DBCursor response? Can you please show a complete example of code? The Jongo team have been working hard to make that unmarshalling step as performant as the driver; have you computed the performance of your solution? – yves amsellem Jan 20 '13 at 10:32
  • I'm not unmarshalling the cursor, rather the DBObject(s) returned by the cursor. – Vladimir Jan 22 '13 at 07:42
1

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.

assylias
  • 321,522
  • 82
  • 660
  • 783
Benoît Guérout
  • 1,977
  • 3
  • 21
  • 30
  • Templating does simplify some things, but I still don't see this being a good-enough reason to switch to jongo -- this can easily be achieved with string-replace. One way or another, if nobody else provides a better answer I'll accept yours. – Vladimir Jan 22 '13 at 20:51