└── assignment.txt /assignment.txt: -------------------------------------------------------------------------------- 1 | 1. Write a MongoDB query to display all the documents in the collection restaurants. 2 | 3 | sol: db.restaurants.find() 4 | 5 | 2. Write a MongoDB query to display the fields restaurant_id, name, borough and cuisine for all the documents in the collection restaurant. 6 | 7 | sol: db.restaurants.find( 8 | {}, { restaurant_id: true, name: true, borough: true, cuisine:true } 9 | ) 10 | 11 | 3. Write a MongoDB query to display the fields restaurant_id, name, borough and cuisine, but exclude the field _id for all the documents in the collection restaurant. 12 | 13 | sol: db.restaurants.find( 14 | {}, { _id: false, restaurant_id: true, name: true, borough: true, cuisine:true } 15 | ) 16 | 17 | 4. Write a MongoDB query to display the fields restaurant_id, name, borough and zip code, but exclude the field _id for all the documents in the collection restaurant. 18 | 19 | sol: db.restaurants.find( 20 | {},{ _id: false, restaurant_id: true, name: true, borough: true, "address.zipcode":true } 21 | ) 22 | 23 | 5. Write a MongoDB query to display all the restaurant which is in the borough Bronx. 24 | 25 | sol: db.restaurants.find( 26 | { borough: "Bronx"} 27 | ) 28 | 29 | 6. Write a MongoDB query to display the first 5 restaurant which is in the borough Bronx. 30 | 31 | sol: db.restaurants.find( 32 | { borough: "Bronx"} 33 | ).limit(5) 34 | 35 | 7.Write a MongoDB query to display the next 5 restaurants after skipping first 5 which are in the borough Bronx. 36 | 37 | sol: db.restaurants.find( 38 | { borough: "Bronx"} 39 | ).skip(5).limit(5) 40 | 41 | 8. Write a MongoDB query to find the restaurants who achieved a score more than 90. 42 | 43 | sol: db.restaurants.find({grades : { $elemMatch:{"score":{$gt : 90}}}}); or db.restaurants.find({ "grades.score": { $gt: 90}}) 44 | 45 | 9. Write a MongoDB query to find the restaurants that achieved a score, more than 80 but less than 100. 46 | 47 | sol: db.restaurants.find({grades : { $elemMatch:{"score":{$gt : 80, $lt: 100}}}}); 48 | 49 | 10. Write a MongoDB query to find the restaurants which locate in latitude value less than -95.754168 50 | 51 | sol: db.restaurants.find({"address.coord": { $lt: -95.754168}}); 52 | 53 | 11. Write a MongoDB query to find the restaurants that do not prepare any cuisine of 'American' and their grade score more than 70 and latitude less than -65.754168. 54 | 55 | sol:db.restaurants.find( 56 | {"cuisine" : {$ne :"American "}, 57 | "grades.score" : {$gt : 70}, 58 | "address.coord" : {$lt : -65.754168}} 59 | ); 60 | 61 | 12. Write a MongoDB query to find the restaurants which do not prepare any cuisine of 'American' and achieved a score more than 70 and located in the longitude less than -65.754168. 62 | 63 | sol: db.restaurants.find( 64 | {"cuisine" : {$ne :"American "}, 65 | "grades.score" : {$gt : 70}, 66 | "address.coord" : {$lt : -65.754168}} 67 | ); 68 | 69 | 13. Write a MongoDB query to find the restaurants which do not prepare any cuisine of 'American ' and achieved a grade point 'A' not belongs to the borough Brooklyn. The document must be displayed according to the cuisine in descending order. 70 | 71 | sol: db.restaurants.find( 72 | 73 | {"cuisine" : {$ne :"American "}, 74 | "grades.grade" : "A", 75 | "borough" : { $ne: "Brooklyn"}} 76 | ).sort( { cuisine: -1 } ); 77 | 78 | 14. Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which contain 'Wil' as first three letters for its name. 79 | 80 | sol: db.restaurants.find( 81 | {name: /^Wil/}, 82 | {restaurant_id: true, name:true, borough: true, cuisine: true,} 83 | ); 84 | 85 | 15. Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which contain 'ces' as last three letters for its name. 86 | 87 | sol: db.restaurants.find( 88 | {name: /ces$/}, 89 | {restaurant_id: true, name:true, borough: true, cuisine: true,} 90 | ); 91 | 92 | 16. Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which contain 'Reg' as three letters somewhere in its name. 93 | 94 | sol: db.restaurants.find( 95 | {name: /Reg/}, 96 | {restaurant_id: true, name:true, borough: true, cuisine: true,} 97 | ); 98 | 99 | 17. Write a MongoDB query to find the restaurants which belong to the borough Bronx and prepared either American or Chinese dish. 100 | 101 | sol: db.restaurants.find( 102 | { borough: "Bronx", 103 | cuisine: { 104 | $in: ["American ", "Chinese"]}} 105 | ); 106 | 107 | 18. Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which belong to the borough Staten Island or Queens or Bronxor Brooklyn. 108 | 109 | sol: db.restaurants.find( 110 | {borough: { 111 | $in: ["Bronx", "Staten Island", "Brooklyn", "Queens"]}}, 112 | {restaurant_id: true, name:true, borough: true, cuisine: true,} 113 | ); 114 | 115 | 19. Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which are not belonging to the borough Staten Island or Queens or Bronxor Brooklyn. 116 | 117 | sol: db.restaurants.find( 118 | {borough: { 119 | $nin: ["Bronx", "Staten Island", "Brooklyn", "Queens"]}}, 120 | {restaurant_id: true, name:true, borough: true, cuisine: true,} 121 | ); 122 | 123 | 20. Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which achieved a score which is not more than 10. 124 | 125 | sol: db.restaurants.find( 126 | {"grades.score": { $not: { $gt: 10}}}, 127 | {restaurant_id: true, name:true, borough: true, cuisine: true,} 128 | ); 129 | 130 | 21. Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which prepared dish except 'American' and 'Chinees' or restaurant's name begins with letter 'Wil'. 131 | 132 | sol: db.restaurants.find( 133 | { $or: [ 134 | { cuisine: { 135 | $nin: ["American ", "Chinese"]} }, 136 | {name: /^Wil/} 137 | ]}, 138 | {restaurant_id: true, name:true, borough: true, cuisine: true,} 139 | ); 140 | 141 | 22. Write a MongoDB query to find the restaurant Id, name, and grades for those restaurants which achieved a grade of "A" and scored 11 on an ISODate "2014-08-11T00:00:00Z" among many of survey dates.. 142 | 143 | sol: db.restaurants.find( 144 | { 145 | "grades.date": ISODate("2014-08-11T00:00:00Z"), 146 | "grades.grade":"A" , 147 | "grades.score" : 11 148 | }, 149 | {"restaurant_id" : 1,"name":1,"grades":1} 150 | ); 151 | 152 | 23. Write a MongoDB query to find the restaurant Id, name and grades for those restaurants where the 2nd element of grades array contains a grade of "A" and score 9 on an ISODate "2014-08-11T00:00:00Z". 153 | 154 | sol: db.restaurants.find( 155 | { 156 | "grades.1.date": ISODate("2014-08-11T00:00:00Z"), 157 | "grades.1.grade":"A" , 158 | "grades.1.score" : 9 159 | }, 160 | {"restaurant_id" : 1,"name":1,"grades":1} 161 | ); 162 | 163 | 164 | 24. Write a MongoDB query to find the restaurant Id, name, address and geographical location for those restaurants where 2nd element of coord array contains a value which is more than 42 and upto 52.. 165 | 166 | sol: db.restaurants.find( 167 | { 168 | "address.coord.1": { 169 | $gt:42, 170 | $lte:52 171 | } }, 172 | {"restaurant_id" : 1,"name":1,"address": 1} 173 | ); 174 | 175 | 25. Write a MongoDB query to arrange the name of the restaurants in ascending order along with all the columns. 176 | 177 | sol: db.restaurants.find( 178 | { } 179 | ).sort({ name: 1}); 180 | 181 | 26. Write a MongoDB query to arrange the name of the restaurants in descending along with all the columns. 182 | 183 | sol: db.restaurants.find( 184 | { 185 | } 186 | ).sort({ name: -1}); 187 | 188 | 27. Write a MongoDB query to arranged the name of the cuisine in ascending order and for that same cuisine borough should be in descending order. 189 | 190 | sol: db.restaurants.find().sort( 191 | {"cuisine":1,"borough" : -1,} 192 | ); 193 | 194 | 195 | 28. Write a MongoDB query to know whether all the addresses contains the street or not. 196 | 197 | sol: db.restaurants.find( 198 | {"address.street" : 199 | { $exists : true } 200 | } 201 | ); 202 | 203 | 29. Write a MongoDB query which will select all documents in the restaurants collection where the coord field value is Double. 204 | // 1 is the alias for "double" bson type; 205 | sol: db.restaurants.find( 206 | {"address.coord" : 207 | {$type : 1} 208 | } 209 | ); 210 | 211 | 30. Write a MongoDB query which will select the restaurant Id, name and grades for those restaurants which returns 0 as a remainder after dividing the score by 7. 212 | 213 | sol: db.restaurants.find( 214 | {"grades.score" : 215 | {$mod : [7,0]} 216 | }, 217 | {"restaurant_id" : 1,"name":1,"grades":1} 218 | ); 219 | 220 | 31. Write a MongoDB query to find the restaurant name, borough, longitude and attitude and cuisine for those restaurants which contains 'mon' as three letters somewhere in its name. 221 | 222 | sol: db.restaurants.find( 223 | { 224 | name: /mon/i 225 | }, 226 | {"restaurant_id" : 1,"name":1,"borough":1, "cuisine":1, "address.coord":1} 227 | ); 228 | 229 | 32. Write a MongoDB query to find the restaurant name, borough, longitude and latitude and cuisine for those restaurants which contain 'Mad' as first three letters of its name. 230 | sol: db.restaurants.find( 231 | { 232 | name: /^Mad/ 233 | }, 234 | {"restaurant_id" : 1,"name":1,"borough":1, "cuisine":1, "address.coord":1} 235 | ); --------------------------------------------------------------------------------