人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

MongoDB連接數(shù)據(jù)庫并創(chuàng)建數(shù)據(jù)等使用方法

發(fā)布日期:2022-02-01 20:09 | 文章來源:站長之家

1.mongodb官網(wǎng)

MongoDB: the application data platform | MongoDB

2.進(jìn)入MongoDB官網(wǎng)下載MongoDB以及MongoDB compass 和Mongodb--database--tools

3.nodejs操作MongoDB數(shù)據(jù)庫需要依賴nodejs的第三方包mongoose?

終端指令: npm install mongoose

4.

?5.

以管理員身份運(yùn)行PowerShell cd到文件所在目錄 如果沒有開啟MongoDB的話?

使用net start mongodb 指令啟動(dòng)

?6.

//引入mongoose模塊
const mongoose = require('mongoose');
// console.log(mongoose);
 
//todo 連接數(shù)據(jù)庫
mongoose.connect('mongodb://localhost/test001')
    .then(() => console.log('數(shù)據(jù)庫鏈接成功'))
    .catch(erro => console.log('連接失敗'))

7.在vscode的集成終端中cd到文件所在目錄,使用nodemon 'node 02.js'指令打開文件

?8. 設(shè)定集合規(guī)則 創(chuàng)建集合并應(yīng)用規(guī)則

//todo 設(shè)定集合規(guī)則
const courseSchema = new mongoose.Schema({
    name: String,
    author: String,
    isPublished: Boolean
});
// todo 創(chuàng)建集合并應(yīng)用規(guī)則
// todo 1.集合名稱'' 2.集合規(guī)則
const Course = mongoose.model('Course', courseSchema);

??9. 創(chuàng)建集合實(shí)例document的兩種方式

// todo 第一種方式   創(chuàng)建集合實(shí)例 文檔document
const course = new Course({
    name:'xiaoguo',
    author:'aaa',
    tags:['node','backend'],
    isPublished:false
})
// 將數(shù)據(jù)保存在數(shù)據(jù)庫中
course.save();
 
 
 
//todo 第二種方式 創(chuàng)建文檔 不需要使用course.save()方式保存,會(huì)自動(dòng)保存進(jìn)數(shù)據(jù)庫
Course.create({
    name:'xiaowei',
    author:'sh',
    isPublished:true
},(erro,data)=>{
    console.log(erro);
    console.log(data)
});
//也支持promise對象
Course.create({
    name:'xiaoli',
    author:'zz',
    isPublished:true
}).then(data=> console.log(data))
.catch(erro=>console.log(erro))

?10. 查詢用戶集合中的所有文檔,返回的是一個(gè)數(shù)組

// todo 查詢用戶集合中的所有文檔 返回的是一個(gè)數(shù)組
Course.find()
.then(result =>{console.log(result)})

?11. 通過ID字段查詢用戶集合中的某個(gè)文檔,返回?cái)?shù)組

// todo 通過ID字段查詢用戶集合中的某個(gè)文檔 返回?cái)?shù)組
Course.find({
_id:"619b0f75dc5e07d1b9924ee9"
})
.then(result =>{console.log(result)})

?12. 根據(jù)條件查找文檔 如果不寫條件返回?cái)?shù)據(jù)庫中的第一條文檔 返回一個(gè)對象

// todo 根據(jù)條件查找文檔 如果不寫條件返回?cái)?shù)據(jù)庫中的第一條文檔 返回一個(gè)對象
Course.findOne({
    name:'xiaowei'
})
.then(result=>console.log(result))

13. 根據(jù)范圍條件查找文檔 $gt 最小值 $lt最大值

// todo 根據(jù)范圍條件查找文檔
Course.find({
    age: { $gt: 20, $lt: 50 }
})
.then(result => console.log(result))

?14. 查詢包含

// todo 根據(jù)范圍條件查找文檔
Course.find({
    name: { $in: ['xiao'] }
})
.then(result => console.log(result))

