標籤:express replace localhost att 串連數 flow http XML tle
1.MongoDB下載安裝E:\MongoDB
2.建立資料庫檔案的存放位置 E:\MongoDB\data\db
3.啟動mongodb服務
庫操作
建立資料庫:第一步:use 建立資料庫名;第二步:進行此庫相關的操作;如果不進行第二步,該資料庫不會被建立
查看資料庫:show dbs;
建立表:db.createCollection(‘要建立的表名‘);
查看當前資料庫下表: show collections;
刪除當前資料庫指定表:db.表名.drop();
刪除當前資料庫:db.dropDatabase();
---------------------------------------------------------------我是分割線------------------------------------------------------
下面寫一個簡單的demo
1.express建立E:\test 項目
不會點這
2.建立資料庫
E:\MongoDB\bin 啟動 mongo.exe
輸入
use text \建立一個叫text的資料庫
db.createCollection(“users”) \建立一個集合
db.users.insert({“name”:“admin”,“password”:“111”}) \給users集合添加一個文檔。
db.users.find() \查詢你添加的文檔
3.項目串連資料庫
在項目根目錄下建立一個的檔案夾 database ,然後在建立一個db.js E:\test \ database\db.js
[html] view plain copy
- var mongoose = require(‘mongoose‘);
- var db = mongoose.connect(‘mongodb://localhost/text‘);//;串連資料庫
- var Schema = mongoose.Schema; // 建立模型
- var userScheMa = new Schema({
- name: String,
- password: String
- }); // 定義了一個新的模型,但是此模式還未和users集合有關聯
- exports.user = db.model(‘users‘, userScheMa); // 與users集合關聯
4.views檔案夾建立視圖檔案
5.路由的控制
在routes檔案中的index.js
6.啟動node伺服器
在http://localhost:3000/查看
demo檔案:https://github.com/MMMsCheng/nodedemoLogin
Node.js開發 ---- 建立並串連資料庫mongodb