Mongodb shortest introduction (1) Install & start on EC2 & suddenly put tens of thousands of items Continued
Even though there were tens of thousands, I was able to search by partial match in Japanese. In (1), I tried to put data of about 1G of text amount with t2.micro of EC2. Pretty light
--On the command line, use jsavascript regular expressions. --If you pass it as an object with a python regular expression, you can use partial match and regular expression --Since it is included in bson instead of json, use the utility of bson.
python search.py
search.py
# coding: utf-8
import pymongo
from bson.json_util import loads
from bson.json_util import dumps
import re
client=pymongo.MongoClient()
#database
db=client.test
regx = re.compile("pink", re.IGNORECASE)
cursor = db.honyarara.find({"title":regx})
for document in cursor:
print dumps(document)
I will search the data put in MongoDB and make it a CGI that returns the result with json
python -m CGIHTTPServer &
See here ↓ CGI server (1) python in one line
curl http://127.0.0.1:8000/cgi-bin/search.py?q=test
This time, the word passed by q = <> is ** OR searched ** in two fields called title or kana.
Assuming that there is such data → {"title ":" test mongo! "," Kana ":" "}, {}, {} ,,
search.py
#!/usr/bin/python
# coding: utf-8
import pymongo
from bson.json_util import dumps
import re
import json
import cgi
print "Content-type: text/html\n\n"
client=pymongo.MongoClient()
#database test
db=client.test
#Get query
form = cgi.FieldStorage()
#If there is a request variable called q
if(form.has_key("q")):
q = form["q"].value
regx = re.compile(q, re.IGNORECASE)
#Set the regular expression in the field and get it with mongo's find method
cursor = db.honyarara.find({
"$or": [
{"title":regx},
{"kana":regx}
]
})
else:
print "{}"
exit()
#Put it in the list, bson → json character string → convert json to variable
items=[]
for document in cursor:
items.append(json.loads(dumps(document)))
#Convert from array to json
print json.dumps({"items": items })
service mongod stop
--Check the data directory in /etc/mongo.conf`` / var / lib / mongo
--Roughly erase the contents. Or recreate the directory with mongod user privileges
--Or point to another location in conf
--So, reboot
service mongod start
It seems that the paths etc. are the same according to the method of MySQL. With my old MyISAM, it's okay to roughly delete or move files, and I miss it. ..
Recommended Posts