Create we DB and read STUFF from IT creates a database and reads data from IT
A: Install the MongoDB database
We ' re leaving our text editor for a bit and going back to our command prompt. At the very same, we ' re going to our web browser, pointing it tohttp://mongodb.org/and downloading Mongo. 1 Download MONGO from http://mongodb.org/first. This is give you a zip file, which your should unzip to a temp directory. 2 after the download is completed, a zip file is extracted into a temporary directory. Then your should make a directory where your want to forever after store Mongo. 3 then create a directory to store the MONGO. We will be storing US database in our Nodetest1 directory. We'll store our databases under our Nodetest1 directory.
B: Run Mongod and MONGOIn your Nodetest1 directory, create a subdirectory called data. Then then navigate to the directory in which your placed your MongoDB files (let's say C:\mongodb for now). From that directory, type the following. 1 and then go to the directory where you deposit MongoDB, such as C:\mongodb enter the following
Mongod--dbpath C:\node\nodetest1\data
You'll be the Mongo server start up. This is going to take a while if it's the "the", because it has to do some preallocating of spaces and a few other Hou sekeeping tasks.
You will see the MONGO server start up. If it is the first time it will take a little time, because have to do some space preload and other management tasks. 2 Reopen a command window, go to mogodb directory C:\mongodb directory, enter MONGO will display the following information:
C:\mongo>mongo
MongoDB Shell version:2.4.5
connecting To:test
Additionally, if you are paying attention to your Mongod instance, you'll be in it mention that a connection has been Shed. All right, your ' ve got MongoDB up and running, and your ' ve connected to it with the client. We'll use this client to manually work in our database for a bit, but it's not necessary for running the website. Only the server daemon background (mongod) is needed to that.
In addition, if you pay attention to your Mongod instance, you will find one that creates a connection. You have started the MongoDB and run it, connecting a client. You will be working with this client manually on our db.
C: Create a DBDon ' t worry about "connecting to:test" ... that's just the default database Mongo decides to use if you don ' t specify one O n the command line, don't worry about Connectiong to:test, if you don't specify it in the command-line, it's just the default db.
Use Nodetest1
D: Adding DataMy favorite thing about MongoDB be that it uses JSON to its structure, which means the it was instantly familiar for me. MongoDB My favorite is that it organizes data in JSON, which is very familiar to me.
Db.usercollection.insert ({"username": "TestUser1", "email": "testuser1@testdomain.com"}) // A Usercollection collection is added to the current DB object db.
Db.usercollection.find (). Pretty () //Find all content in the current collection, and return to display
Newstuff = [{"username": "TestUser2", "email": "testuser2@testdomain.com"}, {"username": "Testuser3", "Email": "tes Tuser3@testdomain.com "}]
Db.usercollection.insert (newstuff); Another 2 data will be inserted
E:mongo Connect to node on HOOK MONGO up to Node
in the end, we generated HTML files as follows
<ul>
<li><a href= "mailto:testuser1@testdomain.com" >testuser1</a></li>
<li><a href= "mailto:testuser2@testdomain.com" >testuser2</a></li>
<li><a href= "mailto:testuser3@testdomain.com" >testuser3</a></li>
</ul>
1 Open the App.js file and add the following code:
New Code
var mongo = require (' MongoDB ');
var monk = require (' monk ');
var db = Monk (' Localhost:27017/nodetest1 '); Note: Nodetest1 is the database name
These are lines tell our apps we want to talk to MongoDB, we ' re going to-do it, and our database are Monk at located Lhost:27017/nodetest1
The function of these commands is to tell us the application we want to visit MongoDB, we use Monk to do it, and our database is in Localhost:27017/nodetest1
2 The Routing section adds the following code
App.get ('/userlist ', routes.userlist (db));
F: Remove the data from the MONGO and displayOpen the Nodetest1\routes\index.js file in the stylistic editor, and at the end with a third route, enter the following information