12.mongodb的增刪改查
阿新 • • 發佈:2022-03-04
操作mongodb
- express本身並沒有對mongodb操作提供API
- 操作mongodb需要安裝第三方依賴
- npm i mongodb -S
- 使用程式碼如下
// 引入依賴 const mongoDb = require('mongodb') // 配置本地資料庫地址 let url = 'mongodb://localhost:27017' // 配置要連結的資料庫名稱 let dbName = 'shop' // 進行連線 mongoDb.MongoClient.connect(url, (err, client)=>{ // 出現錯誤會返回一個異常 if (err) throw err // 沒有錯誤會返回這個資料庫連線 console.log('資料庫已連線') // 選中資料庫 const db = client.db(dbName) // 選中集合 const goods = db.collection('goods') // 將集合所有資料取出 goods.find().toArray((err, doc)=>{ console.log(doc) client.close() }) })
新增
- 新增單條資料
- goods.insertOne({name:'張三',age: 18},callback)
- 新增多條資料
- goods.insertMany([
{name:'張三',age: 18},
{name:'張三',age: 19},
{name:'李四',age: 18}
], callback)
- goods.insertMany([
查詢語句
- 條件查詢
- goods.find()
- 取出集合裡面所有資料 相當於
select * from goods
- 取出集合裡面所有資料 相當於
- goods.findOne()
- 取出第一條資料 相當於
select * from goods limit 0,1
- 取出第一條資料 相當於
- goods.find({age:20})
- 取出集合中json帶有age=20 相當於
select * from goods where age = 20;
- 取出集合中json帶有age=20 相當於
- goods.find({age:{$gt:20}})
- 取出集合中json帶有age>20 相當於
select * from goods where age > 20;
- 取出集合中json帶有age>20 相當於
- goods.find({age:{$lt:20}})
- 取出集合中json帶有age<20 相當於
select * from goods where age < 20;
- 取出集合中json帶有age<20 相當於
- goods.find({age:{$gte:20}})
- 取出集合中json帶有age>=20 相當於
select * from goods where age >= 20;
- 取出集合中json帶有age>=20 相當於
- goods.find({age:{$lte:20}})
- 取出集合中json帶有age<=20 相當於
select * from goods where age <= 20;
- 取出集合中json帶有age<=20 相當於
- goods.find({age:{$ne:20}})
- 取出集合中json帶有age!=20 相當於
select * from goods where age != 20;
- 取出集合中json帶有age!=20 相當於
- goods.find({}, {age: true})
- 取出集合中所有資料 只顯示age欄位 相當於
select age from goods
- 取出集合中所有資料 只顯示age欄位 相當於
- goods.find({age: {$gt:20}}, {age: true, name: true})
- 取出集合中age>20 只顯示age,name欄位 相當於
select age,name from goods where age> 20
- 取出集合中age>20 只顯示age,name欄位 相當於
- goods.find()
- 模糊查詢
- goods.find({name: /^張/})
- 取出集合中json帶有name以張開頭的 相當於
select * from goods where name like '張%';
- 取出集合中json帶有name以張開頭的 相當於
- goods.find({name: /張$/})
- 取出集合中json帶有name以張結尾的 相當於
select * from goods where name like '%張';
- 取出集合中json帶有name以張結尾的 相當於
- goods.find({name: /張/})
- 取出集合中json帶有name有張的 相當於
select * from goods where name like '%張%';
- 取出集合中json帶有name有張的 相當於
- goods.find({name: /^張/})
- 排序
- goods.find().sort({age:1})
- 以age欄位升序 相當於
select * from goods order by age asc
- 以age欄位升序 相當於
- goods.find().sort({age:-1})
- 以age欄位降序 相當於
select * from goods order by age desc
- 以age欄位降序 相當於
- goods.find().sort({age:1})
- 分頁查詢
- goods.find().limit(5)
- 取出前5條資料 相當於
select * from goods limit 0,5
- 取出前5條資料 相當於
- goods.find().limit(10).skip(5)
- 取出5-10條資料 相當於
select * from goods limit 5,10
- 取出5-10條資料 相當於
- goods.find().limit(5)
- 多條件查詢
- goods.find({age:{$gt:20},name: '張三'})
- 查詢age>20並且name='張三' 相當於
select * from goods where age > 20 and name="張三";
- 查詢age>20並且name='張三' 相當於
- goods.find({$or:[{age:{$gt:20}},{name: '張三'}]})
- 查詢age>20或者name='張三' 相當於
select * from goods where age > 20 or name="張三";
- 查詢age>20或者name='張三' 相當於
- goods.find({age:{$gt:20},name: '張三'})
- 統計獲取資料數量
- goods.find().count()
刪除
- 刪除單條資料
- goods.deleteOne({age:7},callback) 刪除第一條滿足的資料
- 刪除多條資料
- goods.deleteMany({age:7}, callback) 刪除所有滿足的資料
- goods.deleteMany() 清空集合
- goods.drop() 刪除集合
修改
- 修改單條資料
- goods.updateOne(條件, {$set: 新資料},callback)
- 修改多條資料
- goods.updateMany(條件, {$set: 新資料},callback)
- 關於某個欄位的操作
- goods.updateOne({id:10}, {$set: {name: '張三'}},callback)
- goods.updateOne({id:10}, {$set: {name: ['張三','李四']}},callback)
- goods.updateOne({id:10}, {$unset: {name: ''}},callback)
- goods.updateOne({id:10}, {$inc: {count: 5}},callback)
- goods.updateOne({id:10}, {$inc: {count: -5}},callback)
- goods.updateOne({id:10}, {$push: {hobbies: '吃飯'}},callback)
- goods.updateOne({id:10}, {$push: {hobbies: ['吃飯','睡覺','打豆豆']}},callback)
- goods.updateOne({id:10}, {$pushAll: {hobbies: ['吃飯','睡覺','打豆豆']}},callback)
- goods.updateOne({id:10}, {$pull: {hobbies: '吃飯'}},callback)
- goods.updateOne({id:10}, {$pop: {hobbies: -1}},callback)
- goods.updateOne({id:10}, {$pop: {hobbies: 1}},callback)