├── .gitignore ├── Dataset.json ├── README.md ├── customers.json └── products.json /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # ASP.NET Scaffolding 66 | ScaffoldingReadMe.txt 67 | 68 | # StyleCop 69 | StyleCopReport.xml 70 | 71 | # Files built by Visual Studio 72 | *_i.c 73 | *_p.c 74 | *_h.h 75 | *.ilk 76 | *.meta 77 | *.obj 78 | *.iobj 79 | *.pch 80 | *.pdb 81 | *.ipdb 82 | *.pgc 83 | *.pgd 84 | *.rsp 85 | *.sbr 86 | *.tlb 87 | *.tli 88 | *.tlh 89 | *.tmp 90 | *.tmp_proj 91 | *_wpftmp.csproj 92 | *.log 93 | *.tlog 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 298 | *.vbp 299 | 300 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 301 | *.dsw 302 | *.dsp 303 | 304 | # Visual Studio 6 technical files 305 | *.ncb 306 | *.aps 307 | 308 | # Visual Studio LightSwitch build output 309 | **/*.HTMLClient/GeneratedArtifacts 310 | **/*.DesktopClient/GeneratedArtifacts 311 | **/*.DesktopClient/ModelManifest.xml 312 | **/*.Server/GeneratedArtifacts 313 | **/*.Server/ModelManifest.xml 314 | _Pvt_Extensions 315 | 316 | # Paket dependency manager 317 | .paket/paket.exe 318 | paket-files/ 319 | 320 | # FAKE - F# Make 321 | .fake/ 322 | 323 | # CodeRush personal settings 324 | .cr/personal 325 | 326 | # Python Tools for Visual Studio (PTVS) 327 | __pycache__/ 328 | *.pyc 329 | 330 | # Cake - Uncomment if you are using it 331 | # tools/** 332 | # !tools/packages.config 333 | 334 | # Tabs Studio 335 | *.tss 336 | 337 | # Telerik's JustMock configuration file 338 | *.jmconfig 339 | 340 | # BizTalk build output 341 | *.btp.cs 342 | *.btm.cs 343 | *.odx.cs 344 | *.xsd.cs 345 | 346 | # OpenCover UI analysis results 347 | OpenCover/ 348 | 349 | # Azure Stream Analytics local run output 350 | ASALocalRun/ 351 | 352 | # MSBuild Binary and Structured Log 353 | *.binlog 354 | 355 | # NVidia Nsight GPU debugger configuration file 356 | *.nvuser 357 | 358 | # MFractors (Xamarin productivity tool) working folder 359 | .mfractor/ 360 | 361 | # Local History for Visual Studio 362 | .localhistory/ 363 | 364 | # Visual Studio History (VSHistory) files 365 | .vshistory/ 366 | 367 | # BeatPulse healthcheck temp database 368 | healthchecksdb 369 | 370 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 371 | MigrationBackup/ 372 | 373 | # Ionide (cross platform F# VS Code tools) working folder 374 | .ionide/ 375 | 376 | # Fody - auto-generated XML schema 377 | FodyWeavers.xsd 378 | 379 | # VS Code files for those working on multiple tools 380 | .vscode/* 381 | !.vscode/settings.json 382 | !.vscode/tasks.json 383 | !.vscode/launch.json 384 | !.vscode/extensions.json 385 | *.code-workspace 386 | 387 | # Local History for Visual Studio Code 388 | .history/ 389 | 390 | # Windows Installer files from build outputs 391 | *.cab 392 | *.msi 393 | *.msix 394 | *.msm 395 | *.msp 396 | 397 | # JetBrains Rider 398 | *.sln.iml 399 | -------------------------------------------------------------------------------- /Dataset.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "Iphone 15", 4 | "price": 80000, 5 | "brand": "Apple", 6 | "productType": "Mobile", 7 | "categories": ["electronics"] 8 | }, 9 | { 10 | "name": "Iphone 14 Pro Max", 11 | "price": 115000, 12 | "brand": "Apple", 13 | "productType": "Mobile", 14 | "categories": ["electronics"] 15 | }, 16 | { 17 | "name": "Samsung Galaxy A50", 18 | "price": 54000, 19 | "brand": "Samsung", 20 | "productType": "Mobile", 21 | "categories": ["electronics"] 22 | }, 23 | { 24 | "name": "Nothing Phone 2a", 25 | "price": 56000, 26 | "brand": "Nothing", 27 | "productType": "Mobile", 28 | "categories": ["electronics"] 29 | }, 30 | { 31 | "name": "Samsung Galaxy s24 Ultra", 32 | "price": 93000, 33 | "brand": "Samsung", 34 | "productType": "Mobile", 35 | "categories": ["electronics"] 36 | }, 37 | { 38 | "name": "Bravia 43 inch 4k", 39 | "price": 45000, 40 | "brand": "Samsung", 41 | "productType": "TV", 42 | "categories": ["electronics"] 43 | }, 44 | { 45 | "name": "Mac Book Pro", 46 | "price": 120000, 47 | "brand": "Apple", 48 | "productType": "Laptop", 49 | "specification": { 50 | "ram": "8 GB", 51 | "processor": "M1", 52 | "storageType": "SSD", 53 | "storageSize": "512 GB" 54 | }, 55 | "categories": ["electronics"] 56 | }, 57 | { 58 | "name": "Lenovo Legion", 59 | "price": 120000, 60 | "brand": "Lenovo", 61 | "productType": "Laptop", 62 | "specification": { 63 | "ram": "16 GB", 64 | "processor": "i7 13th Gen", 65 | "storageType": "SSD", 66 | "storageSize": "1 TB" 67 | }, 68 | "categories": ["electronics"] 69 | }, 70 | { 71 | "name": "MSI Katna 15", 72 | "price": 98000, 73 | "brand": "MSI", 74 | "productType": "Laptop", 75 | "specification": { 76 | "ram": "16 GB", 77 | "processor": "i7 12th Gen", 78 | "storageType": "SSD", 79 | "storageSize": "512 GB" 80 | }, 81 | "categories": ["electronics"] 82 | }, 83 | { 84 | "name": "MSI Katna 15 Pro", 85 | "price": 130000, 86 | "brand": "MSI", 87 | "productType": "Laptop", 88 | "specification": { 89 | "ram": "16 GB", 90 | "processor": "i7 13th Gen", 91 | "storageType": "SSD", 92 | "storageSize": "1 TB" 93 | }, 94 | "categories": ["electronics"] 95 | }, 96 | { 97 | "_id": "666982f9132be8d964cdce53", 98 | "name": "Samsung RT28T3022S8", 99 | "price": 25000, 100 | "brand": "Samsung", 101 | "productType": "Refrigerator", 102 | "specification": { 103 | "capacity": "253L", 104 | "energyRating": "3 Star" 105 | }, 106 | "categories": ["kitchen_appliances"] 107 | }, 108 | { 109 | "_id": "666982f9132be8d964cdce54", 110 | "name": "LG MC2846BG", 111 | "price": 15000, 112 | "brand": "LG", 113 | "productType": "Microwave Oven", 114 | "specification": { 115 | "type": "Convection", 116 | "capacity": "28L" 117 | }, 118 | "categories": ["kitchen_appliances"] 119 | }, 120 | { 121 | "_id": "666982f9132be8d964cdce55", 122 | "name": "Whirlpool IF278", 123 | "price": 22000, 124 | "brand": "Whirlpool", 125 | "productType": "Refrigerator", 126 | "specification": { 127 | "capacity": "265L", 128 | "energyRating": "4 Star" 129 | }, 130 | "categories": ["kitchen_appliances"] 131 | }, 132 | { 133 | "_id": "666982f9132be8d964cdce56", 134 | "name": "IFB 30BRC2", 135 | "price": 20000, 136 | "brand": "IFB", 137 | "productType": "Microwave Oven", 138 | "specification": { 139 | "type": "Convection", 140 | "capacity": "30L" 141 | }, 142 | "categories": ["kitchen_appliances"] 143 | }, 144 | { 145 | "_id": "666982f9132be8d964cdce57", 146 | "name": "Samsung WW60R20GLMA", 147 | "price": 28000, 148 | "brand": "Samsung", 149 | "productType": "Washing Machine", 150 | "specification": { 151 | "capacity": "6 kg", 152 | "type": "Front Load" 153 | }, 154 | "categories": ["laundry_appliances"] 155 | }, 156 | { 157 | "_id": "666982f9132be8d964cdce58", 158 | "name": "LG T7281NDDLG", 159 | "price": 18000, 160 | "brand": "LG", 161 | "productType": "Washing Machine", 162 | "specification": { 163 | "capacity": "6.2 kg", 164 | "type": "Top Load" 165 | }, 166 | "categories": ["laundry_appliances"] 167 | }, 168 | { 169 | "_id": "666982f9132be8d964cdce59", 170 | "name": "Philips HD6975/00", 171 | "price": 7000, 172 | "brand": "Philips", 173 | "productType": "Oven Toaster Grill", 174 | "specification": { 175 | "capacity": "25L", 176 | "powerConsumption": "1500W" 177 | }, 178 | "categories": ["kitchen_appliances"] 179 | }, 180 | { 181 | "_id": "666982f9132be8d964cdce5a", 182 | "name": "Dyson V11 Absolute Pro", 183 | "price": 52000, 184 | "brand": "Dyson", 185 | "productType": "Vacuum Cleaner", 186 | "specification": { 187 | "type": "Cord-free", 188 | "runTime": "60 minutes" 189 | }, 190 | "categories": ["home_cleaning"] 191 | }, 192 | { 193 | "_id": "666982f9132be8d964cdce5b", 194 | "name": "Eureka Forbes Aquaguard Marvel", 195 | "price": 15000, 196 | "brand": "Eureka Forbes", 197 | "productType": "Water Purifier", 198 | "specification": { 199 | "type": "RO+UV", 200 | "storageCapacity": "8L" 201 | }, 202 | "categories": ["water_appliances"] 203 | }, 204 | { 205 | "_id": "666982f9132be8d964cdce5c", 206 | "name": "Kent Grand Plus", 207 | "price": 19000, 208 | "brand": "Kent", 209 | "productType": "Water Purifier", 210 | "specification": { 211 | "type": "RO+UV+UF", 212 | "storageCapacity": "9L" 213 | }, 214 | "categories": ["water_appliances"] 215 | } 216 | ] 217 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MongoDB Complete Course 2 | 3 | ##### Youtube - [https://www.youtube.com/@codewithkarthik](https://www.youtube.com/@codewithkarthik) 4 | 5 | ![https://www.youtube.com/@codewithkarthik](https://yt3.ggpht.com/twg8jbKidn4ovJaChPm2X5de9fRGlwt5xvltlqi7KltzzfMsHyMBjF8Q6iDP7KVR_mF8kWQs=s88-c-k-c0x00ffffff-no-rj) 6 | 7 | # How to Configure MongoDB 8 | 9 | ### Step 1 10 | 11 | ##### Install MongoDB Database 12 | 13 | Download MongoDB - [https://www.mongodb.com/try/download/community](https://www.mongodb.com/try/download/community) 14 | 15 | ### Step 2 16 | 17 | ##### Install MongoDB Shell 18 | 19 | Download MongoDB Shell - [https://www.mongodb.com/try/download/shell](https://www.mongodb.com/try/download/shell) 20 | 21 | ### Step 3 22 | 23 | Configure Enviroment Variables As Per Tutorial Video - [https://youtu.be/0zwYbudzaJc](https://youtu.be/0zwYbudzaJc) 24 | 25 | --- 26 | 27 | # No-SQL Commands 28 | 29 | #### Connecting to MongoDB Server 30 | 31 | ```bash 32 | Step 1: Open command prompt(cmd) in your system 33 | Step 2: Type 'mongosh' then hit enter key 34 | ``` 35 | 36 | --- 37 | 38 | #### Show database 39 | 40 | ###### The following command is used to show avaliable database list. 41 | 42 | ```bash 43 | show dbs 44 | ``` 45 | 46 | --- 47 | 48 | #### Command to connect database 49 | 50 | ###### The following command is used to connect with database even if database is not exists it will create new database. 51 | 52 | ```bash 53 | #Syntax 54 | use [database name] 55 | ``` 56 | 57 | ```bash 58 | #Example 59 | use maxishop 60 | ``` 61 | 62 | --- 63 | 64 | #### Creating Collection & Document 65 | 66 | ```bash 67 | #Syntax 68 | db.collectionName.insertOne({document}) 69 | ``` 70 | 71 | ```bash 72 | #Example 73 | db.products.insertOne({name:'Iphone 15',price:80000}) 74 | 75 | ``` 76 | 77 | --- 78 | 79 | #### To get all the data from the collection 80 | 81 | ```bash 82 | #Syntax 83 | db.collectionName.find() 84 | ``` 85 | 86 | ```bash 87 | #Example 88 | db.products.find() 89 | 90 | #For Older version 91 | db.products.find().pretty() 92 | ``` 93 | 94 | --- 95 | 96 | #### Creating Document with different schema or structure 97 | 98 | ```bash 99 | #Example 100 | db.products.insertOne({name:'Mac Book Pro',price:120000,brand:'Apple'}) 101 | ``` 102 | 103 | ```bash 104 | db.products.insertOne({name:'Samsung Galaxy s24 Ultra',price:120000,brand:'Samsung'}) 105 | ``` 106 | 107 | ```bash 108 | db.products.insertOne({name:'Bravia 43 inch 4k',price:120000,brand:'Samsung',productType:'TV'}) 109 | ``` 110 | 111 | #### Embedded Document 112 | 113 | ##### Instead Document in Collections 114 | 115 | ```bash 116 | db.products.insertOne({name:'Lenovo Legion',price:120000,brand:'Lenovo',productType:'Laptop',specification:{ram:'16 GB',processor:'i7 13th Gen',storageType:'SSD',storageSize:'1 TB'}}) 117 | ``` 118 | 119 | ```bash 120 | #for easy understanding 121 | db.products 122 | .insertOne( 123 | { 124 | name:'Lenovo Legion', 125 | price:120000, 126 | brand:'Lenovo', 127 | productType:'Laptop' 128 | specification:{ 129 | ram:'16 GB', 130 | processor: 'i7 13th Gen', 131 | storageType:'SSD', 132 | storageSize: '1 TB' 133 | } 134 | }) 135 | ``` 136 | 137 | you can also provide your own id as well like the following example. 138 | 139 | ###### note: Id should be unique, no duplicate key 140 | 141 | ```bash 142 | db.products.insertOne({_id:'AX1000001',name:'MSI Katna 15',price:98000,brand:'MSI',productType:'Laptop',specification:{ram:'16 GB',processor:'i7 12th Gen',storageType:'SSD',storageSize:'512 GB'}}) 143 | ``` 144 | 145 | ```bash 146 | #for easy understanding 147 | db.products 148 | .insertOne( 149 | { 150 | _id:'AX1000001' 151 | name:'MSI Katna 15', 152 | price:98000, 153 | brand:'MSI', 154 | productType:'Laptop' 155 | specification:{ 156 | ram:'16 GB', 157 | processor: 'i7 12th Gen', 158 | storageType:'SSD', 159 | storageSize: '512 GB' 160 | } 161 | }) 162 | ``` 163 | 164 | --- 165 | 166 | #### Datatypes 167 | 168 | ```bash 169 | use temp 170 | ``` 171 | 172 | ```bash 173 | db.orders.insertOne({quantity:50,totalAmount:1234567890123456,discount:0.10,status:'warehouse',isActive:true,details:{address:'Chennai,Tamil Nadu,India',pincode:600044},phoneNo: [123456789,987654321],orderDate:new Date(),createdAt:new Timestamp()}) 174 | ``` 175 | 176 | ```bash 177 | db.orders.find() 178 | ``` 179 | 180 | ```bash 181 | db.orders.drop() 182 | ``` 183 | 184 | ```bash 185 | db.orders.insertOne({quantity:NumberInt(50),totalAmount:NumberLong('1234567890123456'),discount:0.10,status:'warehouse',isActive:true,details:{address:'Chennai,Tamil Nadu,India',pincode:NumberInt(600044)},phoneNo: [NumberLong('123456789'),NumberLong('987654321')],orderDate:new Date(),createdAt:new Timestamp()}) 186 | ``` 187 | 188 | ```bash 189 | db.orders.find() 190 | ``` 191 | 192 | --- 193 | 194 | #### CRUD Operation 195 | 196 | 1. Create 197 | 198 | - insertOne(data,options) 199 | - insertMany(data,options) 200 | 201 | 2. Read 202 | 203 | - find(filter,options) 204 | - findOne(filter,options) #first matching document 205 | 206 | 3. Update 207 | 208 | - updateOne(filter,data,options) 209 | - updateMany(filter,data,options) 210 | - replaceOne(filter,data,options) 211 | 212 | 4. Delete 213 | - deleteOne(filter,options) 214 | - deleteMany(filter,options) 215 | 216 | --- 217 | 218 | ## Create Document In Collection 219 | 220 | #### Inserting Single Record 221 | 222 | ```bash 223 | db.products.insertOne({name:'Nothing Phone 2a',price:56000,brand:'Nothing',productType:'Mobile'}) 224 | ``` 225 | 226 | ```bash 227 | db.products.insertOne({_id:'AX1000002',name:'MSI Katna 15 Pro',price:130000,brand:'MSI',productType:'Laptop',specification:{ram:'16 GB',processor:'i7 13th Gen',storageType:'SSD',storageSize:'1 TB'}}) 228 | ``` 229 | 230 | ```bash 231 | #for easy understanding 232 | db.products 233 | .insertOne( 234 | { 235 | _id:'AX1000002' 236 | name:'MSI Katna 15 Pro', 237 | price:130000, 238 | brand:'MSI', 239 | productType:'Laptop' 240 | specification:{ 241 | ram:"16 GB", 242 | processor: 'i7 13th Gen', 243 | storageType:'SSD', 244 | storageSize: '1 TB' 245 | } 246 | }) 247 | ``` 248 | 249 | #### Inserting Many Records 250 | 251 | ```bash 252 | db.products.insertMany([{name:'Iphone 14',price:75000,brand:'Apple'},{name:'Samsung Galaxy A50',price:54000,brand:'Samsung'}]) 253 | ``` 254 | 255 | --- 256 | 257 | ## Read document from collection 258 | 259 | ```bash 260 | db.products.find() 261 | ``` 262 | 263 | ##### Filter 264 | 265 | ```bash 266 | db.products.find({brand:'Apple'}) 267 | ``` 268 | 269 | ```bash 270 | db.products.findOne({brand:'Apple'}) 271 | ``` 272 | 273 | --- 274 | 275 | ## Update document in collection 276 | 277 | #### Updating Single Record 278 | 279 | ```bash 280 | db.products.updateOne({name:'Mac Book Pro'},{$set:{productType:'Laptop'}}) 281 | ``` 282 | 283 | ```bash 284 | db.products.updateOne({name:'Samsung Galaxy s24 Ultra'},{$set:{price:93000}}) 285 | ``` 286 | 287 | ```bash 288 | db.products.updateOne({name:'Bravia 43 inch 4k'},{$set:{price:45000}}) 289 | ``` 290 | 291 | ```bash 292 | db.products.updateOne({name:'Iphone 15'},{$set:{brand:'Apple',productType:'Mobile'}}) 293 | ``` 294 | 295 | #### Updating Many Records 296 | 297 | ```bash 298 | db.products.updateMany({_id:{$in:[ObjectId('66695c4aa52b865509cdcdf8'),ObjectId('66695c8ba52b865509cdcdfd')]}},{$set:{productType:"Mobile"}}) 299 | ``` 300 | 301 | #### Replacing The Document 302 | 303 | ```bash 304 | db.products.replaceOne({_id:ObjectId('66695c8ba52b865509cdcdfc')},{name:"Iphone 14 Pro Max",price:115000,brand:'Apple',productType:'Mobile'}) 305 | ``` 306 | 307 | --- 308 | 309 | ## Delete document from collection 310 | 311 | #### Deleting Single Record 312 | 313 | ```bash 314 | # the following command will delete the 1st document with matched filter 315 | db.products.deleteOne({brand:'Apple'}) 316 | ``` 317 | 318 | ```bash 319 | db.products.deleteOne({brand:'Samsung'}) 320 | ``` 321 | 322 | ```bash 323 | db.products.deleteOne({productType:'Laptop'}) 324 | ``` 325 | 326 | ```bash 327 | # the following command will delete document based on Id 328 | db.products.deleteOne({_id:'AX1000002'}) 329 | ``` 330 | 331 | #### Deleting Many Record 332 | 333 | ```bash 334 | # the following command will delete document many at same time 335 | db.products.deleteMany({productType:'Laptop'}) 336 | ``` 337 | 338 | ```bash 339 | db.products.deleteMany({}) 340 | ``` 341 | 342 | #### Deleting Collection 343 | 344 | ```bash 345 | db.products.drop() 346 | ``` 347 | 348 | #### Deleting Database 349 | 350 | ```bash 351 | db.dropDatabase() 352 | ``` 353 | 354 | --- 355 | 356 | ## Convert Json to Json minifier 357 | 358 | Link - [https://codebeautify.org/jsonminifier](https://codebeautify.org/jsonminifier) 359 | 360 | #### Prepare Data 361 | 362 | ###### Products 363 | 364 | ``` 365 | db.products.insertMany([{"name":"Iphone 15","price":80000,"brand":"Apple","productType":"Mobile"},{"name":"Iphone 14 Pro Max","price":115000,"brand":"Apple","productType":"Mobile"},{"name":"Samsung Galaxy A50","price":54000,"brand":"Samsung","productType":"Mobile"},{"name":"Nothing Phone 2a","price":56000,"brand":"Nothing","productType":"Mobile"},{"name":"Samsung Galaxy s24 Ultra","price":93000,"brand":"Samsung","productType":"Mobile"},{"name":"Bravia 43 inch 4k","price":45000,"brand":"Samsung","productType":"TV"},{"name":"Mac Book Pro","price":120000,"brand":"Apple","productType":"Laptop","specification":{"ram":"8 GB","processor":"M1","storageType":"SSD","storageSize":"512 GB"}},{"name":"Lenovo Legion","price":120000,"brand":"Lenovo","productType":"Laptop","specification":{"ram":"16 GB","processor":"i7 13th Gen","storageType":"SSD","storageSize":"1 TB"}},{"name":"MSI Katna 15","price":98000,"brand":"MSI","productType":"Laptop","specification":{"ram":"16 GB","processor":"i7 12th Gen","storageType":"SSD","storageSize":"512 GB"}},{"name":"MSI Katna 15 Pro","price":130000,"brand":"MSI","productType":"Laptop","specification":{"ram":"16 GB","processor":"i7 13th Gen","storageType":"SSD","storageSize":"1 TB"}}]) 366 | ``` 367 | 368 | ###### Customers 369 | 370 | ``` 371 | db.customers.insertMany([{"name":"Amit Sharma","age":29,"gender":"Male"},{"name":"Priya Singh","age":30,"gender":"Female"},{"name":"Ravi Verma","age":35,"gender":"Male"},{"name":"Sandeep Kumar","age":28,"gender":"Male"},{"name":"Anita Joshi","age":30,"gender":"Female"},{"name":"Anjali Patel","age":27,"gender":"Female"},{"name":"Dr.Rajesh Mehta","age":47,"gender":"Male"},{"name":"Seema Gupta","age":33,"gender":"Female"},{"name":"Aarti Chawla","age":29,"gender":"Female"},{"name":"Rohit Desai","age":41,"gender":"Male"},{"name":"Karandeep Alun","age":48,"gender":"Male"},{"name":"Neha Singh","age":39,"gender":"Female"},{"name":"Arjun Reddy","age":22,"gender":"Male"},{"name":"Vikram Sharma","age":44,"gender":"Male"},{"name":"Aman Khan","age":41,"gender":"Male"},{"name":"Meera Iyer","age":"35","gender":"Female"},{"name":"Nidhi Kapoor","age":27,"gender":"Female"},{"name":"Arjun Gupta","age":35,"gender":"Male"},{"name":"Kiran Naik","age":53,"gender":"Male"},{"name":"Bharat Rao","age":68,"gender":"Male"},{"name":"Ramesh Patel","age":38,"gender":"Male"}]) 372 | ``` 373 | 374 | --- 375 | 376 | #### Cursor Object 377 | 378 | ```bash 379 | #find will not give all records 380 | db.customers.find() 381 | #it to show remaining records 382 | ``` 383 | 384 | To see all the data use following commands 385 | 386 | ```bash 387 | db.customers.find().toArray() 388 | ``` 389 | 390 | ```bash 391 | db.customers.find().forEach((customersData)=> {printjson(customersData)}) 392 | ``` 393 | 394 | --- 395 | 396 | #### Projection 397 | 398 | ```bash 399 | db.customers.find({},{name:1}) 400 | ``` 401 | 402 | ```bash 403 | db.products.find({},{name:1,price:1}) 404 | ``` 405 | 406 | To ignore id 407 | 408 | ```bash 409 | db.products.find({},{_id:0,name:1,price:1}) 410 | ``` 411 | 412 | ```bash 413 | db.products.find({},{_id:0,name:1,price:1,brand:1}) 414 | ``` 415 | 416 | --- 417 | 418 | #### Array 419 | 420 | ```bash 421 | db.customers.updateOne({name:'Arjun Reddy'},{$set:{contactNo:[5987452368,8536547859]}}) 422 | ``` 423 | 424 | ```bash 425 | db.customers.updateOne({name:'Seema Gupta'},{$set:{hobbies:['cooking','sports','singing']}}) 426 | ``` 427 | 428 | ```bash 429 | db.customers.updateMany({name:{$in:['Neha Singh','Vikram Sharma','Arjun Gupta']}},{$set:{hobbies:['travel','sports']}}) 430 | ``` 431 | 432 | ```bash 433 | db.customers.updateMany({name:{$in:['Meera Iyer','Nidhi Kapoor','Bharat Rao']}},{$set:{hobbies:['cooking','sports']}}) 434 | ``` 435 | 436 | ```bash 437 | db.products.updateMany({},{$set:{categories:['electronics']}}) 438 | ``` 439 | 440 | --- 441 | 442 | #### Delete and Prepare dataset 443 | 444 | ``` 445 | db.products.deleteMany({}) 446 | ``` 447 | 448 | ``` 449 | db.products.insertMany([{"name":"Iphone 15","price":80000,"brand":"Apple","productType":"Mobile","categories":["electronics"]},{"name":"Iphone 14 Pro Max","price":115000,"brand":"Apple","productType":"Mobile","categories":["electronics"]},{"name":"Samsung Galaxy A50","price":54000,"brand":"Samsung","productType":"Mobile","categories":["electronics"]},{"name":"Nothing Phone 2a","price":56000,"brand":"Nothing","productType":"Mobile","categories":["electronics"]},{"name":"Samsung Galaxy s24 Ultra","price":93000,"brand":"Samsung","productType":"Mobile","categories":["electronics"]},{"name":"Bravia 43 inch 4k","price":45000,"brand":"Samsung","productType":"TV","categories":["electronics"]},{"name":"Mac Book Pro","price":120000,"brand":"Apple","productType":"Laptop","specification":{"ram":"8 GB","processor":"M1","storageType":"SSD","storageSize":"512 GB"},"categories":["electronics"]},{"name":"Lenovo Legion","price":120000,"brand":"Lenovo","productType":"Laptop","specification":{"ram":"16 GB","processor":"i7 13th Gen","storageType":"SSD","storageSize":"1 TB"},"categories":["electronics"]},{"name":"MSI Katna 15","price":98000,"brand":"MSI","productType":"Laptop","specification":{"ram":"16 GB","processor":"i7 12th Gen","storageType":"SSD","storageSize":"512 GB"},"categories":["electronics"]},{"name":"MSI Katna 15 Pro","price":130000,"brand":"MSI","productType":"Laptop","specification":{"ram":"16 GB","processor":"i7 13th Gen","storageType":"SSD","storageSize":"1 TB"},"categories":["electronics"]},{"_id":"666982f9132be8d964cdce53","name":"Samsung RT28T3022S8","price":25000,"brand":"Samsung","productType":"Refrigerator","specification":{"capacity":"253L","energyRating":"3 Star"},"categories":["kitchen_appliances"]},{"_id":"666982f9132be8d964cdce54","name":"LG MC2846BG","price":15000,"brand":"LG","productType":"Microwave Oven","specification":{"type":"Convection","capacity":"28L"},"categories":["kitchen_appliances"]},{"_id":"666982f9132be8d964cdce55","name":"Whirlpool IF278","price":22000,"brand":"Whirlpool","productType":"Refrigerator","specification":{"capacity":"265L","energyRating":"4 Star"},"categories":["kitchen_appliances"]},{"_id":"666982f9132be8d964cdce56","name":"IFB 30BRC2","price":20000,"brand":"IFB","productType":"Microwave Oven","specification":{"type":"Convection","capacity":"30L"},"categories":["kitchen_appliances"]},{"_id":"666982f9132be8d964cdce57","name":"Samsung WW60R20GLMA","price":28000,"brand":"Samsung","productType":"Washing Machine","specification":{"capacity":"6 kg","type":"Front Load"},"categories":["laundry_appliances"]},{"_id":"666982f9132be8d964cdce58","name":"LG T7281NDDLG","price":18000,"brand":"LG","productType":"Washing Machine","specification":{"capacity":"6.2 kg","type":"Top Load"},"categories":["laundry_appliances"]},{"_id":"666982f9132be8d964cdce59","name":"Philips HD6975/00","price":7000,"brand":"Philips","productType":"Oven Toaster Grill","specification":{"capacity":"25L","powerConsumption":"1500W"},"categories":["kitchen_appliances"]},{"_id":"666982f9132be8d964cdce5a","name":"Dyson V11 Absolute Pro","price":52000,"brand":"Dyson","productType":"Vacuum Cleaner","specification":{"type":"Cord-free","runTime":"60 minutes"},"categories":["home_cleaning"]},{"_id":"666982f9132be8d964cdce5b","name":"Eureka Forbes Aquaguard Marvel","price":15000,"brand":"Eureka Forbes","productType":"Water Purifier","specification":{"type":"RO+UV","storageCapacity":"8L"},"categories":["water_appliances"]},{"_id":"666982f9132be8d964cdce5c","name":"Kent Grand Plus","price":19000,"brand":"Kent","productType":"Water Purifier","specification":{"type":"RO+UV+UF","storageCapacity":"9L"},"categories":["water_appliances"]}]) 450 | ``` 451 | 452 | --- 453 | 454 | #### Accessing Embedded Document Data 455 | 456 | ###### Filter by hobbies in customers 457 | 458 | ```bash 459 | db.customers.find({hobbies:'sports'}) 460 | ``` 461 | 462 | ```bash 463 | db.customers.find({hobbies:'cooking'}) 464 | ``` 465 | 466 | ###### Finding embedded data in customers 467 | 468 | ```bash 469 | db.customers.findOne({age:22}).contactNo 470 | ``` 471 | 472 | ```bash 473 | db.customers.findOne({name:'Nidhi Kapoor'}).hobbies 474 | ``` 475 | 476 | ###### Filter by catgory in products 477 | 478 | ``` 479 | db.products.find({categories:'electronics'}); 480 | ``` 481 | 482 | ``` 483 | db.products.find({categories:'kitchen_appliances'}); 484 | ``` 485 | 486 | ###### Fliter by embedded data in products 487 | 488 | ``` 489 | db.products.find({"specification.storageSize":'512 GB'}) 490 | ``` 491 | 492 | ``` 493 | db.products.find({"specification.storageSize":'1 TB'}) 494 | ``` 495 | 496 | ``` 497 | db.products.find({"specification.ram":'8 GB'}) 498 | ``` 499 | 500 | ``` 501 | db.products.find({'specification.energyRating':'4 Star'}); 502 | ``` 503 | 504 | --- 505 | 506 | ### Database Methods 507 | 508 | ##### Status of database 509 | 510 | ```bash 511 | db.stats() 512 | ``` 513 | 514 | ##### Get current database name 515 | 516 | ```bash 517 | db.getName() 518 | ``` 519 | 520 | ##### Get collection list from current database 521 | 522 | ```bash 523 | db.getCollectionNames() 524 | ``` 525 | 526 | ##### Get collection list with more information from current database 527 | 528 | ```bash 529 | db.getCollectionInfos() 530 | ``` 531 | 532 | ### Collection Methods 533 | 534 | ##### Find no of documents in collection 535 | 536 | ```bash 537 | db.products.countDocuments() 538 | ``` 539 | 540 | ##### Find distinct values from collection 541 | 542 | ```bash 543 | db.products.distinct('categories') 544 | ``` 545 | 546 | --- 547 | 548 | ## Document Validation 549 | 550 | ```bash 551 | use schoolDb 552 | ``` 553 | 554 | ```bash 555 | db.createCollection('staff',{ 556 | validator:{ 557 | $jsonSchema:{ 558 | bsonType:'object', 559 | required:['name','age','email'], 560 | properties:{ 561 | name:{ 562 | bsonType:'string', 563 | description:'must be string and required field' 564 | }, 565 | age:{ 566 | bsonType:'int', 567 | description:'must be integer and required field' 568 | }, 569 | email:{ 570 | bsonType:'string', 571 | description:'must be string and required field' 572 | } 573 | } 574 | } 575 | } 576 | }) 577 | ``` 578 | 579 | ```bash 580 | db.createCollection('staffs',{validator:{$jsonSchema:{bsonType:'object',required:['name','age','email'],properties:{name:{bsonType:'string',description:'must be string and required field'},age:{bsonType:'int',description:'must be integer and required field'},email:{bsonType:'string',description:'must be string and required field'}}}}}) 581 | ``` 582 | 583 | ```bash 584 | db.staffs.insertOne({name:'john',age:25,email:'john@gmail.com'}) 585 | ``` 586 | 587 | ```bash 588 | db.runCommand({ 589 | collMod:'staffs', 590 | validator:{ 591 | $jsonSchema:{ 592 | bsonType:'object', 593 | required:['name','email'], 594 | properties:{ 595 | name:{ 596 | bsonType:'string', 597 | description:'must be string and required field' 598 | }, 599 | email:{ 600 | bsonType:'string', 601 | description:'must be string and required field' 602 | } 603 | } 604 | } 605 | } 606 | }); 607 | ``` 608 | 609 | ```bash 610 | db.runCommand({collMod:'staffs',validator:{$jsonSchema:{bsonType:'object',required:['name','email'],properties:{name:{bsonType:'string',description:'must be string and required field'},email:{bsonType:'string',description:'must be string and required field'}}}}}); 611 | ``` 612 | 613 | ```bash 614 | db.staffs.insertOne({name:'alex',email:'alex@gmail.com'}) 615 | ``` 616 | 617 | ```bash 618 | db.staffs.find() 619 | ``` 620 | 621 | --- 622 | 623 | ## Operators 624 | 625 | ```bash 626 | use maxishop 627 | ``` 628 | 629 | ### Comparison Operators 630 | 631 | ##### Equals 632 | 633 | ```bash 634 | db.products.find({price:{$eq:80000}}) 635 | ``` 636 | 637 | ##### Greater Than 638 | 639 | ```bash 640 | db.products.find({price:{$gt:80000}}) 641 | ``` 642 | 643 | ```bash 644 | db.products.findOne({price:{$gt:80000}}) 645 | ``` 646 | 647 | ##### Greater Than or Equal 648 | 649 | ```bash 650 | db.products.find({price:{$gte:80000}}) 651 | ``` 652 | 653 | ##### Less Than 654 | 655 | ```bash 656 | db.products.find({price:{$lt:80000}}) 657 | ``` 658 | 659 | ```bash 660 | db.products.findOne({price:{$lt:80000}}) 661 | ``` 662 | 663 | ##### Less Than or Equal 664 | 665 | ```bash 666 | db.products.find({price:{$lte:80000}}) 667 | ``` 668 | 669 | ##### Not Equal 670 | 671 | ```bash 672 | db.products.find({price:{$ne:80000}}) 673 | ``` 674 | 675 | ##### Matches any of the values in an array 676 | 677 | ```bash 678 | db.customers.find({hobbies:{$in:['cooking']}}) 679 | ``` 680 | 681 | ```bash 682 | db.customers.find({hobbies:{$in:['cooking','travel']}}) 683 | ``` 684 | 685 | ##### Not Matched any of the values in an array 686 | 687 | ```bash 688 | db.customers.find({hobbies:{$nin:['cooking']}}) 689 | ``` 690 | 691 | ```bash 692 | db.customers.find({hobbies:{$nin:['cooking','travel']}}) 693 | ``` 694 | 695 | ### Logical Operators 696 | 697 | ##### AND 698 | 699 | ```bash 700 | db.products.find({$and:[{price:{$gt:80000}},{brand:'Apple'}]}) 701 | ``` 702 | 703 | ##### OR 704 | 705 | ```bash 706 | db.products.find({$or:[{price:{$lt:10000}},{brand:'Apple'}]}) 707 | ``` 708 | 709 | ##### NOR 710 | 711 | ```bash 712 | db.products.find({$nor:[{price:{$lt:10000}},{productType:'Laptop'},{productType:'Mobile'}]}) 713 | ``` 714 | 715 | ```bash 716 | db.products.find({$nor:[{price:{$lt:40000}},{productType:'Laptop'},{productType:'Mobile'}]}) 717 | ``` 718 | 719 | ##### NOT 720 | 721 | ```bash 722 | db.products.find({price:{$not:{$gt:10000}}}) 723 | ``` 724 | 725 | ### Element Operators 726 | 727 | ##### Exists 728 | 729 | ```bash 730 | db.customers.find({hobbies:{$exists:false}}) 731 | ``` 732 | 733 | ```bash 734 | db.customers.find({hobbies:{$exists:true}}) 735 | ``` 736 | 737 | ```bash 738 | db.customers.find({$and:[{hobbies:{$exists:true}},{age:{$gt:40}}]}) 739 | ``` 740 | 741 | ```bash 742 | db.customers.find({$and:[{hobbies:{$exists:false}},{age:{$gt:40}}]}) 743 | ``` 744 | 745 | ##### Type 746 | 747 | ```bash 748 | db.customers.find({age:{$type:'string'}}) 749 | db.customers.find({age:{$type:'number'}}) 750 | ``` 751 | 752 | #### Expr - expression 753 | 754 | ```bash 755 | use personalData 756 | ``` 757 | 758 | ```bash 759 | db.personalData.insertMany([{category:'food',budget:400,spent:450},{category:'clothes',budget:100,spent:50},{category:'travel',budget:200,spent:650}]) 760 | ``` 761 | 762 | ```bash 763 | db.personalData.find({$expr:{$gt:['$spent','$budget']}}) 764 | ``` 765 | 766 | ### Array Operators 767 | 768 | ```bash 769 | use maxishop 770 | ``` 771 | 772 | ##### Array Size 773 | 774 | ```bash 775 | db.customers.find({hobbies:{$size:3}}) 776 | ``` 777 | 778 | ```bash 779 | db.customers.find({hobbies:{$size:2}}) 780 | ``` 781 | 782 | ##### All 783 | 784 | ```bash 785 | db.customers.find({hobbies:["sports","travel"]}) 786 | ``` 787 | 788 | ```bash 789 | db.customers.find({hobbies:["travel","sports"]}) 790 | ``` 791 | 792 | ```bash 793 | db.customers.find({hobbies:{$all:["sports","travel"]}}) 794 | ``` 795 | 796 | --- 797 | 798 | ### Sort, Skip & Limit 799 | 800 | ##### Sorting the document 801 | 802 | ```bash 803 | db.products.find().sort('price') 804 | ``` 805 | 806 | ```bash 807 | db.products.find().sort({'price':-1}) 808 | ``` 809 | 810 | ```bash 811 | db.products.find({productType:'Mobile'}).sort('price') 812 | ``` 813 | 814 | ```bash 815 | db.products.find({productType:'Mobile'}).sort({'price':-1}) 816 | ``` 817 | 818 | ##### Skip the document 819 | 820 | ```bash 821 | db.products.find({productType:'Mobile'}).sort('price').skip(2) 822 | ``` 823 | 824 | ##### Limit the document 825 | 826 | ```bash 827 | db.products.find({productType:'Mobile'}).sort('price').limit(2) 828 | ``` 829 | 830 | ```bash 831 | db.products.find({productType:'Mobile'}).sort({'price':-1}).skip(1).limit(2) 832 | ``` 833 | 834 | --- 835 | 836 | ### Example from Learning 837 | 838 | ```bash 839 | db.products.find({},{_id:0,name:1,'specification.ram':1}).sort({'price':-1}).limit(4) 840 | ``` 841 | 842 | ```bash 843 | db.products.find({specification:{$exists:true}},{_id:0,name:1,'specification.ram':1}).sort({'price':-1}).limit(4) 844 | ``` 845 | 846 | --- 847 | 848 | ### CRUD Options 849 | 850 | ##### Create option 851 | 852 | ```bash 853 | use temp 854 | ``` 855 | 856 | ```bash 857 | db.users.insertOne({_id:'UA001',name:'John wick'}) 858 | ``` 859 | 860 | ```bash 861 | db.users.insertMany([{_id:'UA001',name:'Lisa'},{_id:'UA002',name:'Rose'}]) 862 | ``` 863 | 864 | ordered - partial insert for valid documents 865 | 866 | ```bash 867 | db.users.insertMany([{_id:'UA001',name:'Lisa'},{_id:'UA002',name:'Rose'}],{ordered:false}) 868 | ``` 869 | 870 | ##### Read Option 871 | 872 | ```bash 873 | use maxishop 874 | ``` 875 | 876 | Projection 877 | 878 | ```bash 879 | db.products.find({},{name:1}) 880 | ``` 881 | 882 | ```bash 883 | db.products.find({},{_id:0,name:1}) 884 | ``` 885 | 886 | ```bash 887 | db.products.find({productType:'Laptop'},{name:1}) 888 | ``` 889 | 890 | ```bash 891 | db.products.find({productType:'Laptop'},{name:1,specification:1}) 892 | ``` 893 | 894 | ```bash 895 | db.products.find({productType:'Laptop'},{name:1,'specification.ram':1}) 896 | ``` 897 | 898 | ```bash 899 | db.products.find({productType:'Laptop'},{name:1,'specification.ram':1,'specification.processor':1}) 900 | ``` 901 | 902 | Slice 903 | 904 | ```bash 905 | db.customers.find({hobbies:{$exists:true}},{hobbies:{$slice:1}}) 906 | ``` 907 | 908 | Slice Skip and Take 909 | 910 | ```bash 911 | db.customers.find({hobbies:{$exists:true}},{hobbies:{$slice:[1,2]}}) 912 | ``` 913 | 914 | ##### Update option 915 | 916 | Before upsert, if there is not match then changes will not happen 917 | 918 | ```bash 919 | db.customers.updateOne({name:'Swetha'},{$set:{name:'Swetha Sri',age:25,gender:'Female',hobbies:['travel','dancing']}}) 920 | ``` 921 | 922 | upsert 923 | 924 | ```bash 925 | db.customers.updateOne({name:'Swetha'},{$set:{name:'Swetha Sri',age:25,gender:'Female',hobbies:['travel','dancing']}},{upsert:true}) 926 | ``` 927 | 928 | ##### Delete option 929 | 930 | ```bash 931 | db.customers.find().sort('age').limit(3) 932 | ``` 933 | 934 | ```bash 935 | db.customers.findOneAndDelete({},{sort:{age:1}}) 936 | ``` 937 | 938 | --- 939 | 940 | ### Relations 941 | 942 | ##### One to One Relation 943 | 944 | ```bash 945 | use OrgDB 946 | ``` 947 | 948 | ```bash 949 | db.employees.insertOne({_id:'EMP001',name:'John Doe',postion:'Software Enginner'}) 950 | ``` 951 | 952 | ```bash 953 | db.profiles.insertOne({employee_id:'EMP001',age:26,address:'123 Main St',phone:'555-555-5555'}) 954 | ``` 955 | 956 | Aggregate 957 | 958 | ```bash 959 | db.employees.aggregate([{$lookup:{from:'profiles',localField:'_id',foreignField:'employee_id',as:"personalDetails"}}]) 960 | ``` 961 | 962 | ##### One to Many Relation 963 | 964 | Creating Companies 965 | 966 | ```bash 967 | db.companies.insertMany([{_id:'CMP001',name:'Nule Soft',isMNC:true},{_id:'CMP002',name:'JexiTech',isMNC:false},{_id:'CMP003',name:'DSA Softwares',isMNC:true}]) 968 | ``` 969 | 970 | Updateing old employee to assgin company 971 | 972 | ```bash 973 | db.employees.updateOne({_id:'EMP001'},{$set:{company_id:'CMP001'}}) 974 | ``` 975 | 976 | Adding More Employee with different companies 977 | 978 | ```bash 979 | db.employees.insertMany([{_id:'EMP002',name:'Alice Smith',postion:'Product Manager',company_id:'CMP001'},{_id:'EMP003',name:'Bob Brown',postion:'UI/UX Designer',company_id:'CMP002'},{_id:'EMP004',name:'Charlie Davis',postion:'Backend Developer',company_id:'CMP001'},{_id:'EMP005',name:'Diana Evans',postion:'Frontend Developer',company_id:'CMP001'}]) 980 | ``` 981 | 982 | ```bash 983 | db.profiles.insertMany([{employee_id:'EMP002',age:36,address:'123 Main St',phone:'444-555-5555'},{employee_id:'EMP003',age:28,address:'123 Main St',phone:'222-333-5555'},{employee_id:'EMP004',age:37,address:'567 Main St',phone:'111-333-4444'},{employee_id:'EMP005',age:32,address:'987 Main St',phone:'111-222-8888'}]) 984 | ``` 985 | 986 | Aggregate 987 | 988 | ```bash 989 | db.companies.aggregate([{$lookup:{from:'employees',localField:'_id',foreignField:'company_id',as:'Employees'}}]) 990 | ``` 991 | 992 | ##### Many to Many Relation 993 | 994 | Creating projects 995 | 996 | ```bash 997 | db.projects.insertMany([{_id:'P001',name:'Project Alpha'},{_id:'P002',name:'Project Beta'}]) 998 | ``` 999 | 1000 | ```bash 1001 | db.employee_projects.insertMany([{employee_id:'EMP001',project_id:'P001'},{employee_id:'EMP005',project_id:'P001'},{employee_id:'EMP003',project_id:'P001'},{employee_id:'EMP002',project_id:'P002'},{employee_id:'EMP004',project_id:'P002'},{employee_id:'EMP003',project_id:'P002'}]) 1002 | ``` 1003 | 1004 | Aggregate 1005 | 1006 | ```bash 1007 | db.projects.aggregate([{$lookup:{from:'employee_projects',localField:'_id',foreignField:'project_id',as:'employee_projects'}}]) 1008 | ``` 1009 | 1010 | ```bash 1011 | db.projects.aggregate([{$lookup:{from:'employee_projects',localField:'_id',foreignField:'project_id',as:'employee_projects'}},{$unwind:"$employee_projects"},{$lookup:{from:'employees',localField:'employee_projects.employee_id',foreignField:'_id',as:'employees'}},{$unwind:'$employees'},{$group:{_id:'$_id',name:{$first:'$name'},employees:{$push:'$employees'}}},{$project:{_id:0,name:1,employees:1}}]) 1012 | ``` 1013 | 1014 | ```bash 1015 | db.projects.aggregate([{$lookup:{from:'employee_projects',localField:'_id',foreignField:'project_id',as:'employee_projects'}},{$unwind:"$employee_projects"},{$lookup:{from:'employees',localField:'employee_projects.employee_id',foreignField:'_id',as:'employees'}},{$unwind:'$employees'},{$group:{_id:'$_id',name:{$first:'$name'},employees:{$push:{employee_id:'$employees._id',name:'$employees.name'}}}},{$project:{_id:1,name:1,employees:1}}]) 1016 | ``` 1017 | 1018 | --- 1019 | 1020 | # Copyrights to codewithkarthik 1021 | -------------------------------------------------------------------------------- /customers.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "Amit Sharma", 4 | "age": 29, 5 | "gender": "Male" 6 | }, 7 | { 8 | "name": "Priya Singh", 9 | "age": 30, 10 | "gender": "Female" 11 | }, 12 | { 13 | "name": "Ravi Verma", 14 | "age": 35, 15 | "gender": "Male" 16 | }, 17 | { 18 | "name": "Sandeep Kumar", 19 | "age": 28, 20 | "gender": "Male" 21 | }, 22 | { 23 | "name": "Anita Joshi", 24 | "age": 30, 25 | "gender": "Female" 26 | }, 27 | { 28 | "name": "Anjali Patel", 29 | "age": 27, 30 | "gender": "Female" 31 | }, 32 | { 33 | "name": "Dr. Rajesh Mehta", 34 | "age": 47, 35 | "gender": "Male" 36 | }, 37 | { 38 | "name": "Seema Gupta", 39 | "age": 33, 40 | "gender": "Female" 41 | }, 42 | { 43 | "name": "Aarti Chawla", 44 | "age": 29, 45 | "gender": "Female" 46 | }, 47 | { 48 | "name": "Rohit Desai", 49 | "age": 41, 50 | "gender": "Male" 51 | }, 52 | { 53 | "name": "Karandeep Alun", 54 | "age": 48, 55 | "gender": "Male" 56 | }, 57 | { 58 | "name": "Neha Singh", 59 | "age": 39, 60 | "gender": "Female" 61 | }, 62 | { 63 | "name": "Arjun Reddy", 64 | "age": 22, 65 | "gender": "Male" 66 | }, 67 | { 68 | "name": "Vikram Sharma", 69 | "age": 44, 70 | "gender": "Male" 71 | }, 72 | { 73 | "name": "Aman Khan", 74 | "age": 41, 75 | "gender": "Male" 76 | }, 77 | { 78 | "name": "Meera Iyer", 79 | "age": "35", 80 | "gender": "Female" 81 | }, 82 | { 83 | "name": "Nidhi Kapoor", 84 | "age": 27, 85 | "gender": "Female" 86 | }, 87 | { 88 | "name": "Arjun Gupta", 89 | "age": 35, 90 | "gender": "Male" 91 | }, 92 | { 93 | "name": "Kiran Naik", 94 | "age": 53, 95 | "gender": "Male" 96 | }, 97 | { 98 | "name": "Bharat Rao", 99 | "age": 68, 100 | "gender": "Male" 101 | }, 102 | { 103 | "name": "Ramesh Patel", 104 | "age": 38, 105 | "gender": "Male" 106 | } 107 | ] 108 | -------------------------------------------------------------------------------- /products.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "Iphone 15", 4 | "price": 80000, 5 | "brand": "Apple", 6 | "productType": "Mobile" 7 | }, 8 | { 9 | "name": "Iphone 14 Pro Max", 10 | "price": 115000, 11 | "brand": "Apple", 12 | "productType": "Mobile" 13 | }, 14 | { 15 | "name": "Samsung Galaxy A50", 16 | "price": 54000, 17 | "brand": "Samsung", 18 | "productType": "Mobile" 19 | }, 20 | { 21 | "name": "Nothing Phone 2a", 22 | "price": 56000, 23 | "brand": "Nothing", 24 | "productType": "Mobile" 25 | }, 26 | { 27 | "name": "Samsung Galaxy s24 Ultra", 28 | "price": 93000, 29 | "brand": "Samsung", 30 | "productType": "Mobile" 31 | }, 32 | { 33 | "name": "Bravia 43 inch 4k", 34 | "price": 45000, 35 | "brand": "Samsung", 36 | "productType": "TV" 37 | }, 38 | { 39 | "name": "Mac Book Pro", 40 | "price": 120000, 41 | "brand": "Apple", 42 | "productType": "Laptop", 43 | "specification": { 44 | "ram": "8 GB", 45 | "processor": "M1", 46 | "storageType": "SSD", 47 | "storageSize": "512 SSD" 48 | } 49 | }, 50 | { 51 | "name": "Lenovo Legion", 52 | "price": 120000, 53 | "brand": "Lenovo", 54 | "productType": "Laptop", 55 | "specification": { 56 | "ram": "16 GB", 57 | "processor": "i7 13th Gen", 58 | "storageType": "SSD", 59 | "storageSize": "1 TB" 60 | } 61 | }, 62 | { 63 | "name": "MSI Katna 15", 64 | "price": 98000, 65 | "brand": "MSI", 66 | "productType": "Laptop", 67 | "specification": { 68 | "ram": "16 GB", 69 | "processor": "i7 12th Gen", 70 | "storageType": "SSD", 71 | "storageSize": "512 GB" 72 | } 73 | }, 74 | { 75 | "name": "MSI Katna 15 Pro", 76 | "price": 130000, 77 | "brand": "MSI", 78 | "productType": "Laptop", 79 | "specification": { 80 | "ram": "16 GB", 81 | "processor": "i7 13th Gen", 82 | "storageType": "SSD", 83 | "storageSize": "1 TB" 84 | } 85 | } 86 | ] 87 | --------------------------------------------------------------------------------