pymongo


How can I get pymongo to always return str and not unicode?


From the pymongo docs:
MongoDB stores data in BSON format. BSON strings are UTF-8 encoded so PyMongo must ensure
that any strings it stores contain only valid UTF-8 data. Regular strings () are > validated and stored unaltered. Unicode strings () are encoded UTF-8 first. > The reason our example string is represented in the Python shell as u’Mike’ instead of
‘Mike’ is that PyMongo decodes each BSON string to a Python unicode string, not a regular
str."
It seems a bit silly to me that the database can only store UTF-8 encoded strings, but the return type in pymongo is unicode, meaning the first thing I have to do with every string from the document is once again call encode('utf-8') on it. Is there some way around this, i.e. telling pymongo not to give me unicode back but just give me the raw str?
No, there is no such feature in PyMongo; every string decoded from BSON is decoded as UTF-8. Python represents the string internally as UCS-2 or some other format, depending on the Python version. See the code where the BSON decoder extracts a string.
In the upcoming PyMongo 3.x series we may add features for more flexible BSON decoding to allow developers to optimize uncommon use cases like this.

Related Links

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?
python motor offers fetch_next exception on aggregation
python motor mongo cursor length or peek next
PyMongo check if update succeeded

Categories

HOME
rust
office365
plesk
class
verification
android-fragments
concurrency
windows-10
nsis
tcp
google-webmaster-tools
implicit-conversion
ipv6
bellman-ford
lapack
fullcalendar-scheduler
bundler
orange-api
uima
gmp
cloudsim
http-authentication
restier
vuforia
jpa-2.1
plyr
tf-idf
jackrabbit-oak
pyspark-sql
sfml
openbr
locks
source-insight
tightvnc
phpstorm-2016.1
quick-nimble
netstat
ms-solver-foundation
django-crispy-forms
django-smart-selects
assert
google-now
best-buy-api
oauth2
system-on-chip
yoast
jqgrid-asp.net
julius-speech
common-table-expression
wysihtml5
deployd
phppgadmin
sonatype
iptv
android-webservice
simple-schema
freshdesk
mongodb-aggregation
word-2007
mobile-angular-ui
email-forwarding
predicates
oracle-fusion-apps
portal
android-4.2-jelly-bean
android-syncadapter
uibinder
distributed-cache
webgrind
ui4j
google-maps-api-2
nette
dsa
gluon-desktop
textscan
subfolder
innerhtml
nsmutabledata
htmltidy
microsoft-expression-web
thoughtworks-go
cout
berkeley-db-xml
lmax
distributed-r
zend-search-lucene
typesafe-stack
codahale-metrics
quickgraph
ember-table
jacob
jsplitpane
spark-view-engine
audiotoolbox
wiktionary
glx
filelock
winsxs
cewolf
regression-testing
active-record-query
odac
raw-data
cleartype
rawcontacts
main-method
tso
stretchblt
synthesizer

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