pymongo


How to update Saved Document Extra data in GridFS


I have stored files on gridFS that has extra information stored on them - it is not set on the metadata otherwise I would use this
For Pymongo 3.2.2 we stored information on the same level as the actual data in fs.files (not using the metadata)
so for example we have:
fs.files = [ {
_id, description, title, ...
}]
When I call GridFS.put like so and nothing happens
FS = GridFS(mongo_,)
file.description = request_data.get('description', None)
FS.put(file)
How can I update that file extra information such as description?
What is file in your code example? That is, what are you passing to GridFS.put?
To add metadata to a GridFS file, pass additional keyword arguments to put, as it shows in the PyMongo tutorial:
>>> fs.put(b'data', filename='foo', description='my description')
ObjectId('5825ea8ea08bff9df5059099')
Now the metadata is stored along with your data in GridFS:
>>> gridout = fs.get(ObjectId('5825ea8ea08bff9df5059099'))
>>> gridout.description
u'my description'
Under the hood, you can see that PyMongo stored the metadata in the fs.files collection in MongoDB:
>>> for doc in db.fs.files.find()
... print(doc.get('description'))
...
my description
But the better way to access GridFS data is with PyMongo's GridFS API, not by directly querying the collections.
Another way to store metadata is to create a GridIn, set a field, and call close:
>>> gridin = fs.new_file()
>>> gridin.filename = 'foo'
>>> gridin.description = 'my description'
>>> gridin.write(b'data')
>>> gridin.close()
This is a good option if you need to call write multiple times with chunks of data.
Manually update those extra fields without using GridFS:
file = mongo_.fs.files.find_one({'_id': ObjectId(fileId)})
file['description'] = request_data.get('description', None)
mongo_.fs.files.save(file)

Related Links

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
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?

Categories

HOME
blast
cucumber
eclipse-plugin
workflow
redmine
writefile
message-queue
ios-simulator
iverilog
gallery
biztalk-2010
azure-mobile-services
openfoam
onvif
native-base
tweepy
tup
gtk
swarm
orc
feature-extraction
amazon-elb
nest-api
l20n
gmp
coroutine
parceler
cmake-gui
firemonkey-style
pubxml
xdebug
math.js
amazon-iam
intune
objectmapper
netbios
wampsharp
cloveretl
char-pointer
numerics
jslint
code-rally
rdp
export-to-pdf
right-click
raytracing
git-tfs
code-push
react-intl
axis
viewmodel
.net-micro-framework
lightning-workbench
entropy
lvalue
polymaps
.when
zend-server
google-shopping-api
ternary-operator
logical
seccomp
dojo-build
jeasyui
xcopy
teamviewer
flush
ibm-was
lowercase
nastran
jnlp
search-form
karma-coverage
console.log
whitespace-language
python-c-extension
sql-processor
jcheckbox
n-tier-architecture
relationships
git-reset
two.js
forceclose
property-injection
lsa
pbkdf2
vlab
lru
insertion
delphi-xe4
proc-open
2d-vector
nsfont
gwt-celltable
azure-acs
mediacontroller
nsconnection
formal-semantics
chunking
3-tier
odac
spring-modules
downcasting
code-camp

Resources

Encrypt Message