nodejs + express + ejs + mongodb 一个非常简单的前后端开发的实例3

    CREATE OUR DB AND READ STUFF FROM IT  创建数据库并从中读取数据

   A: 安装MongoDB数据库
 
   We're leaving our text editor for a bit and going back to our command prompt. Well, first we're going to our web browser, pointing it tohttp://mongodb.org/ and downloading Mongo. 
    1 首先从http://mongodb.org/ 下载Mongo. 
    This will give you a zip file, which you should unzip to a temp directory.  
    2 下载完成后是一个zip文件, 解压到一个临时目录中. 
    Then you should make a directory where you want to forever after store Mongo.
    3 然后创建一个目录用来存储Mongo. we'll be storing our database in our nodetest1 directory.我们将在我们的nodetest1目录下存储我们的数据库. 

B: 运行Mongod和Mongo
   在你的nodetest1目录下, 创建一个称为data的子目录.  然后Then navigate to the directory in which you placed your MongoDB files (let's say C:\mongodb for now). From that directory, type the following.
    
   1 然后转到你存入 MongoDB的目录下, 如C:\mongodb 输入如下内容  
mongod --dbpath c:\node\nodetest1\data
You'll see the Mongo server start up. This is going to take a while if it's the first time, because it has to do some preallocating of space and a few other housekeeping tasks. 
你将会看到Mongo服务器启动. 如果是第一次将会花费一点时间, 因为不得不做一些空间的预加载和其他的些管理任务.
   2 重新打开一个命令窗口, 转到mogodb的目录C:\mongodb目录下, 输入mongo将会显示如下信息:
c:\mongo>mongo
MongoDB shell version: 2.4.5
connecting to: test
   Additionally, if you're paying attention to your mongod instance, you'll see it mention that a connection has been established. All right, you've got MongoDB up and running, and you've connected to it with the client. We'll use this client to manually work on our database, for a bit, but it's not necessary for running the website. Only the server daemon后台 (mongod) is needed for that.
   另外, 如果你注意你的mongod实例, 你将会发现一个创建了一个连接. 你已经启动了MongoDB并运行, 连接了一个客户端. 你将用这个客户端手动在我们的DB上工作. 

C: 创建一个DB
  Don't worry about "connecting to: test" … that's just the default database Mongo decides to use if you don't specify one on the command line,  不要担心connectiong to: test , 如果你没有在命令行中指定, 它仅仅是默认的DB.
  
use nodetest1
D: 添加数据
  My favorite thing about MongoDB is that it uses JSON for its structure, which means it was instantly familiar for me.
  MongoDB我最喜欢的是它用JSON来组织数据, 这是我非常熟悉的.
  
db.usercollection.insert({ "username" : "testuser1", "email" : "testuser1@testdomain.com" })   //当前的DB对象db中加入一个usercollection集合. 
  
db.usercollection.find().pretty()  //查找当前集合中所有的内容, 并回车显示
newstuff = [{ "username" : "testuser2", "email" : "testuser2@testdomain.com" }, { "username" : "testuser3", "email" : "testuser3@testdomain.com" }]
db.usercollection.insert(newstuff);  //再将插入另外2条数据
E:  Mongo连接到Node上   HOOK MONGO UP TO NODE
  最终我们生成的HTML文件如下
<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 打开app.js 文件, 添加如下代码:
  
// New Code
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/nodetest1'); //注:nodetest1为数据库名
These lines tell our app we want to talk to MongoDB, we're going to use Monk to do it, and our database is located at localhost:27017/nodetest1
   这些命令的作用是告诉我们的应用我们想要访问MongoDB, 我们用Monk来做, 并且我们的数据库在localhost:27017/nodetest1

2  路由部分加下如下代码
app.get('/userlist', routes.userlist(db));
F:  从mongo中取出数据并显示
   在文体编辑器中打开nodetest1\routes\index.js文件, 在最后加上第三个路由, 输入如下信息
  
exports.userlist = function(db) {
    return function(req, res) {
        var collection = db.get('usercollection'); //获得数据库中的集合(类似关系数据库中的表)
        collection.find({},{},function(e,docs){ //取得所有的集合数据, 渲染到页面上,关键字是userlist
            res.render('userlist', {
                "userlist" : docs
            });
        });
    };
};
   view下创建userlist.ejs, 文件中输入如下内容. 
<html>
<head>
<title></title>
<style type="text/css">
   ul li { list-style: none; width: 200px; padding:5px; background: pink; margin-bottom: 5px;}
</style>
</head>
<body>
	<ul>
		<% for(i=0; i< userlist.length; i++) {%>
		   <li><a><%= userlist[i].username %></a></li>
		<% } %>
        </yk>
</body>
</html>

   kill app.js后在重新启动
C:\node\nodetest1>node app.js
  Now open your browser and head to http://localhost:3000/userlist and marvel in the results. 
  运行效果如下:
  
注: MongoDB是非关系型的数据库.  文档(键值对, 类似关系数据的行), 集合(文档的集合, 类似表) ,DB(多个集合).


评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值