Keep throwing the string "hogehoge" into the locally created MongoDB
Please install and launch MongoDB locally. "Thin book of MongoDB" will be helpful.
Also, please put pymongo. It is a guy to touch MongoDB with python.
Put "hogehoge" in the prepared MongoDB.
First, prepare a client to local MongoDB with getDBCollection and select a collection. All you have to do is write with Collection.insert (formatToInsert ("hogehoge")).
Main.py
#-*- coding: utf-8 -*-
from pymongo import MongoClient
#Get the DB write destination
def getDBCollection():
#Write to MongoDB on Localhost
client = MongoClient()
#I use a DB called LogsDB
db = client.LogsDB
#I use a collection called LogsCollection
Collection = db.LogsCollection
return Collection
#Transform data for writing
def formatToInsert(Contents):
#DB"Key name" : "data"
return {"Contents" : Contents}
if __name__ == '__main__':
Collection = getDBCollection()
#Write records to the collection
while true:
Collection.insert(formatToInsert("hogehoge"))
time.sleep(10)
Recommended Posts