Search code examples
javamongodbindexingmorphia

How to add indexes in mongoDB project with Morphia framework


i'm working on a gwt project, that uses mongoDB as database, and morphia framework to work with mongodb.

I already finished the basic dao of my classes and now i want to insert indexes in my classes to speedup the mongo searches.

I looked the morphia documentation, and i saw that haves a @Indexed that makes this, but i don't know how to really use the index in a search. The morphia will automatically use the index?

Does anyone have a good example of index in a real project ? (the hello world examples of mongodb site doesn't help to much)

== EDIT ==

Is recommended insert index only in embed fields ?


Solution

  • Mongodb will automatically use indexes so that isn't handled by morphia. You should index fields that you would commonly use for queries, for example:

    Post:

    {
        title : "My title", // indexed
        content : "My long long long long loooooong content" // Not indexed
    }
    

    In the simple post document shown above you see that the title field is indexed because a blog engine commonly searches over titles instead of contents plus the content will use a lot of your RAM so it might not fit in memory. That might no be the best example but it shows the main idea.

    I suggest you to read the indexes link.