日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

芒果db怎么连mysql_MongoDB 芒果数据库的使用

發布時間:2023/12/10 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 芒果db怎么连mysql_MongoDB 芒果数据库的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、兩種主流的數據庫

關系型數據庫

非關系型數據庫

SQL 就是 Structor Query Language 結構化查詢語言。典型的關系型數據庫最大的優點就是主從查找。。

缺點,就是限制字段,表的字段是不能自由更改的,不能某一個條目有一些字段,另外的條目的沒有。

于此相比 MongoDB 就靈活的多了。

二、MongoDB 安裝和指導

非常簡單,簡單的令人發指。

1.芒果數據庫,官網:https://www.mongodb.com/

下載可以選擇 zip 格式或者 msi 格式,msi 格式就是下一步、下一步安裝。我們講解 zip格式。

解壓縮這個文件夾,千萬不要有中文路徑

解壓之后找到你的 bin 文件夾,再次確認沒有中文路徑。

將這個bin 文件夾路徑設置為系統的環境變量。

在系統任何盤符下能夠輸入mongo命令不會報“mongo不是可以執行的命令”錯誤。說明你已經成功安裝。

學習教程查找:

1

2

3

4

三、數據庫的基本操作

要找一個地方建立文件夾存儲數據庫。我建的文件夾名為 database。

首先需要使用 mongod 來“開機”,表示打開數據庫。用 mongod 來開機。

開機命令:

mongod --dbpath D:/mongodb/database

--dbpath表示數據庫的路徑。

此時光標是掛起狀態的。

等待連接,提示如下:

2019-08-06T21:57:16.391+0800 I NETWORK [initandlisten] waiting for connections

on port 27017

補充如果連接了顯示0 connections now open 如果沒連接數據庫了顯示1 connections now open

2019-08-06T22:00:32.272+0800 I NETWORK [conn1] end connection 127.0.0.1:49588 (

0 connections now open)

2019-08-06T22:00:41.539+0800 I NETWORK [listener] connection accepted from 127.

0.0.1:49589 #2 (1 connection now open)

此時注意,這個CMD窗口不能關閉,一關閉CMD窗口,數據庫就關閉了。

再打開一個新的CMD窗口:輸入 mongo 就能進入 mongo 的 REPL 環境(Read-Eval-Print-Loop,讀一句、執行一句、顯示一句)

我輸入 100 + 99 回車計算輸出結果如下

操作數據庫就是在 REPL 里面操作的。

在REPL環境中:

> use student

這行語句表示使用 student 數據庫,此時沒有 student 數據庫,MongoDB會幫我們自動創建。

此時我們試著插入一條數據:

> db.class1.insert({"people":57,"gir":38,"boy":19})

提示成功寫入一條數據

class1 這個集合完全可以當做,student 這個數據庫的表。

>db.class1.find()

此時我們想查看這條數據:

四、看看 NodeJS 和 MongoDB 怎樣連接

基本連接

新建一個 nodedb 文件夾,安裝依賴,需要注意的是如果安裝最新版的會發出警告:

(node:4488) DeprecationWarning: current URL string parser is deprecated, and wil

l be removed in a future version. To use the new parser, pass option { useNewUrl

Parser: true } to MongoClient.connect.

版本問題,測試結果如下:

本人實測node安裝Mongodb不警告,最高版本3.0.9.png

安裝依賴并指定版本:

npm install --save mongodb@3.0.9

補充個知識點 node 內置 assert (斷言)的用法

在 01.js 文件輸入以下內容,node 01.js 運行

const assert = require("assert");

let a = 1438;

console.log("如果assert.equal()里面的兩個參數相等,就會輸出五角星")

assert.equal(a,1438);

console.log("☆☆☆☆☆☆☆☆");

assert.equal()里面的兩個參數相等,后面的語句成功執行。

現在改變以下:

const assert = require("assert");

let a = 1438;

console.log("如果assert.equal()里面的兩個參數相等,就會輸出五角星")

assert.equal(a,"你是三八");

console.log("☆☆☆☆☆☆☆☆");

assert.equal()里面的兩個參數不相等,成功報錯,后面的語句不執行。其中相等比較包含隱式轉換。

// 引入mongodb

const MongoClient = require('mongodb').MongoClient;

// 斷言為下面的連接狀態返回結果是true還是false準備

const assert = require('assert');

// Connection URL數據庫連接網址

const url = 'mongodb://localhost:27017';

// Database Name數據庫名,沒有會自動創建

const dbName = 'class';

// Create a new MongoClient實例化這個方法

const client = new MongoClient(url);

// Use connect method to connect to the Server

// 連接數據庫

client.connect(function(err) {

//如果連接錯誤則會拋出錯誤,不在執行此語句的下面的語句

assert.equal(null, err);

// 上面的語句沒有錯誤,則顯示正確連接

console.log("Connected successfully to server");

// 連接database這個數據庫

const db = client.db(dbName);

// ============數據庫操作區域===================

// 關閉數據庫連接

client.close();

// ============數據庫操作區域===================

});

引入文檔內容添加注釋,然后 node 返回結果成功!完美。

數據庫成功連接

不過的提一句,斷言 assert 打斷程序,因為直接刨一個錯誤,也不給提示,不很友好所以使用 if 替代是比較好的方案。

五、集合和文檔的概念

數據庫由集合組成,集合就是 JSON 的集合,每一條 JSON 叫做一個文檔。

集合:

collection

文檔:

JSON

MongoDB 用 JSON 來存儲數據。可以想當然的認為一個 JSON 就是一個 document ,一個 document (文檔)就是一個表。

六、node 操作 MongoDB進行增刪查改

6.1 、增

增加多條insertMany([],callback)

增加我所有女朋友的列表。嘻嘻嘻嘻

// 引入mongodb

const MongoClient = require('mongodb').MongoClient;

// Connection URL數據庫連接網址

const url = 'mongodb://localhost:27017';

// Database Name數據庫名,沒有會自動創建

const dbName = 'class';

// Create a new MongoClient實例化這個方法

const client = new MongoClient(url);

// Use connect method to connect to the Server

// 連接數據庫

client.connect(function(err) {

//如果連接錯誤則會拋出錯誤,不在執行此語句的下面的語句

if(err){

console.log("程序運行出錯,有可能你沒有使用(mongod --dnpath D:/mongod/database)打開數據庫")

return;

}

// 上面的語句沒有錯誤,則顯示正確連接

console.log("Connected successfully to server");

// 連接database這個數據庫

const db = client.db(dbName);

// ============數據庫操作區域===================

db.collection("students").insertMany([

{"class":"高一一班","name":"花月","sex":"girl","age":19,"height":"170","weight":45},

{"class":"高一二班","name":"紫萱","sex":"girl","age":17,"height":"168","weight":48},

{"class":"高一三班","name":"佳寧","sex":"girl","age":20,"height":"164","weight":46},

{"class":"高一一班","name":"香巧","sex":"girl","age":19,"height":"169","weight":50},

{"class":"高一一班","name":"惜玉","sex":"girl","age":17,"height":"157","weight":52},

{"class":"高一四班","name":"雅靜","sex":"girl","age":16,"height":"160","weight":43},

{"class":"高一一班","name":"玥婷","sex":"girl","age":18,"height":"162","weight":56},

{"class":"高一四班","name":"詩琪","sex":"girl","age":19,"height":"163","weight":58},

{"class":"高一一班","name":"欣怡","sex":"girl","age":20,"height":"164","weight":55},

{"class":"高一一班","name":"玥怡","sex":"girl","age":19,"height":"163","weight":48},

{"class":"高一四班","name":"夢瑤","sex":"girl","age":17,"height":"161","weight":45},

{"class":"高一一班","name":"憐雪","sex":"girl","age":19,"height":"162","weight":46},

{"class":"高一二班","name":"安婷","sex":"girl","age":21,"height":"163","weight":41},

{"class":"高一五班","name":"怡瑤","sex":"girl","age":20,"height":"164","weight":52},

{"class":"高一一班","name":"韻茹","sex":"girl","age":16,"height":"165","weight":51},

{"class":"高一一班","name":"念蕾","sex":"girl","age":19,"height":"166","weight":43},

{"class":"高一五班","name":"一萌","sex":"girl","age":20,"height":"160","weight":42},

{"class":"高一一班","name":"凌旋","sex":"girl","age":21,"height":"159","weight":51},

{"class":"高一二班","name":"芷夢","sex":"girl","age":22,"height":"171","weight":57},

{"class":"高一一班","name":"紫夏","sex":"girl","age":15,"height":"168","weight":56},

{"class":"高一一班","name":"蕓萱","sex":"girl","age":16,"height":"165","weight":56},

{"class":"高一五班","name":"靖瑤","sex":"girl","age":15,"height":"164","weight":48}

],function(err,result){

//拋出一個錯誤

if(err){

console.log(err)

return;

}

//result是所有的數據庫變動信息,result以對象方式存儲的,ops屬性就是插入的數據

//insertedCount屬性表示插入的條目數量

console.log("成功插入了" + result.insertedCount + "條數據");

// 關閉數據庫連接

client.close();

});

// ============數據庫操作區域===================

});

運行結果

