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") #????
got the answer:
answer from Google groups