Unable to insert data into MongoDB.

Arjun Srivatsa arjuns123 at gmail.com
Tue Feb 16 07:41:57 EST 2016


Thanks a lot. Will implement that. Although I am able to do using just 2 scripts as well. 

On Monday, February 15, 2016 at 5:34:08 PM UTC+1, Peter Otten wrote:
> Arjun Srivatsa wrote:
> 
> > Hi Peter.
> > 
> > Thank you for the reply.
> > 
> > This is the read_server code:
> > 
> > import socket
> > from pymongo import MongoClient
> > #import datetime
> > import sys
> > 
> > # Connection to server (PLC) on port 27017
> > host = "10.52.124.135"
> > port = 27017
> > 
> > s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> > s.connect((host, port))
> > sys.stdout.write(s.recv(1024))
> > 
> > 
> > And the write_db code:
> > 
> > from pymongo import MongoClient
> > import datetime
> > import socket
> > import sys
> > 
> > client = MongoClient('mongodb://localhost:27017/')
> > db = client.test_database
> > 
> > mongodoc = { "data": 'data', "date" : datetime.datetime.utcnow() }
> > values = db.values
> > values_id = values.insert_one(mongodoc).inserted_id
> > 
> > 
> > So, both these scripts work independently. While, read_server shows the
> > output of the actual data from PLC, write_db inserts the sample data into
> > the MongoDB.
> > 
> > I am not sure as to how to combine these both and get the desired output.
> 
> What I mean is once you have working scripts
> 
> connect_to_mongodb()
> while True:
>     record = make_fake_data()
>     insert_record_into_mongodb(record)
> 
> and
> 
> connect_to_server()
> while True:
>     record = read_record_from_server()
>     print(record)
> 
> you can combine the code in a third script to
> 
> connect_to_server()
> connect_to_mongodb()
> while True:
>     record = read_record_from_server()
>     insert_record_into_mongodb(record)
> 
> and be fairly sure that the combination works, too.




More information about the Python-list mailing list