[PYTHON] Try to put data in MongoDB

things to do

Keep throwing the string "hogehoge" into the locally created MongoDB

Junbi

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.

Squeeze

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

Try to put data in MongoDB
How to create data to put in CNN (Chainer)
Try to put LED in your own PC (slightly)
Try to decipher the login data stored in Firefox
Try to calculate Trace in Python
Cython to try in the shortest
Try logging in to qiita with Python
Try working with binary data in Python
[Itertools.permutations] How to put permutations in Python
PUT gzip directly to S3 in Python
Try to reproduce NumPy's add.at in Julia
Try converting to tidy data with pandas
[V11 ~] A memorandum to put in Misskey
Books on data science to read in 2020
Try to extract specific data from JSON format data in object storage Cloudian/S3
[Introduction to SEIR model] Try fitting COVID-19 data ♬
First steps to try Google CloudVision in Python
Try to implement Oni Maitsuji Miserable in python
Try to calculate a statistical problem in Python
3.14 π day, so try to output in Python
Try using django-import-export to add csv data to django
Try to aggregate doujin music data with pandas
Try auto to automatically price Enums in Python 3.6
Try to separate Controllers using Blueprint in Flask
Sort BigQuery tables according to data in Dataflow
3 steps to put Python + mecab in yum only
Try to calculate RPN in Python (for beginners)
Preparing to try "Data Science 100 Knock (Structured Data Processing)"
To add a module to python put in Julialang
How to read time series data in PyTorch
Try to imitate Rails' _method parameter in webapp2
Sample to put Python Kivy in one file
Try to display the railway data of national land numerical information in 3D
The first step to log analysis (how to format and put log data in Pandas)
Put matplotlib in Centos7.
Summary of tools needed to analyze data in Python
How to apply markers only to specific data in matplotlib
Try to make a Python module in C language
Sampling in imbalanced data
Try to improve your own intro quiz in Python
Put jedi in emacs 24
Try to solve Sudoku in various ways (SAT, CSP)
Put AWS data in Google Spreadsheet with boto + gspread
Access MongoDB in C
I made a script to put a snippet in README.md
Try to implement yolact
Try to divide twitter data into SPAM and HAM
Try gRPC in Python
Try to delete tweets in bulk using Twitter API
Put pip in Blender
Try 9 slices in Python
Try writing JSON format data to object storage Cloudian/S3
[Linux] How to put your IP in a variable
Try scraping the data of COVID-19 in Tokyo with Python
Save in Japanese to StringProperty in Google App Engine data store
Various ways to calculate the similarity between data in python
Ingenuity to handle data with Pandas in a memory-saving manner
How to generate exponential pulse time series data in python
Put the lists together in pandas to make a DataFrame
Try to extract Azure SQL Server data table with pyodbc
Try to extract the keywords that are popular in COTOHA