免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
2 教你如何用js去操作數(shù)據(jù)庫 mongoDB-Moogoose

課堂目標(biāo)

  • 掌握mongodb基本使用
  • 理解文檔型數(shù)據(jù)庫設(shè)計(jì)理念
  • 掌握原生模塊node-mongodb-native應(yīng)用
  • 掌握ODM模塊mongoose應(yīng)用
  • pupteer爬蟲 keystornJs

資源

  • MongoDB:下載
  • node驅(qū)動:文檔
  • mongoose:文檔
  • 可視化工具:Robo3T

mongodb安裝、配置

下載安裝(https://www.runoob.com/mongod...
配置環(huán)境變量
創(chuàng)建dbpath文件夾
啟動: mongod --dbpath=/data

// 查詢所有數(shù)據(jù)庫show dbs// 切換/創(chuàng)建數(shù)據(jù)庫,當(dāng)創(chuàng)建一個集合(table)的時候會自動創(chuàng)建當(dāng)前數(shù)據(jù)庫use test// 得到當(dāng)前db的所有聚集集合db.getCollectionNames()// 查詢db.fruits.find()// 插入一條數(shù)據(jù)db.fruits.save({name:'蘋果',price:5})// 條件查詢db.fruits.find({price:5})db.fruits.find({price: {$lte: 10}})

mongodb更多相關(guān)操作 點(diǎn)擊

ODM - Mongoose

  • 安裝: npm install mongoose -S
  • 基本使用:
const mongoose = require("mongoose");// 1.連接mongoose.connect("mongodb://localhost:27017/test", { useNewUrlParser: true });const conn = mongoose.connection;conn.on("error", () => console.error("連接數(shù)據(jù)庫失敗"));conn.once("open", async () => {    // 2.定義一個Schema - Table    const Schema = mongoose.Schema({        category: String,        name: String    });    // 3.編譯一個Model, 它對應(yīng)數(shù)據(jù)庫中復(fù)數(shù)、小寫的Collection    const Model = mongoose.model("fruit", Schema);    try {        // 4.創(chuàng)建,create返回Promise        let r = await Model.create({            category: "溫帶水果",            name: "蘋果",            price: 5        });        console.log("插入數(shù)據(jù):", r);        // 5.查詢,find返回Query,它實(shí)現(xiàn)了then和catch,可以當(dāng)Promise使用        // 如果需要返回Promise,調(diào)用其exec()        r = await Model.find({ name: "蘋果" });        console.log("查詢結(jié)果:", r);        // 6.更新,updateOne返回Query        r = await Model.updateOne({ name: "蘋果" }, { $set: { name: '芒果' } });        console.log("更新結(jié)果:", r);        // 7.刪除,deleteOne返回Query        r = await Model.deleteOne({ name: "蘋果" });        console.log("刪除結(jié)果:", r);    } catch (error) {        console.log(error);    }});

Schema

  • 字段定義
  const blogSchema = mongoose.Schema({    title: { type: String, required: [true, '標(biāo)題為必填項(xiàng)'] }, // 定義校驗(yàn)規(guī)則    author: String,    body: String,    comments: [{ body: String, date: Date }], // 定義對象數(shù)組    date: { type: Date, default: Date.now }, // 指定默認(rèn)值    hidden: Boolean,    meta: {        // 定義對象        votes: Number,        favs: Number    }});// 定義多個索引blogSchema.index({ title:1, author: 1, date: -1 });const BlogModel = mongoose.model("blog", blogSchema);const blog = new BlogModel({    title: "nodejs持久化",    author: "jerry",    body: "...."});const r = await blog.save();console.log("新增blog", r);
  • 定義實(shí)例方法:抽象出常用方法便于復(fù)用
  // 定義實(shí)例方法?blogSchema.methods.findByAuthor = function () {???return this.model('blog').find({ author: this.author }).exec();}?// 獲得模型實(shí)例?const BlogModel = mongoose.model("blog", blogSchema);?const blog = new BlogModel({...});?// 調(diào)用實(shí)例方法?r = await blog.findByAuthor();?console.log('findByAuthor', r);
  • 靜態(tài)方法
blogSchema.statics.findByAuthor = function(author) {??return this.model("blog")??.find({ author })??.exec();};?r=await BlogModel.findByAuthor('jerry')?console.log("findByAuthor", r);
  • 虛擬屬性
?blogSchema.virtual("commentsCount").get(function() {??return this.comments.length;});?let r = await blog.findOne({author:'jerry'});?console.log("blog留言數(shù):", r.commentsCount);   

購物車相關(guān)接口實(shí)現(xiàn)

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
在nodejs中使用mongodb
GraphQL入門有這一篇就足夠了
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block
Puppeteer 系列踩坑日志—4—跨域的幾種方式
第三方系統(tǒng)訪問微搭低代碼的后端API
如何使用Puppeteer + Chrome將網(wǎng)頁,博客批量導(dǎo)出成PDF?(一)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服