繼續輸入 Mongo 進入數據庫的 REPL 查看插入結果:

插入一條數據inserOne({},callback)

// 引入mongodb

const MongoClient = require('mongodb').MongoClient;

// Connection URL數據庫連接網址

const url = 'mongodb://localhost:27017';

// Database Name數據庫名,沒有會自動創建

const dbName = 'class';

// Create a new MongoClient實例化這個方法

const client = new MongoClient(url);

// Use connect method to connect to the Server

// 連接數據庫

client.connect(function(err) {

//如果連接錯誤則會拋出錯誤,不在執行此語句的下面的語句

if(err){

console.log("程序運行出錯,有可能你沒有使用(mongod --dnpath D:/mongod/database)打開數據庫")

return;

}

// 上面的語句沒有錯誤,則顯示正確連接

console.log("Connected successfully to server");

// 連接database這個數據庫

const db = client.db(dbName);

// ============數據庫操作區域===================

db.collection("students").insertOne(

{"class":"高一零班","name":"不悔","sex":"girl","age":18,"height":"165","weight":46}

,function(err,result){

//拋出一個錯誤

if(err){

console.log(err)

return;

}

//result是所有的數據庫變動信息,result以對象方式存儲的,ops屬性就是插入的數據

//insertedCount屬性表示插入的條目數量

console.log("成功插入了" + result.insertedCount + "條數據");

// 關閉數據庫連接

client.close();

});

// ============數據庫操作區域===================

});

插入一條數據

查詢結果:

查詢結果

6.2、 刪deleteMany({},callback)

// 引入mongodb

const MongoClient = require('mongodb').MongoClient;

// Connection URL數據庫連接網址

const url = 'mongodb://localhost:27017';

// Database Name數據庫名,沒有會自動創建

const dbName = 'class';

// Create a new MongoClient實例化這個方法

const client = new MongoClient(url);

// Use connect method to connect to the Server

// 連接數據庫

client.connect(function(err) {

//如果連接錯誤則會拋出錯誤,不在執行此語句的下面的語句

if(err){

console.log("程序運行出錯,有可能你沒有使用(mongod --dnpath D:/mongod/database)打開數據庫")

return;

}

// 上面的語句沒有錯誤,則顯示正確連接

console.log("Connected successfully to server");

// 連接database這個數據庫

const db = client.db(dbName);

// ============數據庫操作區域===================

db.collection("students").deleteMany(

{"class":"高一零班","name":"不悔"}

,function(err,result){

//拋出一個錯誤

if(err){

console.log(err)

return;

}

//deletedCount 屬性表示刪除的條目數量

console.log("成功刪除了" + result.deletedCount + "條數據");

// 關閉數據庫連接

client.close();

});

// ============數據庫操作區域===================

});

{"class":"高一零班","name":"不悔"},刪除我所有在高一零班且名叫不悔的女朋友。

測試結果:

再次查詢

{"class":"高一零班","name":"不悔","sex":"girl","age":18,"height":"165","weight":46}

這個數據沒了。成功刪除!

6.3、改

updateOne({},{$set:{}},callback) 和 replaceOne

匹配第一個班級為高一一班的把她變性為男孩

// ============數據庫操作區域===================

db.collection("students").replaceOne(

{"class":"高一一班"},{"sex":"boy"}

,function(err,result){

//拋出一個錯誤

if(err){

console.log(err)

return;

}

console.log("成功匹配"+result.matchedCount+"個,一共修改了"+ result.modifiedCount +"個")

// 關閉數據庫連接

client.close();

});

// ============數據庫操作區域===================

提示:以前我用 updateOne 直接修改,例如:{"class":"高一一班"},{"sex":"boy"} 這個 JSON 數據其他未改的項會消失,現在不能這么寫了。會提示:

MongoError: the update operation document must contain atomic operators

這個問題查了老半天,百度的搜索結果太垃圾了。使用微軟的 bing 馬上得到結果:

來源網址:

我認為這更改為引入updateOne()方法以及update()和updateMany()的副作用,作為一種防止用戶意外覆蓋整個文檔的安全措施。

更改動作必須包含原子操作符;什么是原子操作符就是 $set 這種,看演示。

我們把上面那個想不開變了性的女孩再給改回來。

// ============數據庫操作區域===================

db.collection("students").updateOne(

{"class":"高一一班"},{$set:{"sex":"girl"}}

,function(err,result){

//拋出一個錯誤

if(err){

console.log(err)

return;

}

console.log("成功匹配"+result.matchedCount+"個,一共修改了"+ result.modifiedCount +"個")

// 關閉數據庫連接

client.close();

});

// ============數據庫操作區域===================

