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

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

Categories

HOME
entity-framework
gaming
hdfs
openlayers-3
dronekit-python
cryptography
elisp
google-sheets-query
vue-resource
ipv6
azure-mobile-services
facebook-android-sdk
gtk
circleci
jquery-select2-4
angular4
jquery-file-upload
mpu6050
sonicwall
word2vec
gmp
http-authentication
codefluent
spring-ldap
anova
hdl
web-analytics
generator
firemonkey-style
data-collection
basic-authentication
openbr
source-insight
toad
fotorama
universal
testlink
microkernel
virtual-memory
cyanogenmod
image-registration
knockout-2.0
sharpdx
simple-schema
freshdesk
winmerge
patternlab.io
network-protocols
predicates
finder
rational-performance-test
p6spy
spring-mongo
xcopy
cryptojs
bayesglm
smart-tv
arbre
mousehover
lowercase
chicagoboss
nastran
blank-line
screen-orientation
mixins
css-paged-media
swagger-maven-plugin
google-gdk
explain
rfc5545
kognitio-wx2
nanomsg
fasterxml
string.format
multivariate-testing
full-text-indexing
roxy-fileman
nosql-aggregation
particle-swarm
eclim
aero
xmlwriter
coordinate
gamesalad
prototypal-inheritance
uploading
n-queens
multidrop-bus
filelock
cewolf
simile
qcar-sdk
printqueue
git-log
multiple-users
oracle-pro-c
powergui
downcasting
index.dat
acceleration

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