josephxsxn

aggs101 - match, project

Feb 6th, 2023 (edited)
1,334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.63 KB | None | 0 0
  1.   var pipeline = [
  2.     {
  3.       $match: {
  4.         "imdb.rating": { $gte: 7 },
  5.         genres: { $nin: [ "Crime", "Horror" ] } ,
  6.         rated: { $in: ["PG", "G" ] },
  7.         languages: { $all: [ "English", "Japanese" ] }
  8.       }
  9.     }
  10.   ]
  11.  
  12. db.movies_sample.aggregate(pipeline)
  13.  
  14.  
  15.   var pipeline = [
  16.     {
  17.       $match: {
  18.         "imdb.rating": { $gte: 7 },
  19.         genres: { $nin: [ "Crime", "Horror" ] } ,
  20.         rated: { $in: ["PG", "G" ] },
  21.         languages: { $all: [ "English", "Japanese" ] }
  22.       }
  23.     },
  24.     {
  25.       $project: { "_id": 0, "title": 1, "rated": 1 }
  26.     }
  27.   ]
  28.  
  29.   var pipeline = [
  30.     {
  31.       $match: {
  32.         title: {
  33.           $type: "string"
  34.         }
  35.       }
  36.     },
  37.     {
  38.       $project: {
  39.         title: { $split: ["$title", " "] },
  40.         _id: 0
  41.       }
  42.     },
  43.     {
  44.       $match: {
  45.         title: { $size: 1 }
  46.       }
  47.     }
  48.   ]
  49.  
  50.  
  51. var pipeline = [
  52.   {
  53.     $match: {
  54.       cast: { $elemMatch: { $exists: true } },
  55.       directors: { $elemMatch: { $exists: true } },
  56.       writers: { $elemMatch: { $exists: true } }
  57.     }
  58.   },
  59.   {
  60.   $project: {
  61.     _id: 0,
  62.     cast: 1,
  63.     directors: 1,
  64.     writers: {
  65.         $map: {
  66.           input: "$writers",
  67.           as: "writer",
  68.           in: {
  69.             $arrayElemAt: [
  70.               { $split: ["$$writer", " ("] }, 0
  71.             ]
  72.           }
  73.         }
  74.       }
  75.     }
  76.   },
  77.   {
  78.     $project: {
  79.       labor_of_love: {
  80.         $gt: [ { $size: { $setIntersection: ["$cast", "$directors", "$writers"] } }, 0 ]
  81.       }
  82.     }
  83.   },
  84.   {
  85.     $match: { labor_of_love: true }
  86.   },
  87.   {
  88.     $count: "labors of love"
  89.   }
  90. ]
Advertisement
Add Comment
Please, Sign In to add comment