1

I want to order the records in result set. How can I receive the results in MongoDB like what I expected. In MySQL, I can use the following query to get my expected way of sort.

But how can I get in MongoDB.

SELECT * FROM fruit 
ORDER BY FIELD(name, 'Banana', 'Apple', 'Pear', 'Orange'), variety;

Please advice. Thanks!

2plus
  • 261
  • 1
  • 7
  • 16
  • The sort order for results in MongoDB either has to be based on a field present in the documents you are querying, or a field calculated/projected in an [Aggregation Framework](http://docs.mongodb.org/manual/core/aggregation-pipeline/) pipeline. – Stennie Aug 10 '14 at 14:14

1 Answers1

0

I guess this will help:

  db.users.find({},{_id:0,"accounts":1,"name":1});

But i have never tried ordering fields.

SUNDARRAJAN K
  • 2,237
  • 2
  • 22
  • 38