Create Content Profile Data to MongoDB
1. Prerequire
- Pycharm: conda install pymongo
- MongoDB: Download and install MongoDB
- Robo 3T: Download & install Robo 3T
- MongoDB Installation Guide: Installation guide updating
2. Connect MongoDB
import pymongo
import datetime
2.1 Initial MongoDB Class
class MongoDB(object):
def __init__(self, db):
mongo_client = self._connect('127.0.0.1', '27017', '', '', db)
self.db_client = mongo_client[db]
self.collection_test = self.db_client['test_collection']
2.2 Create Connect
def _connect(self, host, port, user, pwd, db):
mongo_info = self._splicing(host, port, user, pwd, db)
mongo_client = pymongo.MongoClient(mongo_info)
return mongo_client
2.3 Splicing connetion
@staticmethod
def _splicing(host, port, user, pwd, db):
client = host + str(port)
if user != '':
client = user + pwd + host + str(port)
if db != '':
client = client + db
return client
3. Test&Result
3.1 Content Profile
def test_insert(self):
test_collection = dict()
test_collection['name'] = 'AINLPer'
test_collection['job'] = 'AI algorithm'
test_collection['date'] = datetime.datetime.utcnow()
self.collection_test.insert_one(test_collection)
3.2 Test
if __name__ == '__main__':
mongo = MongoDB(db='mongodbnlp')
mongo.test_insert()
3.3 Result