Get newly added document first from collections.

15 views
Skip to first unread message

victor fienkelstein

unread,
Jun 17, 2021, 10:27:59 PM6/17/21
to MongoEngine Users
Hey Folks,
I am new to mongoengine and working on one flask app in that I need the data in descending order like newly added data show first.
I have a model name User
class User(db.Document):
    name = db.StringField()
     age = db.IntField()

I am querying like this :-  data = userdb.objects()
I am getting data in ascending order, but i need in descending order. 
Can anyone please tell me how to do .

ThankYou :)

Paulo Cauca

unread,
Jun 18, 2021, 8:29:00 AM6/18/21
to mongoeng...@googlegroups.com
Hey Victor ,

Try put data = userdb.objects().order_by('-name') 

 The order may be specified by prepending each of the keys by a "+" or
        a "-". Ascending order is assumed if there's no prefix.

        If no keys are passed, existing ordering is cleared instead.

        :param keys: fields to order the query results by; keys may be
            prefixed with "+" or a "-" to determine the ordering direction.


--
You received this message because you are subscribed to the Google Groups "MongoEngine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoengine-us...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongoengine-users/85b0be1f-4980-45f7-a26f-83ec5ad15c1an%40googlegroups.com.

victor fienkelstein

unread,
Jun 20, 2021, 8:34:14 AM6/20/21
to MongoEngine Users
Thanks...
Reply all
Reply to author
Forward
0 new messages