-
Notifications
You must be signed in to change notification settings - Fork 0
/
onequery.py
35 lines (29 loc) · 1.27 KB
/
onequery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from pymongo import MongoClient
def search_word_in_collection(username, password, database_name, collection_name, search_word, columns=[]):
host = 'localhost'
port = 27017
uri = f"mongodb://{username}:{password}@{host}:{port}"
# Connect to MongoDB
client = MongoClient(uri)
db = client[database_name]
collection = db[collection_name]
# Create a filter to search for the word in specified columns
filters = []
for column in columns:
filters.append({column: {"$regex": search_word, "$options": "i"}})
# If no columns are specified, search in all columns
if not filters:
filters = [{"$or": [{key: {"$regex": search_word, "$options": "i"}} for key in collection.find_one().keys()]}]
# Search for the word in the collection
result = collection.find({"$or": filters})
# Print the matched documents
for document in result:
print(document)
# Example usage
username = 'root'
password = 'onecrawlrootpass'
database_name = "crawler"
collection_name = "page_informations"
search_word = "australia"
columns = ["content_text", "description"] # Specify the columns to search in (leave empty to search in all columns)
search_word_in_collection(username, password, database_name, collection_name, search_word, columns)