pymongo


Return .str of ObjectID using pymongo


How would I return just the string component of an BSON ObjectId using pymongo. I'm able to encode a string into an Object id by importing ObjectId from bson.objectid; but am unable to do the reverse.
When I try:
for post in db.votes.find({'user_id':userQuery['_id']}):
posts += post['_id'].str
I get an ObjectId has no attribute str error.
Thanks!
The standard way in python to get object's string representation is using the str builtin function:
id = bson.objectid.ObjectId()
str(id)
=> '5190666674d3cc747cc12e61'
try this:
for post in db.votes.find({'user_id':userQuery['_id']}):
posts += str(post['_id'])
BTW, you can use MongoKit to deal with the special bson data structure.
from bson.objectid import ObjectId
class CustomObjectId(CustomType):
mongo_type = ObjectId # optional, just for more validation
python_type = str
init_type = None # optional, fill the first empty value
def to_bson(self, value):
"""convert type to a mongodb type"""
return ObjectId(value)
def to_python(self, value):
"""convert type to a python type"""
return str(value)
def validate(self, value, path):
"""OPTIONAL : useful to add a validation layer"""
if value is not None:
pass # ... do something here
This custom ObjectId can turn the bson ObjectId to python str.
Visit http://mongokit.readthedocs.org/mapper.html#the-structure for more information.

Related Links

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
Return .str of ObjectID using pymongo

Categories

HOME
list
apple-push-notifications
functional-programming
adfs3.0
schema.org
code-formatting
tcp
cypher
gentelella
tfs2015
video-streaming
sharepoint-online
siesta
freertos
stimulsoft
jodatime
google-sites
word2vec
http-authentication
nullreferenceexception
jest
sqlplus
coreldraw
sybase-ase
backendless
pdf.js
offline-caching
ruby-daemons
echosign
history.js
guice
google-play-developer-api
matlab-app-designer
location-services
progid
grails-2.5
runtimeexception
xvfb
gitolite
selenide
praat
right-click
notesview
moinmoin
linq-to-excel
spongycastle
rm
htmlspecialchars
mongodb-aggregation
front-camera
bilinear-interpolation
nsrunloop
fragmentstatepageradapter
wicked-pdf
node-glob
facebook-ios-sdk
swiffy
mercurial-extension
preferenceactivity
bayesglm
linuxbrew
html.actionlink
largenumber
astropy
highest
swagger-maven-plugin
darwin
clarion
kotlin-android-extensions
shell-extensions
fasterxml
inequality
otl
maven-central
sql-function
objective-c-runtime
.net-remoting
rikulo
web-frameworks
handshake
ip-geolocation
idispatch
monocross
dropshadow
mvcrecaptcha
net-use
play2-mini
channelfactory
chunking
urchin
wmd-editor
icon-language
activex-exe
webclient.uploaddata

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