Index Not Created?

51 views
Skip to first unread message

Matt

unread,
Dec 18, 2009, 11:43:13 AM12/18/09
to MongoMapper
I have a model that looks like:

MongoMapper.database = 'catalog'
class Item
include MongoMapper::Document

key :title, String, :required => true, :index => true
key :author, String
end

And I can create new items without any problems. However, when I run
the mongo shell and type "db.items.getIndexes()" it appears that the
index for title isn't being created:

[
{
"name" : "_id_",
"ns" : "catalog.items",
"key" : {
"_id" : ObjectId("000000000000000000000000")
}
}
]

I've tried removing :required and setting it via ensure_index as well
with no luck. I'm just getting started with mongodb, so I could be
missing/misunderstanding something.

Michael Dirolf

unread,
Dec 18, 2009, 12:02:59 PM12/18/09
to mongo...@googlegroups.com
Matt,

When you check in the shell are you using the right database?

Try `use catalog` before running your query.

- Mike

> --
> You received this message because you are subscribed to the Google
> Groups "MongoMapper" group.
> For more options, visit this group at
> http://groups.google.com/group/mongomapper?hl=en?hl=en
>

Cyril Mougel

unread,
Dec 18, 2009, 12:05:58 PM12/18/09
to mongo...@googlegroups.com
Le 18/12/09 17:43, Matt a �crit :

Have you launch MongoMapper.ensure_index! ? without this command, no
index are created by MongoMapper.

--
Cyril Mougel
http://blog.shingara.fr/

Matt

unread,
Dec 18, 2009, 12:11:56 PM12/18/09
to MongoMapper
On Dec 18, 11:02 am, Michael Dirolf <m...@10gen.com> wrote:
> Matt,
>
> When you check in the shell are you using the right database?
>
> Try `use catalog` before running your query.
>
> - Mike

Yep, I'm doing that. I've tried dropping the database and starting
over with no effect. I can create the index manually from the shell
via `db.bib_records.ensureIndex({'title':1})` and it shows up as you'd
expect.

John Nunemaker

unread,
Dec 18, 2009, 12:14:38 PM12/18/09
to mongo...@googlegroups.com
Make sure your models have been loaded when you call MongoMapper.ensure_indexes! or it will ensure nothing.




--

Nathan Stults

unread,
Dec 18, 2009, 12:11:01 PM12/18/09
to mongo...@googlegroups.com
Wow - I did not realize that - I was under the impression ensure_index was called behind the scenes when you used the DSL to specify a key should have one... is MongoMapper.ensure_index! Something that gets run at application startup time then?

-----Original Message-----
From: mongo...@googlegroups.com [mailto:mongo...@googlegroups.com] On Behalf Of Cyril Mougel
Sent: Friday, December 18, 2009 9:06 AM
To: mongo...@googlegroups.com
Subject: Re: [MongoMapper] Index Not Created?

Le 18/12/09 17:43, Matt a écrit :

--

Matt

unread,
Dec 18, 2009, 12:28:17 PM12/18/09
to MongoMapper
Ah, that did it ... I'm with Nathan - something I read in another
thread made me think MongoMapper.ensure_indexes! was handled
automatically. Now we know - thanks!

On Dec 18, 11:11 am, "Nathan Stults" <Nathan_Stu...@HSIHealth.com>
wrote:

> Cyril Mougelhttp://blog.shingara.fr/

John Nunemaker

unread,
Dec 18, 2009, 1:07:44 PM12/18/09
to mongo...@googlegroups.com
No it is not handled automatically. I was running into issues where models were loading before connections were made and it was a pain so I moved the actual performing of index creations to the MongoMapper.ensure_indexes! method. 

I'll revisit this in the future so that things are more automatic but I needed to get something working in the short term.
Reply all
Reply to author
Forward
0 new messages