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

How do I deal with Error stated below?
pymongo query return in direct query not in pymongo
Displaying nested dictionaries in flask-admin
mongoengine slow serialization of embedded documents with reference fields
Pymongo error : 'Cursor' object has no attribute 'find' , how to filter it?
Spark Streaming: foreachRDD insert into mongoDB using python?
how to find right version of bson from pip for pymongo/mongoengine
How to enable `projection` in pymongo query class `find()`?
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

Categories

HOME
rust
yaml
seedstack
jms
android-fragments
theano
informatica
haxe
google-search-console
selection
migrate
fbloginview
asp.net-core-1.0
closures
fosuserbundle
cloudsim
node-webkit
workday
backtracking
batch-rename
qa
ini
firemonkey-style
python-appium
clipboard
react-bootstrap-table
bobo-browse.net
credit-card
locks
phpstorm-2016.1
plantuml
cargo
macports
spring-restcontroller
right-click
grails-spring-security
secure-coding
spooler
ipp-protocol
viewmodel
uiautomatorviewer
password-hash
iptv
aurelia-fetch-client
sesame
word-2016
fitbit
wildfly-9
gawk
petsc
openquery
adjacency-list
vaadin4spring
voronoi
wepay
physicsjs
sunos
famo.us
incognito-mode
pageviews
preorder
boolean-algebra
lsa
jmenubar
eclim
microformats
itunes-sdk
lynx
workflow-manager-1.x
prefuse
server-variables
resharper-5.1
tabbarcontroller
ixmldomdocument
3270
subtext
fuzzy-comparison
rootkit

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App