?15. 選擇要查詢的字段并排序 默認(rèn)升序 降序加個(gè)-

// todo 選擇要查詢的字段 (升序)
Course.find().select('name age')
//相反的順序用.sort('-age') (降序)
.then(result => console.log(result))

??16. ?skip跳過前兩條數(shù)據(jù) limit限制查詢數(shù)量

// todo skip跳過前兩條數(shù)據(jù) limit限制查詢數(shù)量
Course.find().skip(2).limit(2)
.then(result => console.log(result))

? ?17. ?查找一個(gè)文檔并刪除文檔 返回值是刪除的文檔 如果匹配到多個(gè)文檔 只刪除第一個(gè)

// todo 查找一個(gè)文檔并刪除文檔 返回值是刪除的文檔 如果匹配到多個(gè)文檔 只刪除第一個(gè)
Course.findOneAndDelete({
   _id:"619b0f75dc5e07d1b9924ee9"
})
.then(result=>console.log(result))

?18. ?刪除多個(gè)文檔 ?返回一個(gè)對象 {n:刪除的文檔數(shù)量 ok:1(刪除成功)}

// todo 刪除多個(gè)文檔  返回一個(gè)對象 {n:刪除的文檔數(shù)量 ok:1(刪除成功)}
Course.deleteMany({
   _id:"619b0f75dc5e07d1b9924ee9"
})
.then(result=>console.log(result))

??19. ?更新單個(gè)文檔 里面?zhèn)鲀蓚€(gè)對象 ,隔開 第一個(gè)對象是查詢條件 第二個(gè)要改的值

// todo 更新單個(gè)文檔 里面?zhèn)鲀蓚€(gè)對象 ,隔開 第一個(gè)對象是查詢條件 第二個(gè)要改的值
Course.updateOne(
{name:'xiaoguo'},
{name:'xiaoguoguo'}
)
.then(result=>console.log(result))

? ?20. ?更新多個(gè)文檔 里面?zhèn)鲀蓚€(gè)對象 ,隔開 第一個(gè)對象是查詢條件 第二個(gè)要改的值

// todo 更新多個(gè)文檔 里面?zhèn)鲀蓚€(gè)對象 ,隔開 第一個(gè)對象是查詢條件 第二個(gè)要改的值
Course.updateMany(
{},
{age:18}
)
.then(result=>console.log(result))

? ??21.?設(shè)置mongoose驗(yàn)證

?針對String類型字段? ? ??required: [true,'錯(cuò)誤說明']? ?必傳字段??

??針對String類型字段? ? ??minlength: [n,'錯(cuò)誤說明']? ? ?? 最小字段長度

?針對String類型字段? ? ???maxlength: [n,'錯(cuò)誤說明']? ? ? 最大字段長度

??針對String類型字段? ? ??trim:true? ? ? ?//去除字符串兩頭的空格

??針對Number類型字段? ? ??min: [n,'錯(cuò)誤說明']? ? ?? 最小數(shù)值

?針對Number類型字段? ? ???max: [n,'錯(cuò)誤說明']? ? ? 最大數(shù)值

?設(shè)置時(shí)間默認(rèn)值 當(dāng)用戶未傳此字段的數(shù)據(jù)時(shí) 啟用當(dāng)前時(shí)間為默認(rèn)值?

?列舉出當(dāng)前字段可以取的值,必須在范圍內(nèi)上傳

?自定義錯(cuò)誤信息時(shí)的格式

制定規(guī)則驗(yàn)證用戶傳入的值的屬性是否符合規(guī)范 自定義錯(cuò)誤信息 message?

?控制臺(tái)獲取錯(cuò)誤信息

?

到此這篇關(guān)于MongoDB連接數(shù)據(jù)庫并創(chuàng)建數(shù)據(jù)等使用方法的文章就介紹到這了,更多相關(guān)MongoDB連接數(shù)據(jù)庫內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

美國服務(wù)器租用

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部