Mongoengine can push elements to Lists by appending them
BlogPost.objects(id=post.id).update_one(push__tags='nosql')
I want to prepend instead of appending, is there a way ?
Another alternate question to the same problem ..
I can query in a list by position like this
BlogPost.objects(tags__0='nosql')
Is there a way to specify the last element in the list, like -1 index in python lists ?
BlogPost.objects(tags__-1='nosql')# ?.. I wish !
Answers to any of two question will solve my problem.
Thanks in advance.
Neither of those operations is possible, but you can work around it fairly easily by emulating access to the last array element with another field. Suppose your model is:
Add a field
last_tag:Then, when updating:
And when querying:
You’ll want to make sure that
last_tagis indexed, and possibly alsotagsif you query by that frequently as well.