0

querying to mongodb collection to pinning last 24 hour object always come first with condition

[
   {
      "panel":"mobile",
      "createdAt":"2020-11-02T11:19:12+05:00"
   },
   {
      "panel":"web",
      "createdAt":"2020-11-02T11:16:12+05:00"
   },
   {
      "panel":"mobile",
      "createdAt":"2020-11-01T11:14:12+05:00"
   },
   {
      "panel":"web",
      "createdAt":"2020-11-02T11:13:12+05:00"
   },
   {
      "panel":"web",
      "createdAt":"2020-10-31T11:13:12+05:00"
   }
]

I need to do the query to always come first of last 24 hour data with condition where panel="web"

The expected result will be like this

[
   {
      "panel":"web",
      "createdAt":"2020-11-02T11:16:12+05:00"
   },
   {
      "panel":"web",
      "createdAt":"2020-11-02T11:13:12+05:00"
   },
   {
      "panel":"mobile",
      "createdAt":"2020-11-02T11:19:12+05:00"
   },
   {
      "panel":"mobile",
      "createdAt":"2020-11-01T11:14:12+05:00"
   },
   {
      "panel":"web",
      "createdAt":"2020-10-31T11:13:12+05:00"
   }
]

let suppose if I have panel : "mobile" at 4:05 pm and panel: "web" at 3:05 pm I need panel: "web" at first and this condition only applicable for the last 24 hours I don't need to apply this with older.

Ali Ahmed
  • 1
  • 2
  • What was the issue here? Why don't you use `$sort` and [this](https://stackoverflow.com/questions/2943222/find-objects-between-two-dates-mongodb) – Ashh Nov 02 '20 at 10:52
  • This is quite different, i need those object as well where panel="mobile" but panel="web" always comes first if it have in last 24 hours – Ali Ahmed Nov 02 '20 at 11:04
  • this is your answer? https://stackoverflow.com/questions/48244163/mongoose-query-for-documents-from-last-24-hours-only-one-document-per-hour – Babak Abadkheir Nov 02 '20 at 12:40

0 Answers0