# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)

import configparser 
config = configparser.ConfigParser()
config.read('config.ini')
ip = config['DEFAULT']['IP']
port = config['DEFAULT']['MongoDB-Port']
contain_string = config['DEFAULT']['Contain-String']

import pymongo
from pymongo import MongoClient
client = MongoClient(ip, int(port))
# -

db_twitter = client["Twitter"]
collections_twitter = db_twitter.collection_names()

# +
dic_collection = {}
for i in collections_twitter:
    if i.startswith("20") and contain_string in i:
        dic_collection[i] = "{:,}".format(db_twitter[i].find({}).count())

for key in sorted(dic_collection):
    print("%s: %s" % (key, dic_collection[key]))
# -

for collection in sorted(dic_collection):
    exist = 0
    # get information of index of each collection
    index_information = db_twitter[collection].index_information()
    # check if collection has "text_text" index
    for index in index_information:
        if index == "text_text":
            exist = 1
            print("Text index exists in " + collection)
    # if "text_text" index not exist then create one
    if exist == 0:
        print("No text index exists in " + collection)
        print("creating text index....")
        db_twitter[collection].create_index([("text", pymongo.TEXT)])
        print("Text index for " + collection + " is done.")



StarcoderdataJupyterScriptsDedupFiltered