pymongo


UUID to NUUID in Python


In my application, I get some values from MSSQL using PyMSSQL. Python interpret one of this values as UUID. I assigned this value to a variable called id. When I do
print (type(id),id)
I get
<class 'uuid.UUID'> cc26ce03-b6cb-4a90-9d0b-395c313fc968
Everything is as expected so far. Now, I need to make a query in MongoDb using this id. But the type of my field in MongoDb is ".NET UUID(Legacy)", which is NUUID. But I don't get any result when I query with
client.db.collectionname.find_one({"_id" : id})
This is because I need to convert UUID to NUUID.
Note: I also tried
client.db.collectionname.find_one({"_id" : NUUID("cc26ce03-b6cb-4a90-9d0b-395c313fc968")})
But it didn't work. Any ideas?
Assuming you are using PyMongo 3.x:
from bson.binary import CSHARP_LEGACY
from bson.codec_options import CodecOptions
options = CodecOptions(uuid_representation=CSHARP_LEGACY)
coll = client.db.get_collection('collectionname', options)
coll.find_one({"_id": id})
If instead you are using PyMongo 2.x
from bson.binary import CSHARP_LEGACY
coll = client.db.collectionname
coll.uuid_subtype = CSHARP_LEGACY
coll.find_one({"_id": id})
You have to tell PyMongo what format the UUID was originally stored in. There is also a JAVA_LEGACY representation.

Related Links

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
Selecting the lowest/highest with find_one in Pymongo

Categories

HOME
opengl
extjs
magento2
keyboard
clearcase
haproxy
dronekit-python
cypher
navigation
clickable-image
fullcalendar-scheduler
grocery-crud
mpu6050
powermock
xbox360
http-authentication
psql
bazaar
social-tables
bosun
intel
tortoisemerge
android-cardview
sendmessage
laravel-4.2
boolean-logic
credit-card
mongodb-3.4
gsp
express-session
amazon-kms
formsauthenticationticket
mv
tidal-scheduler
oauth2
stat
utf
suds
jacoco-maven-plugin
resampling
ejml
httphandler
nomad
liteide
nothing
non-linear-regression
datediff
embedded-v8
word-2007
emf-compare
perl-data-structures
bpms
xcode6.4
nvda
spring-mongo
gnucash
odp.net-managed
urbit
wif4.5
flask-security
apache-commons-digester
nette
asp.net-web-api-helppages
abstract-factory
fuseesb
htmltidy
maven-shade-plugin
pass-by-value
fat32
cout
senchatouch-2.4
bittorrent-sync
wso2bam
flipboard
property-injection
roxy-fileman
django-settings
sensormanager
insertonsubmit
codahale-metrics
chartfx
jsplitpane
linkbutton
cgrectmake
struts2-json-plugin
uitextfielddelegate
liveconnect
n-queens
firefly-mv
startupscript
nsconnection
ios-4.2
tessellation
pinax
self-tracking-entities
sqlsitemapprovider
comment-conventions
synthesizer

Resources

Encrypt Message