0

I am working in MEAN STACK application.
I can not make mongoose query in mongoose like as MYSQL Query which is given bellow

SELECT _id, username
FROM user
ORDER BY FIELD(_id, "575123d687ed49be12404584", "575123d687ed49be12404587", "572d8b5aab1db7e1160a273a", "575123d687ed49be124045a5")

From this I want to get out put in following order using mongoose

first document (record) -> 575123d687ed49be12404584  
second document (record) -> 575123d687ed49be12404587  
third document (record) -> 572d8b5aab1db7e1160a273a  
fourth document (record) -> 575123d687ed49be124045a5  
remaining are in ascending order.

can any one give me a proper solution to get result in give ORDER BY.

Community
  • 1
  • 1
laxman
  • 1,338
  • 2
  • 10
  • 27

2 Answers2

0

you can try this:

user.find({_id : id}).sort({_id : 1}).exec(function(err,result){...});
Ravi Shankar Bharti
  • 8,922
  • 5
  • 28
  • 52
  • I know it but I want to sort by given ids first and then others are ascending not all ascending. – laxman Aug 15 '16 at 06:08
0

If User is the Mongoose Schema then:

User
  .find({_id : {$in: [11, 12, 14]}})
  .sort({_id : 1})
  .exec(function(err,result){

  });
Arun Ghosh
  • 7,634
  • 1
  • 26
  • 38
  • I know it but I want to sort by given ids first and then others are ascending not all ascending. – laxman Aug 15 '16 at 06:08