Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. /*
  2.  
  3. RESTAURANT COLLECTION SINGLE DOCUMENT SCHEMA FOR REFERENCE
  4. {
  5. "_id" : ObjectId("5d83eb456e04f8d82b8a8363"),
  6. "address" : {
  7. "building" : "2780",
  8. "coord" : [
  9. -73.98242,
  10. 40.579505
  11. ],
  12. "street" : "Stillwell Avenue",
  13. "zipcode" : "11224"
  14. },
  15. "borough" : "Brooklyn",
  16. "cuisine" : "American ",
  17. "grades" : [
  18. {
  19. "date": ISODate("2014-06-10T00:00:00.000Z"),
  20. "grade": "A",
  21. "score": 5
  22. },
  23. {
  24. "date": ISODate("2013-06-05T00:00:00.000Z"),
  25. "grade": "A",
  26. "score": 7
  27. },
  28. {
  29. "date": ISODate("2012-04-13T00:00:00.000Z"),
  30. "grade": "A",
  31. "score": 12
  32. },
  33. {
  34. "date": ISODate("2011-10-12T00:00:00.000Z"),
  35. "grade": "A",
  36. "score": 12
  37. }
  38. ],
  39. "name" : "Riviera Caterer",
  40. "restaurant_id" : "40356018"
  41. }
  42. */
  43.  
  44.  
  45.  
  46.  
  47.  
  48. db.getCollection('restaurants').aggregate([
  49. {
  50. "$project": {
  51. "filtered_grades": {
  52. "$filter": {
  53. "input": "$grades",
  54. "as": "grade",
  55. "cond": { "$in": [ "$$grade.score", [ 12, 2 ] ] }
  56. }
  57. }
  58. }
  59. }
  60. ])
  61. /*
  62. result
  63.  
  64. filters only the selected subdocuments from the array of subdocuments 'grades'
  65.  
  66.  
  67. */
  68.  
  69.  
  70.  
  71. // just queries
  72. /*
  73.  
  74. db.getCollection('vendors').aggregate([
  75. {
  76. "$match": {
  77. "category": { $in: ['onion'] }
  78. }
  79. },
  80.  
  81. {
  82. "$lookup": {
  83. "from": "products",
  84. "localField": "products",
  85. "foreignField": "_id",
  86.  
  87.  
  88. "as": "productObjects"
  89. }
  90. },
  91.  
  92. {
  93. "$project": {
  94. "filtered_products": {
  95. "$filter": {
  96. "input": "$productObjects",
  97. "as": "products",
  98. "cond": { "$in": [ "$$products.productname", ["onion","tomato"] ] }
  99. }
  100. }
  101. }
  102. },
  103.  
  104. {
  105. "$project": {
  106. "prods":"$filtered_products" ,
  107. "total":{$size:"$filtered_products"}
  108. }
  109. }
  110.  
  111. ]);
  112.  
  113.  
  114.  
  115.  
  116.  
  117. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement