Node js 如何使用 Mongodb

官方文檔: https://mongoosejs.com/docs/index.html

安裝

npm install mongoose --save

基本使用

// 1. 引入包
const mongoose = require("mongoose");

// 2. 連接數(shù)據(jù)庫
mongoose.connect('mongodb://127.0.0.1/test_db');

// 3. 獲取數(shù)據(jù)庫連接對象
const connection = mongoose.connection;

/**
 *  4. 監(jiān)聽數(shù)據(jù)庫狀態(tài)
**/

// 監(jiān)聽數(shù)據(jù)庫的連接的打開和關(guān)閉
connection.once("open", () => {
    console.log("數(shù)據(jù)庫連接成功");
});
connection.once("close", () => {
    console.log("數(shù)據(jù)庫已經(jīng)關(guān)閉連接");
});

// 監(jiān)聽數(shù)據(jù)庫錯(cuò)誤
connection.on("error", error => {
    console.log("數(shù)據(jù)庫錯(cuò)誤|Database error:" + error);
});

// 5. 創(chuàng)建Schema(模式對象) 類似于SQL中的設(shè)計(jì)表字段約束(此處的Schema注意大寫)
let userSchema = new mongoose.Schema({
    name : String,
    age: Number,
    sex: {
        type: String,
        default: "男"
    },
    height: Number
});

// 6. 創(chuàng)建model對象 model("collectionName", "documents")
// 注意: 此處的 collectionName會自動轉(zhuǎn)換為復(fù)數(shù)形式
let userModel = mongoose.model("users", catSchema);
 
// 7. 插入一條數(shù)據(jù)到 users 這個(gè)集合中
userModel.create({ name : "zs", age: 18, height: 172 }, error => {
    if (error) throw error;
    console.log("插入成功");
});

插入數(shù)據(jù)

// 插入一條數(shù)據(jù)
userModel.create([ { name : "zs", age: 18, height: 172 } ], error => {
    if (error) throw error;
    console.log("插入成功");
});

// 一次插入多個(gè)數(shù)據(jù)
userModel.create([
    { name : "zs", age: 18, height: 172 },
    { name : "ls", age: 20, height: 176 },
    { name : "wc", age: 28, height: 178 },
    { name : "ml", age: 32, height: 168 }
], error => {
    if (error) throw error;
    console.log("插入成功");
});

刪除數(shù)據(jù)

userModel.remove({name: 'ml'}, error => {
    if (error) throw error;
    console.log("刪除成功");
});

修改數(shù)據(jù)

userModel.update({'name' : 'zs'}, { $set: {height: 182} }, error => {
    if (error) throw error;
    console.log("修改成功");
});

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

  • 簡單使用
// 帶條件條件的查詢
userModel.find({name: 'zs'}, (error, docs) => {
    if (error) throw error;
    console.log(docs);
});
  • 查詢數(shù)據(jù),限制顯示字段并且跳過前兩個(gè)顯示一個(gè)
// db.users.find({}, {_id:0, __v:0}).skip(2).limit(1);
userModel.find( {}, {  _id: 0, __v:0 }, { skip:2, limit: 1 }, (error, docs) => {
    if (error) throw err;
    console.log(docs);
});

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 關(guān)于Mongodb的全面總結(jié) MongoDB的內(nèi)部構(gòu)造《MongoDB The Definitive Guide》...
    中v中閱讀 32,304評論 2 89
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,555評論 19 139
  • 是什么吸引了我呢? 是那雙眼皮下大大眼睛? 是那調(diào)皮可愛的樣子? 是那偶爾甜甜的笑容? 是那份輕松的自由自在? 而...
    月明星稀閱讀 715評論 0 50
  • 一周前還覺得五公里是個(gè)遙不可及的距離,今天自己已經(jīng)堅(jiān)持跑了四個(gè)五公里! 還記得自己跑第一個(gè)五公里的情景,跑前心中忐...
    木子晨星閱讀 271評論 0 0
  • 滄海橫流,斗轉(zhuǎn)星移。轉(zhuǎn)眼我已經(jīng)在內(nèi)師一載。在今年,我有幸見證了內(nèi)師六十歲生辰。 六十年前,你蹣跚學(xué)步。開始了...
    妮維雅葉閱讀 272評論 0 0

友情鏈接更多精彩內(nèi)容