From 8d2bdf02fb163cf83d9a6d0303319507474ede15 Mon Sep 17 00:00:00 2001 From: Cédric Bonhomme Date: Tue, 2 Apr 2013 07:53:35 +0200 Subject: Index are generated in background at database initialization. --- source/mongodb.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'source/mongodb.py') diff --git a/source/mongodb.py b/source/mongodb.py index f0aa730a..964c79a4 100644 --- a/source/mongodb.py +++ b/source/mongodb.py @@ -39,14 +39,21 @@ class Articles(object): self.connection = pymongo.connection.Connection(url, port) self.db = pymongo.database.Database(self.connection, self.db_name) self.db.authenticate(user, password) + collections = self.db.collection_names() + for collection_name in collections: + if collection_name != "system.indexes": + self.db[collection_name].ensure_index([("article_date", pymongo.DESCENDING)], \ + name="date_index", unique=False, \ + background=True) + self.db[collection_name].ensure_index([('article_content', pymongo.ASCENDING)], \ + name="content_index", unique=False, \ + background=True) def add_collection(self, new_collection): """ Creates a new collection for a new feed. """ collection = self.db[new_collection["feed_id"]] - collection.create_index([("article_date", pymongo.DESCENDING)], {"unique":False, "sparse":False}) - collection.ensure_index('article_content', pymongo.ASCENDING) collection.insert(new_collection) def add_articles(self, articles, feed_id): -- cgit