Advertisement
mauricioribeiro

Query Mongo 2 json

Nov 22nd, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 1.64 KB | None | 0 0
  1. [
  2.     // select max(datetime), sum(precipitation)
  3.     // from rain where source._id = 5bb4f8cd37a346769874ecdf and precipitation >= 4
  4.     // group by year, month, day
  5.     // order by datetime desc
  6.     /*
  7.     {
  8.         "_id": ..,
  9.         "source": {
  10.             "name": ...
  11.             "_id": ...
  12.         }
  13.     }
  14.     */
  15.     {
  16.         "$match": {
  17.             "$and": [
  18.                 {
  19.                     "source._id": "5bb4f8cd37a346769874ecdf"
  20.                 },
  21.                 {
  22.                     "precipitation": {
  23.                         "$gt": "4"
  24.                     }
  25.                 }
  26.             ]
  27.         }
  28.     },
  29.     {
  30.         "$group": {
  31.             "_id": {
  32.                 "year": {
  33.                     "$year": "$datetime"
  34.                 },
  35.                 "month": {
  36.                     "$month": "$datetime"
  37.                 },
  38.                 "day": {
  39.                     "$dayOfMonth": "$datetime"
  40.                 },
  41.                 "source\u1390_id": "$source._id",
  42.                 "source\u1390name": "$source.name"
  43.             },
  44.             "datetime": {
  45.                 "$max": "$datetime"
  46.             },
  47.             "precipitation": {
  48.                 "$sum": "$precipitation"
  49.             }
  50.         }
  51.     },
  52.     {
  53.         "$project": {
  54.             "_id": "1",
  55.             "source.name": "$_id.source\u1390name",
  56.             "source._id": "$_id.source\u1390_id",
  57.             "datetime": "1",
  58.             "location": "1",
  59.             "precipitation": "1"
  60.         }
  61.     },
  62.     {
  63.         "$sort": {
  64.             "datetime": "-1"
  65.         }
  66.     },
  67.     {
  68.         "$limit": "1"
  69.     }
  70. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement