Indexed fields

61 views
Skip to first unread message

Mike

unread,
Jun 11, 2012, 5:14:05 PM6/11/12
to mongo-jack...@googlegroups.com
Hi,

I've just started to look at Play 2.0 and configured the mongo-jackson-mapper plugin. It all works fine so far. However, I can't find a way to ensure an index on certain fields. Is there an annotation I can use or do I have to code something manually?

Currently in my Play 1.2.x apps, I use Morphia which provides very simple annotations. Anything similar in the mongo-jackson-mapper?

Thanks,

Mike

Stanislav Vasilev

unread,
Jun 15, 2012, 6:31:48 PM6/15/12
to mongo-jack...@googlegroups.com
That would answer my question as well, do I have to explicitly execute the command to ensure the index ?

James Roper

unread,
Jun 15, 2012, 6:53:37 PM6/15/12
to mongo-jack...@googlegroups.com
Hi Mike and Stanislav,

There is no annotation for indexes, it has to be coded manually.  I do intend on providing a simpler API for creating indexes (currently it's very simple for single field ascending, but multiple fields, or sparse indexes etc require stuff like

coll.ensureIndex(new BasicDBObject("someField", 1).append("someOtherField", -1), 
    new BasicDBObject("unique", true).append("sparse", true).append("name", "somename"));

which is obviously not very nice.

As for providing this functionality via annotations on fields, it is not very straight forward to do by design.  The mongo jackson mapper was designed to fully utilise Jackson to do all serialisation/deserialisation.  Consequently, it does very little reflection and inspection of annotations itself, providing it the ability to do this would potentially mean duplicating all the Jackson introspection of classes, though there may be some ways to hack it.  Consequently, while it may be something I might consider doing in the future, it's very low on my priorities.

Another thing to consider, after briefly reading over the Morphia docs it seems that morphia doesn't support multi field indexes, nor does it support sparse indexes.

Cheers,

James

Mike

unread,
Jun 16, 2012, 12:42:47 AM6/16/12
to mongo-jack...@googlegroups.com
Hi James,

Thanks for the detailed information. Manually coding works fine, I mainly tried to make sure I didn't miss anything in terms of available annotations etc.

Just FYI, Morphia supports compound indexes configured as class-level annotations, see http://code.google.com/p/morphia/wiki/AllAnnotations#Compound_Indexes.

Cheers,

Mike
Reply all
Reply to author
Forward
0 new messages