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

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

Categories

HOME
cocoapods
windows-8.1
softlayer
scroll
swi-prolog
theano
initialization
malware
web2py
google-sheets-query
vue-resource
mutation-testing
google-awareness
supervisord
pinterest
deepfreeze
amazon-sns
backendless
password-generator
running-object-table
history.js
oracleforms
react-bootstrap-table
fusion
quickcheck
cloudera-manager
grails-2.5
android-n
praat
flowchart
system-on-chip
git-tfs
section508
intellij-lombok-plugin
nothing
beeline
punctuation
http-basic-authentication
gmaps.js
email-injection
gawk
cocoa-bindings
openquery
jpda
dpkg
deferred-rendering
maximize
timestamping
vaadin4spring
wikimedia-commons
m4
directx-9
android-studio-import
relational-model
webproject
scala-macros
kotlin-android-extensions
signed
cakephp-2.2
wicket-1.5
windows-rt
back
xcode6.3.1
maven-central
windows-messages
cidr
jomsocial
eclim
progress-db
application-server
indesign-server
struts2-json-plugin
taskkill
firebird1.5
acpi
kext
git-log
reliability
aspbutton
sqlsitemapprovider

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