Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a single value from a pymongo query of a mongodb in python?

I'm trying to use pymongo. Everything is ok so far, I can connect to mongodb, insert and make queries. The problem i'm facing is getting data from find() into a python array.

This is my code

>>> a = db.sensor.find({'sensor_id':3})
>>> a
<pymongo.cursor.Cursor object at 0x637f70>
>>> for bla in a:
...     print bla
... 
{u'date': datetime.datetime(2013, 3, 4, 21, 8, 23, 907000), u'_id': ObjectId('51350d4772ab8a691c370453'), u'sensor_id': 3, u'value': -1625.0}
{u'date': datetime.datetime(2013, 3, 4, 21, 20, 12, 694000), u'_id': ObjectId('5135100c72ab8a695b54a4f3'), u'sensor_id': 3, u'value': -1875.0}
{u'date': datetime.datetime(2013, 3, 4, 21, 22, 4, 985000), u'_id': ObjectId('5135107c72ab8a69851a5a95'), u'sensor_id': 3, u'value': -1812.0}
{u'date': datetime.datetime(2013, 3, 4, 21, 26, 11, 758000), u'_id': ObjectId('5135117372ab8a69b285b4c7'), u'sensor_id': 3, u'value': -1312.0}

Is there a way to make something like myValues[i] = bla.value?

like image 903
otmezger Avatar asked Dec 21 '22 08:12

otmezger


1 Answers

The "bla" is just a dictionary, so

myValues[i] = bla['value']

is what you are looking for.

like image 158
teapot_of_wine Avatar answered May 06 '23 07:05

teapot_of_wine