mongodb sort query taking more execution time

49 views
Skip to first unread message

sath...@amberconnect.com

unread,
Jan 23, 2018, 6:42:02 AM1/23/18
to mongodb-user
Hi,

Our MongoDB query is resulting fast but if add a sort attribute with that query .. it taking more time ... without sort it is fast ... any help?

eg: db.user.find().sort({ country: -1 });


--satheesh

Babyduck

unread,
Jan 23, 2018, 4:05:38 PM1/23/18
to mongodb-user
Have you tried indexing the field?

Like:

db.user.createIndex({ country: -1 })

sath...@amberconnect.com

unread,
Jan 23, 2018, 11:58:42 PM1/23/18
to mongodb-user
That field contains index already ... instead of country, we using DateTime as an attribute field...

Wan Bachtiar

unread,
Mar 8, 2018, 11:43:12 PM3/8/18
to mongodb-user

instead of country, we using DateTime as an attribute field…

Hi Sateesh,

It’s not clear what you meant by `attribute` field, but you need to have the field your sorting with in an index.
Please see Use Indexes to Sort Query Results

Also, you can analyse how your query is performing by utilising cursor.explain().
See Explain Results for more information.

Regards,
Wan.

Reply all
Reply to author
Forward
0 new messages