pymongo


Nested pymongo queries (mlab)


I have some documents in mlab mongodb; the format is:
{
"_id": {
"$oid": "58aeb1d074fece33edf2b356"
},
"sensordata": {
"operation": "chgstatus",
"user": {
"status": "0",
"uniqueid": "191b117fcf5c"
}
},
"created_date": {
"$date": "2017-02-23T15:26:29.840Z"
}
}
database name : mparking_sensor
collection name : sensor
I want to query in python to extract status key value pair and created_date key value pair only.
my python code is :
import sys
import pymongo
uri = 'mongodb://thorburn:tekush1!#ds157529.mlab.com:57529/mparking_sensor'
client = pymongo.MongoClient(uri)
db = client.get_default_database().sensor
print db
results = db.find()
for record in results:
print(record["sensordata"] , record['created_date'])
print()
client.close()
which gives me everything under sensordata as expected, dot notations giving me an error, can somebody help?
PyMongo represents BSON documents as Python dictionaries, and subdocuments as dictionaries within dictionaries. To access a value in a nested dictionary:
record["sensordata"]["user"]["status"]
So a complete print statement might be:
print("%s %s" % (record["sensordata"]["user"]["status"], record['created_date']))
That prints:
0 {'$date': '2017-02-23T15:26:29.840Z'}

Related Links

Inserting string as regular string in mongodb
Mongos + Pymongo 2.5 ==>No suitable hosts found
pymongo collection add item using update
How to convert objectid to string
Conditional atomic mongo remove with different results
python script to connect to mongodb master
flask-admin and pymongo search
Running code when mongoengine loads a document
Can't replace mongo document
How do I call a function with pymongo using webpy?
python motor offers fetch_next exception on aggregation
python motor mongo cursor length or peek next
PyMongo check if update succeeded
insert and query an OrderedDict in MongoHQ
Return .str of ObjectID using pymongo
Selecting the lowest/highest with find_one in Pymongo

Categories

HOME
tomcat
mpdf
windows-8.1
xcode8.1
typeerror
dronekit-python
greasemonkey
angular-meteor
lc3
xades4j
cqrs
lapack
google-awareness
mailgun
distributed-computing
orange-api
pinterest
word2vec
bazaar
amazon-mws
libgit2
sqlplus
el
liferay-6.2
altium-designer
acl
pyopencl
react-dnd
pubxml
tortoisemerge
sap-lumira
ruby-daemons
tf-idf
acrobat
javax.persistence
fatfs
copy-protection
source-insight
instructions
python-2.x
lxml
gnu-classpath
dreamfactory
pdfminer
ftp-server
mv
llvm-ir
android-n
reset
system-on-chip
contactless-smartcard
notesview
jqgrid-asp.net
latexml
daz3d
spongycastle
qvtkwidget
image-registration
sony-future-lab-n
palindrome
wikimapia
rcharts
front-camera
pjax
bigdecimal
fuzzer
django-testing
codecharge
jeasyui
wif4.5
flask-security
dsa
bootstrap-wysiwyg
abstract-factory
signed
ssmtp
mutators
multivariate-testing
angular-ui-select
webmethod
squeezebox
stripe.net
commenting
nodeload
wso2bam
jack
hotswap
dynamic-rdlc-generation
rikulo
cgrectmake
zend-pdf
workflow-manager-1.x
dropshadow
play2-mini
libxslt
solandra
ccnet-config
fgetc
3270
projectpier
iphone-sdk-3.1.3
objectinstantiation
gui-designer
downloadfile

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App