-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext index.py
36 lines (28 loc) · 980 Bytes
/
text index.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
36
# Create a collection called "articles" and insert documents representing articles with fields like "title" and "content".
# Create a text index on the "content" field.
# Write a query to search for articles that contain a specific keyword in their content.
import pymongo
import datetime
from pprint import pprint
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client['test']
articles = db.articles
articles_data = [
{
'title': 'Introduction to MongoDB',
'content': 'MongoDB is a NoSQL database...',
},
{
'title': 'Python Programming Guide',
'content': 'Python is a powerful programming language...',
},
{
'title': 'Web Development Basics',
'content': 'Web development involves creating...',
}
]
# articles.insert_many([articles_data])
articles.create_index([('content', pymongo.TEXT)])
for article in articles.find({"$text":{"$search":"python"}}):
pprint(article)