where to use ensure_index ????

49 views
Skip to first unread message

aliane abdelouahab

unread,
Sep 22, 2012, 6:17:31 PM9/22/12
to mongodb-user
hi, i've asked it on stackoverflow, and no answers, so i've said that
maybe ensure_index is only done by the admininstrator with the shell?
and not when the client clic somewhere!
here is the original question:

How can I add an index in production code? I have found only the way
to add it as a command and not embedded in a complete code:

If you want to add indexing to speed queries:

db.users.ensure_index(the_key)

So I tried to add it to a class:

class Registration(BaseHandler):
def post(self):
# do stuff to get user information using the
self.get_argument()
user={"all information":informations}
self.db.users.insert(user, w=1)
self.db.users.ensure_index(pseudo, commune)

But I get errors like this:

self.db.users.ensure_index(pseudo, commune)
File "build\bdist.win-amd64\egg\pymongo\collection.py", line
829, in ensure_index
return self.create_index(key_or_list, cache_for, **kwargs)
File "build\bdist.win-amd64\egg\pymongo\collection.py", line
740, in create_index
self.__name, name, cache_for)
File "build\bdist.win-amd64\egg\pymongo\connection.py", line
330, in _cache_index
expire = datetime.timedelta(seconds=cache_for) + now
TypeError: unsupported type for timedelta seconds component:
unicode

And I guess it will be the same tip when using inserting a sub-
document:

self.db.users.update({"email":email}, {"$push":{"produit_up":
{"id":id, "namep":namep, "nombre":nombre}}})
self.db.users.ensure_index("product_up.namep") #????

any clues?

Sam Millman

unread,
Sep 22, 2012, 6:43:11 PM9/22/12
to mongod...@googlegroups.com
I saw this question. I think you have a error in your code personally.

What do pseudo and commune equal in your app as you go to call the ensureIndex() function?


--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

aliane abdelouahab

unread,
Sep 22, 2012, 6:50:43 PM9/22/12
to mongodb-user
pseudo will search for pseudo in database

pseudo = self.get_argument("pseudo")
pseudo = db.find({"pseudo":pseud})
and the same for commune, and there is other parameters, but because
those one are the searched fields later...

so if i put it under where i tell mongodb to store the user
information, then, everytime one added a new users, the index will be
stored? then it will be not good for write performances?


On 22 sep, 23:43, Sam Millman <sam.mill...@gmail.com> wrote:
> I saw this question. I think you have a error in your code personally.
>
> What do pseudo and commune equal in your app as you go to call the
> ensureIndex() function?
>

Sam Millman

unread,
Sep 22, 2012, 7:05:42 PM9/22/12
to mongod...@googlegroups.com
Not sure since if it worked like that it would recreate the b-tree index for all rows all over again. So I would probably say no.

I think you might have the wrong idea of what ensureIndex is. It is much like in SQL where you create an index on a field. An index can only be made once (unless it is removed then remade ofc) nad multiple calls to ensureIndex defining the same index will be classed as no-ops (basically ops that dont run).

Here is a SO question on how to ue ensureIndex: http://stackoverflow.com/questions/5912661/pymongo-mongodb-create-index-or-ensure-index that could be helpful hopefully.

aliane abdelouahab

unread,
Sep 22, 2012, 7:11:20 PM9/22/12
to mongodb-user
so as from i understand, it's a work made by the administrator (from a
separated interface that a client cant get), so like that, he can do
it once a month for example?

On 23 sep, 00:05, Sam Millman <sam.mill...@gmail.com> wrote:
> Not sure since if it worked like that it would recreate the b-tree index
> for all rows all over again. So I would probably say no.
>
> I think you might have the wrong idea of what ensureIndex is. It is much
> like in SQL where you create an index on a field. An index can only be made
> once (unless it is removed then remade ofc) nad multiple calls to
> ensureIndex defining the same index will be classed as no-ops (basically
> ops that dont run).
>
> Here is a SO question on how to ue ensureIndex:http://stackoverflow.com/questions/5912661/pymongo-mongodb-create-ind...
> could be helpful hopefully.

Sam Millman

unread,
Sep 22, 2012, 7:18:01 PM9/22/12
to mongod...@googlegroups.com
No No, ensureIndex is the same as creating an index. It does nothing special, maybe the name of the function is a little odd really and should be called create_index instead.

Since Mongo has no pre-defined schema (NoSQL) it has to create indexes a different way to SQL. The way proposed was through client side when the user decides they need an index on a particular field. This is how collections and what not are created, when they are used by the client.

This function was called ensureIndex().

So there is only one call needed to ensureIndex() within your app and it should be a list of fields for that index as the first param and any extra options (like sparse) for the second param.

There is no work needed by the administrator to keep this index intact, well not once a month but if something fails in the cluster then yea, some work will be needed by the admin to possibly remake the index.

I personally have a file of indexes I call everytime my app is loaded.

I hope this clears things up a little,

aliane abdelouahab

unread,
Sep 22, 2012, 7:20:11 PM9/22/12
to mongodb-user
i got it, so what i do, is, making the indexes once just after
launching the application, so i'll index fields that a user will
search.
thank you :)

On 23 sep, 00:18, Sam Millman <sam.mill...@gmail.com> wrote:
> No No, ensureIndex is the same as creating an index. It does nothing
> special, maybe the name of the function is a little odd really and should
> be called create_index instead.
>
> Since Mongo has no pre-defined schema (NoSQL) it has to create indexes a
> different way to SQL. The way proposed was through client side when the
> user decides they need an index on a particular field. This is how
> collections and what not are created, when they are used by the client.
>
> This function was called ensureIndex().
>
> So there is only one call needed to ensureIndex() within your app and it
> should be a list of fields for that index as the first param and any extra
> options (like sparse) for the second param.
>
> There is no work needed by the administrator to keep this index intact,
> well not once a month but if something fails in the cluster then yea, some
> work will be needed by the admin to possibly remake the index.
>
> I personally have a file of indexes I call everytime my app is loaded.
>
> I hope this clears things up a little,
>

Sam Millman

unread,
Sep 22, 2012, 7:23:34 PM9/22/12
to mongod...@googlegroups.com
Exactly :) Np glad to have helped

Bernie Hackett

unread,
Sep 23, 2012, 12:39:04 AM9/23/12
to mongod...@googlegroups.com
> self.db.users.ensure_index(pseudo, commune)

Not sure what "commune" is here but the second parameter to
ensure_index is the length of time in seconds that the index is cached
client side. Given the error message I assume "commune" is not an
integer. The documentation for ensure_index is here:

http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.ensure_index

aliane abdelouahab

unread,
Sep 23, 2012, 5:26:05 AM9/23/12
to mongodb-user
ah ok! now i can learn the ensure_index now that you helped me to
understand the utility of this function ;)
thank you again

On 23 sep, 05:39, Bernie Hackett <ber...@10gen.com> wrote:
> > self.db.users.ensure_index(pseudo, commune)
>
> Not sure what "commune" is here but the second parameter to
> ensure_index is the length of time in seconds that the index is cached
> client side. Given the error message I assume "commune" is not an
> integer. The documentation for ensure_index is here:
>
> http://api.mongodb.org/python/current/api/pymongo/collection.html#pym...
Reply all
Reply to author
Forward
0 new messages