pymongo


convert pymongo cursor to json


I know this is a fairly common problem. I'm writing a small Flask app and I'm trying to feed some queries back to the view.
I've connected to my local MongoDB setup, and made a successful query - but I can't generate a json object with it.
The most common solution I've seen is to import json_util from pymongo i.e.
import json
from pymongo import json_util
results = connection.get_collection('papayas_papaya')
results = results.find({
'identifier': '1',
})
serialized_results = [json.dumps(result, default=json_util.default, separators=(',', ':')) for result in results]
I've installed pymongo into my Flask virtualenv using pip i.e :
pip install pymongo
When running the above code I keep getting the following error:
ImportError: cannot import name json_util
I can see this line in the pymongo-2.3-py2.6.egg-info/installed-files.txt
../bson/json_util.py
Anyone got any tips that can help me figure out what I'm doing wrong?
UPDATE:
Having noodled about with this a little further - I've managed to get this working thus:
import pymongo
from bson.json_util import dumps
connection = pymongo.Connection("localhost", 27017)
db = connection.mydocs
def get():
cursor = db.foo.find({"name" : "bar"})
return dumps(cursor)
One of the problems I had was trying to pip install bson independently - pymongo brings bson with it and importing bson separately caused problems.
Thanks #Cagex for pointing me in the right direction
It looks like you want to import from bson not pymongo. I believe json_util was moved to that module recently.
http://api.mongodb.org/python/current/api/bson/json_util.html

Related Links

Variables don't hold value for very long pymongo ipython
pymongo upgrade to 3.0.3 causes this statement to fail
UUID to NUUID in Python
how to test for empty mongodb query in pymong
read video file from mongodb with pymongo
Get OperationFailure error with PyMongo 2.7.1
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?

Categories

HOME
magento2
azure-functions
clearcase
openxml
couchdb
selenium-builder
foaf
mathematical-optimization
fogbugz
share
position
angular-meteor
gprs
watch-os-3
squirrel-sql
bnf
asciimath
word2vec
managed-c++
android-cardview
openbravo
android-service
network-analysis
sfml
fatfs
unmarshalling
matlab-app-designer
netbios
rust-cargo
cucumber-junit
private-key
om-next
transform
netstat
eclipse-gmf
android-alarms
swfupload
notesview
code-push
destroy
daz3d
qvtkwidget
scala-breeze
photoswipe
entropy
spring.net
maven-jaxb2-plugin
android-mediarecorder
pecl
openerp-6
pbx
boost-compute
p6spy
wolfram-language
node.js-connect
wicked-pdf
adjacency-list
processor
urbit
utf-16
persistent-object-store
accessibilityservice
servicestack-bsd
gdl
miniconda
opencyc
css-paged-media
gql
abstract-factory
findersync
nanomsg
microsoft-expression-web
vs-unit-testing-framework
rubber
iodocs
tabris
visual-studio-6
senchatouch-2.4
miglayout
pre
jms-serializer
map
code39
libxml-js
ax
ckeditor.net
xalan
ip-geolocation
playorm
kaazing
series-40
opends
prototypal-inheritance
algol68
acpi
navigationcontroller
play2-mini
ognl
webresponse
tabbarcontroller
iphone-sdk-3.1.3
twitter-feed
error-detection

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