nodejs操作MongoDB

1、安裝MongoDB

npm install mongodb --save-dev? /? cnpm install mongodb --save-dev

2、要在 MongoDB 中創(chuàng)建一個(gè)數(shù)據(jù)庫,首先我們需要?jiǎng)?chuàng)建一個(gè) MongoClient 對(duì)象,然后配置好指定的 URL 和 端口號(hào)。

如果數(shù)據(jù)庫不存在,MongoDB 將創(chuàng)建數(shù)據(jù)庫并建立連接。

var MongoClient = require('mongodb').MongoClient;

var url = "mongodb://localhost:27017/runoob";

MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {??

if (err) throw err;

? console.log("數(shù)據(jù)庫已創(chuàng)建!");

? db.close();

});

3、創(chuàng)建數(shù)據(jù)庫student

var student = db.db('student')

4、數(shù)據(jù)庫student中創(chuàng)建表user

student.createCollection('user',function(err,res){})

5、user表中插入一條數(shù)據(jù)myobj

student.collection('user').insertOne(myobj,function(err,res){})

user表中插入多條數(shù)據(jù)myobj

student.collection('user').insertMany(myobj,function(err,res){})

6、查找

student.collection("user"). find({}).toArray(function(err, result) { // 返回集合中所有數(shù)據(jù) if (err) throw err;

? ? ? ? console.log(result);

? ? ? ? db.close();

? ? });

7、更新一條

var whereStr = {"name":'菜鳥教程'}; // 查詢條件?

?var updateStr = {$set: { "url" : "https://www.runoob.com" }};

?student.collection("user").updateOne(whereStr, updateStr, function(err, res) {? ? ?

?? if (err) throw err;

? ? ? ? console.log("文檔更新成功");

? ? ? ? db.close();

? ? });

更新多條數(shù)據(jù)使用updateMany()

8、刪除一條張三的數(shù)據(jù)

var whereStr = {'name':'張三'}

student.collection('user').deleteOne(whereStr,function(err,res){})

刪除多條數(shù)據(jù)deleteMany()

9、排序sort()

參數(shù){ type: 1 } // 按 type 字段升序

{ type: -1 } // 按 type 字段降序

使用

student.collect('user').find().sort({'name':1}).toArray(function(err,result){})

10、查詢分頁limit()

如果要設(shè)置指定的返回條數(shù)可以使用?limit()?方法,該方法只接受一個(gè)參數(shù),指定了返回的條數(shù)。

student.collect('user').find().limit(number).toArray(function(err,result){})

11、跳過指定數(shù)據(jù)

如果要指定跳過的條數(shù),可以使用?skip()?方法。

跳過前面兩條數(shù)據(jù),讀取兩條數(shù)據(jù)

student.collect('user').find().skip(2).limit(2).toArray(function(err,result){})

12、刪除集合drop()

student.collection("user").drop(function(err, delOK) { // 執(zhí)行成功 delOK 返回 true,否則返回 false?

?if (err) throw err;

? ? ? ? if (delOK) console.log("集合已刪除");

? ? ? ? db.close();

? ? });

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

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

  • 安裝 npm install mongodnpm install mongoose 定義數(shù)據(jù)模型 var Blog...
    ZGKzm閱讀 669評(píng)論 0 0
  • MongoDB 的簡介 MongoDB是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫。由C++語言編寫,旨在為WEB應(yīng)用提供可擴(kuò)...
    月帝閱讀 7,916評(píng)論 1 4
  • 第三方模塊庫 www.npmjs.com 使用第三方模塊mongoose 安裝 實(shí)例 (1)插入數(shù)據(jù) (2) 查詢...
    Success85閱讀 219評(píng)論 0 0
  • 概述 原生nodejs操作mongoDB通過 mongoDB下的 MongoClient 實(shí)現(xiàn) 官網(wǎng)API地址版本...
    squidbrother閱讀 1,374評(píng)論 0 0
  • 中午我在洗衣服,婆婆在做飯,兒子在沙發(fā)上玩手機(jī),小寶在床上哭,我想的是盡快洗完去抱小寶,兒子實(shí)在是聽不下去了,把弟...
    江流利閱讀 160評(píng)論 0 0

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