20160812mongodb增刪改查和express的理解

安裝數(shù)據(jù)庫(kù)mongodb(非關(guān)系型數(shù)據(jù)庫(kù))

  • 高性能
  • 易于部署
  • 易于讀取
  1. www.mongodb.com下載對(duì)應(yīng)系統(tǒng)的版本

  2. tar zxvf 解壓

  3. 在數(shù)據(jù)庫(kù)文件夾下創(chuàng)建自己的數(shù)據(jù)庫(kù)文件夾

  4. bin文件夾下的
    mongo --dbpath=../名字_db(默認(rèn)端口27017)

  5. 新建終端進(jìn)入bin目錄
    執(zhí)行./mongo

  6. 編寫(xiě)腳本vim mongo.sh

     #!/bin/bash
     cd /home/fengtianhe/mongodb/bin/
     ./mongod --dbpath=../fth_db
    

//顯示數(shù)據(jù)庫(kù)

show dbs;

//使用數(shù)據(jù)庫(kù),如果沒(méi)有此數(shù)據(jù)庫(kù),就創(chuàng)建數(shù)據(jù)庫(kù)

use test;

//創(chuàng)建數(shù)據(jù)庫(kù)后不顯示,需要插入一條數(shù)據(jù)插入數(shù)據(jù)

db.user.insert({"name":"fth","tel":"12345679811"});

//查詢(xún)

db.user.find();

//插入多條

db.user.insertMany([{},{},{}]);

//格式化顯示

db.user.find().pretty();

//使用數(shù)據(jù)庫(kù)

use test;

//顯示表

show collections;

//更新

db.user.update({sex:{$gt:"1"}},{$set:{tel:"132"}});

//查看數(shù)據(jù)庫(kù)版本

db.version()

//獲取表的名字

db.getCollectionNames();

//查詢(xún)當(dāng)前所屬數(shù)據(jù)庫(kù)

db.getName();

//刪除數(shù)據(jù)

db.user.remove({name:"fth"});

//添加數(shù)據(jù)

db.user.update({name:"fth"},{$set:{"zhiye":"xueshen"}},true);


express 框架

  1. 全局安裝:

     npm install -g express-generator
    
  2. 創(chuàng)建項(xiàng)目:

     express -e 項(xiàng)目名
    

views 視圖
routes 路由
app.js 入口文件

  1. 安裝外部依賴(lài)庫(kù) npm install

  2. 啟動(dòng) npm start

  3. 修改首頁(yè) vim view/index.ejs

  4. 修改路由 routes/index.js

    router.get('/', function(req, res, next) {
    res.render('index', { title: 'Express' });
    });


作業(yè)1:整理mongodb增刪改查的筆記

插入數(shù)據(jù)

MongoDB是面向文檔存儲(chǔ)的數(shù)據(jù)庫(kù),文檔結(jié)構(gòu)形式叫BSON(類(lèi)似JSON)

db.c1.insert({name:"user1"});
db.c1.save({_id:1, name:"user1"}); //save()如果已經(jīng)存在就不會(huì)插入,如果不存在就插入這里要與id相同的值才不會(huì)被插入,save就是id相同就更新,沒(méi)有相同id就插入
for(x=1;x<=1000;x++) db.peroson.insert({Name:"zhuima"+x,Age:x%100,Address:x+".Number Beijing China"})//批量插入數(shù)據(jù)
刪除數(shù)據(jù)
db.c1.remove();
db.c1.remove({name:"user1"});
db.peroson.remove({Age:{$gt:1,$lt:10}})//批量刪除
查詢(xún)數(shù)據(jù)
db.c1.find({name:"user1"},{name:1, age:1});
db.peroson.find({Age:{$gt:33,$lt:44}});//按條件查詢(xún)
條件表達(dá)式
符號(hào) 名稱(chēng)
$gt 大于
$lt 小于
$gte 大于等于
$lte 小于等于
$ne 不等于
修改數(shù)據(jù)

語(yǔ)法 db.collection.update( criteria, objNew, upsert, multi )

