如何在生产代码中添加索引?我只找到了将其添加为命令而不嵌入完整代码的方法:
如果要添加索引以加快查询速度:
db.users.ensure_index(the_key)
所以我尝试将它添加到一个类中:
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)
但我得到这样的错误:
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
而且我想在使用插入子文档时这将是相同的提示:
self.db.users.update({"email":email}, {"$push":{"produit_up":{"id":id, "namep":namep, "nombre":nombre}}})
self.db.users.ensure_index("product_up.namep") #????