MongoDB的基本用法
數(shù)據(jù)庫相關(guān)概念
在一個(gè)數(shù)據(jù)庫軟件中可以包含多個(gè)數(shù)據(jù)倉庫,在每個(gè)數(shù)據(jù)倉庫中可以包含多個(gè)數(shù)據(jù)集合,每個(gè)數(shù)據(jù)集合中可以包含多條文檔(具體的數(shù)據(jù))
?
術(shù)語:
database 數(shù)據(jù)庫,mongoDB 數(shù)據(jù)庫阮家中可以建立多個(gè)數(shù)據(jù)庫
collection ?集合,一組數(shù)據(jù)的集合,可以理解為JavaScript中的數(shù)組
document 文檔,一條具體的數(shù)據(jù),可以理解為JavaScript中的對(duì)象
filed 字段,文檔中的屬性名稱,可以理解為javascript 中的對(duì)象屬性
?
操作數(shù)據(jù)庫
使用node.js 操作數(shù)據(jù)庫需要依賴Node.js 的第三方包 mongoose
運(yùn)行安裝命令
?
npm i mongoose?
連接數(shù)據(jù)庫
1 const mongoose = require('mongoose') 2 3 mongoose.connect('mongodb://localhost/test1', { useNewUrlParser: true }) 4 .then(() => console.log('數(shù)據(jù)庫連接成功')) 5 .catch( err => console.log('數(shù)據(jù)連接失敗' + err))??
創(chuàng)建數(shù)據(jù)庫
在MongoDB 中不需要顯式創(chuàng)建數(shù)據(jù)庫,如果正在使用的數(shù)據(jù)不存在,MongoDB 會(huì)自動(dòng)創(chuàng)建。
?
增刪改查操作
創(chuàng)建集合:
?創(chuàng)建集合分為兩步,一是對(duì)集合設(shè)定規(guī)則,二是創(chuàng)建集合,創(chuàng)建mongoose.Schema 構(gòu)造函數(shù)的實(shí)例即可創(chuàng)建集合。
(創(chuàng)建集合后,這時(shí)數(shù)據(jù)庫并不會(huì)顯示這個(gè)集合,只有當(dāng)插入數(shù)據(jù)后,才會(huì)真正創(chuàng)建。)
1 // 設(shè)定集合規(guī)則 2 const courseSchema = new mongoose.Schema({ 3 name: String, 4 author: String, 5 isPublished: Boolean 6 }) 7 // 創(chuàng)建集合并應(yīng)用規(guī)則 8 const Course = mongoose.model('Course', courseSchema)?
創(chuàng)建文檔:
創(chuàng)建文檔實(shí)際上就是向集合中插入數(shù)據(jù)。
分為兩步:1)創(chuàng)建集合實(shí)例。2)調(diào)用實(shí)例對(duì)象下的save方法將數(shù)據(jù)保存到數(shù)據(jù)庫當(dāng)中。
1 // 插入數(shù)據(jù) 2 const course = new Course ({ 3 name: 'node', 4 author: 'zhangsan', 5 isPublished: true 6 }) 7 course.save()?
向數(shù)據(jù)庫導(dǎo)入數(shù)據(jù)
命令: mongoimport -d 數(shù)據(jù)庫名稱 -c 集合名稱 --flile 要導(dǎo)入的數(shù)據(jù)文件
在導(dǎo)入數(shù)據(jù)之前,需要確保mongodb 已經(jīng)設(shè)置了環(huán)境變量,如果沒有設(shè)置,需要把mongodb/bin 目錄添加到系統(tǒng)環(huán)境中。在設(shè)置好環(huán)境變量之后重啟,就可以使用命令行執(zhí)行操作了。
?
示例:
mongoimport -d test1 -c uesr1 --file ./user.json?
查詢文檔
查找所有數(shù)據(jù)
xxx.find() 方法: 返回一組符合條件的數(shù)據(jù)
xxx.find({查找條件}).then( res => console.log( res )
// 查詢集合中所有文檔 // Course.find().then(res => console.log('查詢結(jié)果' + res))// 根據(jù) _id 查找文檔 Course.find({ _id: '5d343c27d201764240160daf' }).then(res => console.log('查詢結(jié)果' +res))?
查詢一個(gè)數(shù)據(jù)
xxx.findOne() 方法: 返回一個(gè)文檔?
// xxx.findOne() 方法 返回一條文檔,默認(rèn)返回的是第一條文檔,()中可以添加查找條件 Course.findOne().then( res => console.log(res))Course.findOne({name: 'node'}).then( res => console.log( res ))?
條件匹配查詢
xxx.find()
// 匹配數(shù)值范圍 $gt 大于的意思, $lt 小于的意思, 查詢 User 文檔中 age 字段 大于 20 小于 50 的數(shù)據(jù) User.find({ age: { $gt: 20, $lt: 50 } }).then(res => console.log(res))// 匹配包含 $in 是 匹配包含的意思 User.find( {hobbies: { $in: ['足球'] }}).then(res => console.log(res))// 選擇要查詢的字段 (只查詢 name 和 email 字段 并排除 _id 字段 ) -字段名 可以排除查詢到的字段 User.find().select( 'name email -_id').then( res => console.log(res))// 將數(shù)據(jù)按照 age 字段 進(jìn)行升序排列 User.find().sort('age').then( res => console.log(res) )// 將數(shù)據(jù)按照 age 字段 進(jìn)行降序排列 字段前面加 - 表示降序排列 User.find().sort('-age').then( res => console.log(res) )// skip 跳過多少條數(shù)據(jù) limit 限制查詢數(shù)量 User.find().skip(2).limit(5).then( res => console.log(res))?
刪除文檔
const mongoose = require('mongoose')mongoose.connect('mongodb://localhost/test1', { useNewUrlParser: true }) .then(() => console.log('數(shù)據(jù)庫連接成功')) .catch( err => console.log('數(shù)據(jù)連接失敗' + err))// 設(shè)定集合規(guī)則 const courseSchema = new mongoose.Schema({name: String,author: String,isPublished: Boolean }) // 創(chuàng)建集合并應(yīng)用規(guī)則 const Course = mongoose.model('Course', courseSchema)// 刪除一個(gè) Course.findOneAndDelete({_id: '5d343ab3981298416bd26998'}).then( res => console.log(res))// 刪除多條文檔 不添加條件默認(rèn)刪除所有文檔,返回值 ok 等于 表示刪除成功, n 代表刪除數(shù)據(jù)的條數(shù) Course.deleteMany({}).then(res => console.log(res))?
修改文檔
const mongoose = require('mongoose')mongoose.connect('mongodb://localhost/test1', { useNewUrlParser: true }) .then(() => console.log('數(shù)據(jù)庫連接成功')) .catch( err => console.log('數(shù)據(jù)連接失敗' + err))// 設(shè)定集合規(guī)則 const courseSchema = new mongoose.Schema({name: String,author: String,isPublished: Boolean }) // 創(chuàng)建集合并應(yīng)用規(guī)則 const Course = mongoose.model('Course', courseSchema)// 修改單個(gè) // xxx.updateOne({查詢條件}, {要修改的值}).then(res => console.log(res)) // 修改文檔的一個(gè)字段, updataOne() 里面需要傳兩個(gè)參數(shù),第一是要修改的字段,第二個(gè)修改成的內(nèi)容 Course.updateOne({author: 'lisi'}, {author: 'wangwu'}).then(res => console.log(res))// 修改多個(gè) 查詢條件不寫,則修改所有 // xxx.updateMany({查詢條件}, {要修改的值}).then( res => console.log(res)) Course.updateMany({}, {name: 'javascript'}).then( res => console.log(res))?
轉(zhuǎn)載于:https://www.cnblogs.com/liea/p/11221346.html
總結(jié)
以上是生活随笔為你收集整理的MongoDB的基本用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Formform
- 下一篇: ps - 按进程消耗内存多少排序