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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MongoDB的基本用法

發(fā)布時間:2025/7/14 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MongoDB的基本用法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

數(shù)據(jù)庫相關(guān)概念

在一個數(shù)據(jù)庫軟件中可以包含多個數(shù)據(jù)倉庫,在每個數(shù)據(jù)倉庫中可以包含多個數(shù)據(jù)集合,每個數(shù)據(jù)集合中可以包含多條文檔(具體的數(shù)據(jù))

?

術(shù)語:

database 數(shù)據(jù)庫,mongoDB 數(shù)據(jù)庫阮家中可以建立多個數(shù)據(jù)庫

collection ?集合,一組數(shù)據(jù)的集合,可以理解為JavaScript中的數(shù)組

document 文檔,一條具體的數(shù)據(jù),可以理解為JavaScript中的對象

filed 字段,文檔中的屬性名稱,可以理解為javascript 中的對象屬性

?

操作數(shù)據(jù)庫

使用node.js 操作數(shù)據(jù)庫需要依賴Node.js 的第三方包 mongoose

運行安裝命令

?

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 會自動創(chuàng)建。

?

增刪改查操作

創(chuàng)建集合:

?創(chuàng)建集合分為兩步,一是對集合設(shè)定規(guī)則,二是創(chuàng)建集合,創(chuàng)建mongoose.Schema 構(gòu)造函數(shù)的實例即可創(chuàng)建集合。

(創(chuàng)建集合后,這時數(shù)據(jù)庫并不會顯示這個集合,只有當(dāng)插入數(shù)據(jù)后,才會真正創(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ù)據(jù)。

分為兩步:1)創(chuàng)建集合實例。2)調(diào)用實例對象下的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))

?

查詢一個數(shù)據(jù)

xxx.findOne() 方法: 返回一個文檔?

// xxx.findOne() 方法 返回一條文檔,默認返回的是第一條文檔,()中可以添加查找條件 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 字段 進行升序排列 User.find().sort('age').then( res => console.log(res) )// 將數(shù)據(jù)按照 age 字段 進行降序排列 字段前面加 - 表示降序排列 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)// 刪除一個 Course.findOneAndDelete({_id: '5d343ab3981298416bd26998'}).then( res => console.log(res))// 刪除多條文檔 不添加條件默認刪除所有文檔,返回值 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)// 修改單個 // xxx.updateOne({查詢條件}, {要修改的值}).then(res => console.log(res)) // 修改文檔的一個字段, updataOne() 里面需要傳兩個參數(shù),第一是要修改的字段,第二個修改成的內(nèi)容 Course.updateOne({author: 'lisi'}, {author: 'wangwu'}).then(res => console.log(res))// 修改多個 查詢條件不寫,則修改所有 // 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的基本用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。