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

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

Categories

HOME
hpoo
orientdb
protocol-buffers
automapper
apple-push-notifications
xcode8.1
nsview
website
mongoid
tabs
portable-class-library
openflow
dji-sdk
google-search-console
angular-meteor
cisco
sap-fiori
chromecast
buffer
sslhandshakeexception
smartgwt
mailgun
django-rq
sonicwall
exec-maven-plugin
checkout
gmp
neo4j.rb
backtracking
ava
firemonkey-style
fusetools
plyr
strophe.js
sap-lumira
python-imageio
toad
windows2012
python-2.x
kendo-listview
android-canvas
cheat-engine
export-to-pdf
right-click
vb.net-to-c#
pyttsx
gcal
axis
qvtkwidget
swift2.3
nonlinear-functions
dex
gmaps.js
front-camera
oid
petsc
thread-exceptions
nvda
deferred-rendering
highlighting
gridbaglayout
certificate-authority
django-validation
odesk
openshift-cartridge
apache-commons-digester
mixins
knife
google-gdk
algebraixlib
ampersand
abstract-factory
string.format
jquery-mobile-flipswitch
webautomation
forceclose
bittorrent-sync
preorder
usb-flash-drive
iosched
assetic
code39
django-settings
application-server
jquery-lazyload
dibs
arel
jquery-cycle
xmlwriter
multiscaleimage
spark-view-engine
uploading
radscheduler
play2-mini
fork-join
ognl
servlet-container
regression-testing
active-record-query
suggestbox
watchpoint

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