如何使用Morphia框架在mongoDB项目中添加索引 [英] How to add indexes in mongoDB project with Morphia framework

查看:792
本文介绍了如何使用Morphia框架在mongoDB项目中添加索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个gwt项目,它使用mongoDB作为数据库,而morphia框架使用mongodb。

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

我已经完成了我的类的基本功能现在我想在我的类中插入索引以加速mongo搜索。

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

我查看了morphia文档,我看到有一个@Indexed可以做到这一点,但是我不知道如何在搜索中真正使用索引。 morphia会自动使用索引吗?

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?

有没有人在真实项目中有一个很好的索引示例? (mongodb网站的hello world示例无济于事)

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

== 编辑 ==

建议仅在嵌入字段中插入索引吗?

Is recommended insert index only in embed fields ?

推荐答案

Mongodb将自动使用索引因此不是由morphia处理的。您应该索引通常用于查询的字段,例如:

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:

发布:

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

在上面显示的简单帖子文档中,您会看到标题字段已编入索引,因为博客引擎通常会搜索标题而不是内容加上内容将使用大量的RAM,因此它可能不适合内存。这可能不是最好的例子,但它显示了主要的想法。

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.

这篇关于如何使用Morphia框架在mongoDB项目中添加索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