pymongo


Running code when mongoengine loads a document


We are trying to run code that modifies a document when it loads as part of schema maintenance. We have a document such as
from mongoengine import Document
from mongoengine.fields import IntField, StringField
class User(Document):
version = IntField(default=0)
name = StringField()
Instances of User are created with version=1 and saved.
Later on, we modify this class as follows:
class User(Document):
version = IntField(default=0)
name = StringField(max_length=20)
Some of the version 1 documents now need to be truncated:
def upgrade_1_to_2(self):
self.name = self.name[:20]
We want to automatically run that function whenever a User is retrieved from the database, and only if version == 1. (Future upgrades would be upgrade_2_to_3(), and so on.)
Where in this API can I put code that runs when a document is retrieved?
Looks like it's a good candidate for the post_init signal and you can do a check there.
See: http://docs.mongoengine.org/guide/signals.html

Related Links

How to update Saved Document Extra data in GridFS
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

Categories

HOME
extjs
xcode
yaml
barcode-scanner
bots
loopbackjs
plpgsql
google-webmaster-tools
vue-resource
video-streaming
elastic-load-balancer
outlook-restapi
google-awareness
office-ui-fabric
nose
codefluent
significance
jitsi
backtracking
maven-surefire-plugin
connection-refused
chronicle-queue
high-availability
strophe.js
xdebug
marquee
math.js
bonita
dnsmasq
premake
windows2012
universal
kendo-listview
advanced-filter
cmis
numerics
nsuserdefaults
forms-authentication
task-parallel-library
plane
grails3.2.0
dbcc
code-push
xcode8.2
jgrapht
sharpdx
karabiner
palindrome
sesame
emacs25
.when
rustdoc
front-camera
researchkit
dcount
system.net.mail
dpkg
qtcpsocket
android-4.2-jelly-bean
declarative
piping
accessibilityservice
wikimedia-commons
helm
textpattern
pythonxy
swagger-maven-plugin
bootstrap-wysiwyg
com0com
python-curses
tidyr
dmake
igor
communicate
rpg
accessory
nachos
neo4jphp
.htpasswd
sqlbindparameter
nodeload
tabletools
gpu-programming
property-injection
mod-perl
facebook-game-groups
hints
node.js-stream
jacob
pacman
insertion
chartfx
sly-scroller
eager-loading
multiscaleimage
xcode4.5
websphere-6.1
zotonic
uploading
kolite
sql-parametrized-query
kohana-auth
django-pagination
printqueue
gaelyk
urchin
rawcontacts
index.dat
downloadfile

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