從結果上來看 updateOne 和 replaceOne 功能上沒什么不同就是寫法稍有不同。

更改多個 updateMany({},{},callback)

現在班級為高一一班的女孩全想不開非要去變性:

// ============數據庫操作區域===================

db.collection("students").updateMany(

{"class":"高一一班"},{$set:{"sex":"boy"}}

,function(err,result){

//拋出一個錯誤

if(err){

console.log(err)

return;

}

console.log("成功匹配"+result.matchedCount+"個,一共修改了"+ result.modifiedCount +"個")

// 關閉數據庫連接

client.close();

});

// ============數據庫操作區域===================

查看輸出結果:

輸出結果

查看數據庫結果:

一共十一個

6.4、查

查詢所有 find()toArray(function)

// ============數據庫操作區域===================

db.collection("students").find().toArray(function(err,result){

//拋出一個錯誤

if(err){

console.log(err)

return;

}

console.log("成功查詢了" + result.length + "條信息");

// 查詢所有的JSON將會放在result這個數組里面

console.log(result);

// 關閉數據庫連接

client.close();

});

// ============數據庫操作區域===================

查看輸出的 22 條結果:

輸出結果

且邏輯查詢:(逗號表示且)

找出年級是高一一班且年齡為16 的所有人。

// ============數據庫操作區域===================

db.collection("students").find({"class":"高一一班","age":16}).toArray(function(err,result){

//拋出一個錯誤

if(err){

console.log(err)

return;

}

console.log("成功查詢了" + result.length + "條信息");

// 查詢所有的JSON將會放在result這個數組里面

console.log(result);

// 關閉數據庫連接

client.close();

});

// ============數據庫操作區域===================

年級是高一一班且年齡為16

大于邏輯查詢

找出身高大于 165 且體重大于 55 的人。

// ============數據庫操作區域===================

db.collection("students").find({"height":{$gt:"165"},"weight":{$gt:55}}).toArray(function(err,result){

//拋出一個錯誤

if(err){

console.log(err)

return;

}

console.log("成功查詢了" + result.length + "條信息");

// 查詢所有的JSON將會放在result這個數組里面

console.log(result);

// 關閉數據庫連接

client.close();

});

// ============數據庫操作區域===================

身高大于 165 且體重大于 55

大于(gt)小于(lt)等于(eq)都是使用的原子操作符。

或邏輯查找 {$or:[{},{},{}]}

尋找年齡小于 16 或 體重小于 43 的所有人

// ============數據庫操作區域===================

db.collection("students").find({$or:[{"age":{$lt:16}},{"weight":{$lt:43}}]}).toArray(function(err,result){

//拋出一個錯誤

if(err){

console.log(err)

return;

}

console.log("成功查詢了" + result.length + "條信息");

// 查詢所有的JSON將會放在result這個數組里面

console.log(result);

// 關閉數據庫連接

client.close();

});

// ============數據庫操作區域===================

年齡小于 16 或 體重小于 43

五、總結

MongoDB REPL 常用命令:

顯示所有的數據庫

> show dbs

使用某個(class)數據庫

> use class

顯示當前數據庫的所有表(文檔)

> show collections

查詢當前數據對應的 student 表的數據

> db.students.find()//find()里面可加入 JSON作為篩選條件

給表插入一一些數據

> db.students.insert({})

刪庫

> db.dropDatabase()

刪表

> db.students.drop()

導入數據庫

我再在 D:\mongodb\nodedb 新建一個 students.txt 文件,文件內容如下:

students.txt

打開數據庫:

>mongod --dbpath D:/mongodb/database

現在往 dbs 這個數據庫里面的 class 表導入我們的 students.txt 數據。

mongoimport -d dbs -c class D:/mongodb/nodedb/students.txt

2019-08-07T14:07:29.130+0800 connected to: localhost

2019-08-07T14:07:29.526+0800 imported 22 documents

-d 參數表示需要往哪個數據庫中導入數據

-c 參數表示需要往哪個集合(表)中導入數據

提示你成功的導入了 22 條文檔:

注意的是:數據庫仍然要維持開機狀態。但千萬不要進入REPL中輸入命令mongoimport 導入命令!

導出數據庫

mongoexport -d dbs -c class -o 1.txt

其中 -o 是 output 的意思。

前面沒講 MongoDB 的優點。 MongoDB 是非結構型數據庫。比 SQL 的地方在于不限制字段,每個條目(MongoDB中稱為文檔,一個文檔就是一個標準JSON)都可以有自己的字段。而我們在操作數據用到最多的就是 JSON 格式的數據。

完。。。

總結

以上是生活随笔為你收集整理的芒果db怎么连mysql_MongoDB 芒果数据库的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。