- db.createCollection("goods")
- // 引入模块
- var mongoose = require('mongoose');
- //引入data.js
- var seafoodList = require('./data.js').seafoodList
- // 连接数据库
- mongoose.connect('mongodb://localhost:27017/admin')
- // 得到数据库连接句柄
- var db = mongoose.connection;
- //通过 数据库连接句柄,监听mongoose数据库成功的事件
- db.on('open',
- function(err) {
- if (err) {
- console.log('数据库连接失败');
- throw err;
- }
- console.log('数据库连接成功')
- })
- //定义表数据结构
- var seafoodModel = new mongoose.Schema({
- id: Number,
- name: String,
- desc: String,
- discount: Number,
- price: Number,
- coin: Number,
- remain: String,
- img_sm: String,
- img_lg: String
- },
- {
- versionKey: false //去除: - -v
- })
- // 将表的数据结构和表关联起来
- var seafoodModel = mongoose.model("seafoodList", seafoodModel, "goods");
- // 添加数据
- seafoodModel.insertMany(seafoodList,
- function(err, result) {
- if (err) {
- console.log("数据添加失败");
- throw err;
- }
- console.log("数据添加成功:", result);
- })
- //导出数据
- module.exports = {
- seafoodModel: seafoodModel
- }
- //引入express模块
- var express=require("express")
- //引入数据模块
- var goodsData=require("../model/goods").seafoodModel;
- //获取路由对象
- var router=express.Router();
- //商品管理页
- router.get('/goods',function(req,res){
- res.render('index/index')
- })
- //商品信息
- router.get('/goodsList',function(req,res){
- goodsData.find({},function(err,result){
- if(err){
- console.log("查询失败")
- throw err
- }
- console.log("查询结果",result)
- res.render('goods/goodsList',{title:'商品信息',goodsList:result})
- })
- })
- module.exports=router;
- <div class="page-content">
- <div class="page-header">
- <h1>
- 商品管理
- <small>
- <i class="icon-double-angle-right"></i>
- <%= title%>
- </small>
- </h1>
- </div><!-- /.page-header -->
- <!--主要内容-->
- <div class="row">
- <table class="table table-bordered table-hover text-center">
- <thead>
- <tr class="success">
- <th class="text-center">id</th>
- <th class="text-center">name</th>
- <th class="text-center">desc</th>
- <th class="text-center">discount</th>
- <th class="text-center">coin</th>
- <th class="text-center">remain</th>
- <th class="text-center">img_sm</th>
- <th class="text-center">img_lg</th>
- </tr>
- </thead>
- <tbody>
- <
- %
- for
- (
- var
- item
- in
- goodsList
- ){%>
- <tr>
- <td>
- <
- %=
- goodsList
- [
- item
- ]
- .id
- %>
- </td>
- <td>
- <
- %=
- goodsList
- [
- item
- ]
- .name
- %>
- </td>
- <td>
- <
- %=
- goodsList
- [
- item
- ]
- .desc
- %>
- </td>
- <td>
- <
- %=
- goodsList
- [
- item
- ]
- .coin
- %>
- </td>
- <td>
- <
- %=
- goodsList
- [
- item
- ]
- .remain
- %>
- </td>
- <td>
- <
- %=
- goodsList
- [
- item
- ]
- .discount
- %>
- </td>
- <td class="cut">
- <
- %=
- goodsList
- [
- item
- ]
- .img_sm
- %>
- </td>
- <td class="cut1">
- <
- %=
- goodsList
- [
- item
- ]
- .img_lg
- %>
- </td>
- </tr>
- <% }%>
- </tbody>
- </table>
- </div>
- </div><!-- /.page-content -->
- app.set('views', path.join(__dirname, 'views'));
- app.engine(".html", ejs.__express);
- //设置模板引擎类型
- app.set('view engine', 'html');
更多 MongoDB 相关教程见以下内容:
MongoDB 文档、集合、数据库简介 http://www.linuxidc.com/Linux/2016-12/138529.htm
MongoDB 3 分片部署及故障模拟验证 http://www.linuxidc.com/Linux/2016-12/138529.htm
Linux CentOS 6.5 yum 安装 MongoDB http://www.linuxidc.com/Linux/2016-12/137790.htm
CentOS 7 yum 方式快速安装 MongoDB http://www.linuxidc.com/Linux/2016-11/137679.htm
MongoDB 的查询操作 http://www.linuxidc.com/Linux/2016-10/136581.htm
在 Azure 虚拟机上快速搭建 MongoDB 集群 http://www.linuxidc.com/Linux/2017-09/146778.htm
MongoDB 复制集原理 http://www.linuxidc.com/Linux/2017-09/146670.htm
MongoDB 3.4 远程连接认证失败 http://www.linuxidc.com/Linux/2017-06/145070.htm
Ubuntu 16.04 中安装 MongoDB3.4 数据库系统 http://www.linuxidc.com/Linux/2017-07/145526.htm
MongoDB 权威指南第 2 版 PDF 完整带书签目录 下载见 http://www.linuxidc.com/Linux/2016-12/138253.htm
MongoDB 的详细介绍: 请点这里 MongoDB 的下载地址: 请点这里
来源: http://www.linuxidc.com/Linux/2017-12/149532.htm