python - How do I limit number of elements in updated array using $push and $slice in MongoEngine -
i have document array of subdocuments.
these document , subdocument like:
class activity_comment(embeddeddocument): author_name = stringfield() text_excerpt = stringfield() url = stringfield() class author_activity(document): author_id = stringfield(required=true,primary_key=true) author_name = stringfield() author_bio = stringfield() latest_comments = listfield(embeddeddocumentfield(activity_comment))
there multiple server processes modify the document @ same time. want use update_one
method upsert=true
.
i want restrict number of subdocuments in array 5. seems possible in mongodb according these links:
http://docs.mongodb.org/manual/tutorial/limit-number-of-elements-in-updated-array/ http://docs.mongodb.org/manual/reference/operator/update/slice/
these links suggest should use $push
insert subdocuments array , $slice
limit array length desired value.
however, haven't been able figure out how using mongoengine. tried following code
author_activity.objects(author_id="1").update_one(push__latest_comments=activity_comment,slice__latest_comments=5, upsert=true)
but threw following exception:
traceback (most recent call last): file "", line 1, in file "/newsoftheworld/local/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 467, in update_one upsert=upsert, multi=false, write_concern=write_concern, **update) file "/newsoftheworld/local/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 430, in update update = transform.update(queryset._document, **update) file "/newsoftheworld/local/lib/python2.7/site-packages/mongoengine/queryset/transform.py", line 181, in update raise invalidqueryerror(e) invalidqueryerror: cannot resolve field "slice"
what findandmodify returted new value? if reached limit - remove one.
Comments
Post a Comment