參數(shù)說(shuō)明:

  • Criteria:用于設(shè)置查詢(xún)條件的對(duì)象
  • Objnew:用于設(shè)置更新內(nèi)容的對(duì)象
  • Upsert:如果記錄已經(jīng)存在,更新它,否則新增一個(gè)記錄
  • Multi:如果有多個(gè)符合條件的記錄,全部更新 注意:默認(rèn)情況下,只會(huì)更新第一個(gè)符合條件的記錄
方法說(shuō)明
  1. $set //修改字段

db.c5.update({name:"user1"},{$set:{age:10}},0,1);

  1. $inc //對(duì)字段進(jìn)行加 a = a+1

db.c5.update({},{$inc:{age:-10}},0,1);

  1. $unset //刪除一個(gè)字段

db.c5.update({},{$unset:{age:1}});

  1. $push

db.c5.update({},{$push:{arr:1}}); //壓入一個(gè)值進(jìn)入數(shù)組

  1. $pushAll

db.c5.update({}, {$pushAll:{arr:[2,3,4]}}); //壓入多個(gè)值進(jìn)入數(shù)組

  1. $pop

db.c5.update({}, {$pop:{arr:1}}); //彈出數(shù)組中的第一個(gè)元素

  1. $addToSet

{$addToSet: {field: value}}

如果filed是一個(gè)已經(jīng)存在的數(shù)組,并且value值不在其中,那么value加入到數(shù)組中

filed不存在,那么把value當(dāng)成一個(gè)數(shù)組存儲(chǔ)到filed中
filed是一個(gè)已經(jīng)存在的非數(shù)組類(lèi)型,那么將會(huì)報(bào)錯(cuò)

  1. 擴(kuò)展肜戶(hù)

{$addToSet:{a:{$each:[1,2,3,4,5,6]}}};

  1. $pull
    語(yǔ)法:{$pull:{field:_value}}
    功能: 如果field是一個(gè)數(shù)組,那么刪除符合_value檢索條件的記錄
    如果field是一個(gè)已經(jīng)存在的非數(shù)組,那么會(huì)報(bào)錯(cuò)

  2. $pullAll

語(yǔ)法: {$pullAll:{field:value_array}}
功能: 同$pull類(lèi)似,只是value的數(shù)據(jù)類(lèi)型是一個(gè)數(shù)組

  1. $rename
    語(yǔ)法:{$rename:{old_field_name: new_field_name}}
    功能:重命名指定的字段名稱(chēng)

  2. 特殊操作符:$

$操作符代碼查詢(xún)記錄中第一個(gè)匹配條件的記錄項(xiàng)
$db.c5.update({"arr.title":"linx"}, {$set:{arr.$.title:"I lover linx"}});

注意: 在數(shù)組中用$配合$unset操作符的時(shí)候,效果不是刪除的元素,而是把匹配的元素變成null
$db.c5.update({"arr.title":"php"},{$unset:{arr.$:1}});

統(tǒng)計(jì):
db.c1.count();
db.c1.find().count();
//count(1); 里面有一個(gè)條件,如果為1,那么會(huì)取條件,如果為0不會(huì)理會(huì)條件語(yǔ)句
排序:
db.c1.find().sort(age:1); //1是升序 0是降序
取多少個(gè)
db.c1.find().limit(4);
$exists操作檢查一個(gè)字段是否存在
db.c2.find({age:{$exists:1}}); 測(cè)試一個(gè)字段是否存在
$in 操作類(lèi)似于傳統(tǒng)關(guān)系數(shù)據(jù)庫(kù)中的IN
db.c1.find({age:{$in:[1,2,3]}})
$nin 與$in相反
db.c1.find({age:{$nin:[2,3,4]}});
$or 查看指定多個(gè)條件的記錄,跟sql的or差不多
db.c1.find({$or:[{name:"user1"},{name:"user2"},{age:10}]});
$nor 與$or相反過(guò)濾指定的條件
db.c1.find({$nor:[{name:"user1"},{name:"user2"},{age:10}]});
NULL查詢(xún)(有一個(gè)值是為null,或者一個(gè)值不存在)
db.c4.find({age:null}); //列出所有為null或者不存在的值

第二種寫(xiě)法

db.c4.find({age:{$exists:1, $in:[null]}});
//兩個(gè)條件,一個(gè)是exists必須存在,二是為null的數(shù)據(jù)

作業(yè)2:安裝express,了解路由,嘗試把express改成mvc

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容