Welcome to my MongoDB Tutorial. In this tutorial, I’ll show you how to install MongoDB on both Windows and Mac. We’ll also cover how to use Mongo, Mongod, executing JavaScript, creating databases, creating documents, insert, update, find, remove, data types, executing external JS scripts, the mongorc.js file and much more.
All of the code from the video can be found below underneath the video. The rest of the videos will follow very soon after this one.
If you like videos like this it helps when you tell Google with a click here [googleplusone]
Code From the Video
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
---------- Basics & Setup ---------- 1. A database name can't contain spaces, /, \, ., * <, >, ", |, ?, $, or : and normally are lowercase characters that are case sensitive 2. You start the server by typing this in the terminal or command prompt mongod or mongod.exe on Windows 3. You start the shell for interacting with the database by typing mongo or mongo.exe 4. You can run JavaScript code in the shell a. function times2(num){ ... return num * 2; ... } > times2(5); // Returns 10 5. Show all databases with show dbs 6. Show the current database by typing db in the shell 7. Select a database with use test_1 8. Create a MongoDB document and insert it in the database a. derekbanas = {"name" : "Derek Banas", ... "position" : "Programmer", ... "email" : "db@aol.com", ... "hiredate" : new Date()} db.test_1.insert(derekbanas) 9. Show the document with db.test_1.find() 10. Update the database to allow a list of references derekbanas.references = [ ] db.test_1.update({"name" : "Derek Banas"}, derekbanas) db.test_1.find() // Shows the updated document 11. Use remove on its own to delete all documents or with a parameter to delete just the one that matches db.test_1.remove({"name" : "Derek Banas"}) db.test_1.find() // Shows no results 12. Data Types a. null : {"name" : null} b. boolean : {"over20" : true} c. number : (64 bit float) : {"height" : 6.25} 1. 4 byte Int : {"bigint" : 4294967295} 2. 8 byte Long : {"bigLong" : 18446744073709551616} d. string : {"address" : "123 Main St"} e. An array can contain multiple data types : array : {"grades" : ["a", "b", "c", "d", "f"]} f. date object : {"hiredate" : new Date()} g. regular expression : {"streetregex" : /^[A-Za-z0-9\.\' \-]{5,30}$/} h. embedded document : {"info" : {"name" : "Sue Smith"}} i. object id (Unique for every document) : 12 Byte ID for documents j. randomdata = {"name" : null, "over20" : true, "height" : 6.25, "bigint" : 4294967295, "bigLong" : 18446744073709551616, "address" : "123 Main St", "grades" : ["a", "b", "c", "d", "f"], "hiredate" : new Date(), "streetregex" : '/^[A-Za-z0-9\.\' \-]{5,30}$/', "info" : {"name" : "Sue Smith"}} k. db.test_1.insert(randomdata) 13. By typing help you'll receive a list of MongoDB commands 14. You can type commands without parentheses or parameters to see what the function does like -> db.test_1.find 15. exit closes the Mongo session 16. Type ulimit -n 2048 before running mongod to eliminate the error "soft rlimits too low. Number of files is 256" which will allocate more memory for the process. 17. You can execute JS scripts in MongoDB like this a. Create file DefSelectDB.js i. var selectDB = function(port, dbName) { if(!port){ port = 27017; } if(!dbName){ dbName = "test_1"; } db = connect("localhost:" + port + "/" + dbName); return db; } b. Load the script in the Mongo shell : load('DefSelectDB.js') c. Execute the function : selectDB() d. Switch to another database like this : selectDB(27017,"test") 18. .mongorc.js is executed every time you run your shell command a. On Windows it is at c:\Users\derekbanas\.mongorc.js b. On Mac show all files by putting this in the terminal : defaults write com.apple.finder AppleShowAllFiles YES c. Then open .mongorc.js from the finder on Mac d. You can protect the DB, change the prompt and add an editor by adding these lines var protectDB = function() { db.dropDatabase = DB.prototype.dropDatabase = no; DBCollection.prototype.drop = no; DBCollection.prototype.dropIndex = no; print("Database Protected"); }; operationCount = 1; prompt = function(){ if (typeof db == 'undefined'){ return 'nodb > '; } return db + " " + (operationCount++) + " > "; }; EDITOR="vim" e. Then execute protectDB() in the Mongo shell f. This is the code on Windows i. var protectDB = function() { db.dropDatabase = DB.prototype.dropDatabase = no; DBCollection.prototype.drop = no; DBCollection.prototype.dropIndex = no; print("Database Protected"); }; operationCount = 1; prompt = function(){ if (typeof db == 'undefined'){ return 'nodb > '; } return db + " " + (operationCount++) + " > "; }; EDITOR="C:\\PROGRA~1\\SUBLIM~1\\sublime_text.exe" g. Now you can put this in the terminal : susysmith = {"name" : "Susy Smith"} and then edit it with : edit susysmith |
Leave a Reply