pymongo


Inserting string as regular string in mongodb


The pymongo documentation says that BSON strings are UTF-8 encoded so PyMongo must ensure that any strings it stores contain only valid UTF-8 data. Unicode strings (<type ‘unicode’>) 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.
So I understand that to get rid of the Unicode literal 'u', I will have to call json.dumps() on the document returned by the query.
The documentation also says that Regular strings (<type ‘str’>) are validated and stored unaltered. And I am assuming that the query result also throws it back as a regular string and not a Unicode string.
I created a dictionary with regular string types and inserted it in DB and when I retrieve it, I get the strings as Unicode. Any idea on how do I do it? The purpose is to avoid calling json.dumps() on the query result. I need to fetch large number of documents from the DB and json.dumps() is taking quite some time. The strings that I am storing contain ASCII data so I don't need Unicode strings.
The assumption that the regular string is returned back as regular string was not correct. It is stored unaltered and not encoded to UTF-8 because it is already UTF-8. While decoding during the query, everything is converted back to Unicode.
Source:
Automatic string to unicode object conversion
How can I get pymongo to always return str and not unicode?

Related Links

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
insert and query an OrderedDict in MongoHQ

Categories

HOME
amazon-web-services
blast
postgresql-9.4
message-queue
embedded-resource
android-emulator
nsis
tibco
share
clone
angular-meteor
mithril.js
tweepy
parsley.js
documentum
clish
android-externalstorage
trace32
fresco
vertica
google-sites
node-webkit
unity-container
el
fluent-migrator
rxjs5
animated-gif
fusetools
spreadsheetgear
hdmi
offline-caching
android-service
running-object-table
basex
dnsmasq
basic-authentication
xenapp
progid
mongodb-3.4
osx-lion
adminer
busboy
testlink
stackpanel
ng-repeat
best-buy-api
classnotfoundexception
oracle-bmcs
asihttprequest
jshint
bit-shift
icecast
comm
renderman
subforms
android-webservice
apache-modules
dynamics-crm-4
rustdoc
front-camera
wildfly-9
metabase
java-2d
e
p6spy
nsdata
piping
fdt
django-redis
teamviewer
onepage-checkout
nette
highest
extend
libz
org-babel
actionpack
ocmockito
quercus
tidyr
mojo
jcheckbox
datagridcomboboxcolumn
siena
zend-search-lucene
spring-social-linkedin
autonumber
non-ascii-characters
google-code-prettify
chartfx
ofstream
jquery-cycle
system-requirements
multiscaleimage
voice-recording
msinfo32
prefuse
trialware
kolite
fork-join
kohana-auth
android-input-method
aspbutton
ppc
tacit-programming
inversion
database-cloning
code-camp

Resources

Encrypt Message