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

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
android-fragments
apk
haxe
hspi
msmq
stack-overflow
swarm
elastic-load-balancer
log4j2
smooks
grocery-crud
smartgwt
jpa-2.0
corona
user-defined-types
error-logging
unity-container
sqlplus
dashdb
el
unordered-multimap
spreadsheetgear
hevc
jackrabbit-oak
react-bootstrap-table
npoi
splunk-query
phpstorm-2016.1
windows2012
commonsware-cwac
memory-fences
cucumber-junit
appcode
listjs
slideshow
fiware-wirecloud
httphandler
nuclio
audio-converter
qvtkwidget
nusoap
lightning-workbench
sigsegv
embedded-v8
android-mediarecorder
word-2007
http4s
oid
always-on-top
groovyfx
eclemma
deferred-rendering
frame-grab
django-redis
verisign
as3-api
onepage-checkout
outlook.com
linuxbrew
arbre
opencyc
apache-commons-digester
blank-line
wso2ml
ngcordova
cache-manifest
web-api
ssmtp
lifetime
rubber
inequality
distributed-r
mft
visual-c++-2005
boinc
ember-table
gitx
gamesalad
series-40
gwt-celltable
mvcrecaptcha
motodev-studio
groovy-console
uipasteboard
tablet-pc
simile
printqueue
turbine
formal-semantics
pinax
eventaggregator
firephp
fxruby
gtktextview
prism-2
comment-conventions
gui-designer

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