12.mongodb的增删改查

操作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.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;
    • goods.find({age:{$gt:20}})
      • 取出集合中json带有age>20 相当于 select * from goods where age > 20;
    • goods.find({age:{$lt:20}})
      • 取出集合中json带有age<20 相当于 select * from goods where age < 20;
    • goods.find({age:{$gte:20}})
      • 取出集合中json带有age>=20 相当于 select * from goods where age >= 20;
    • goods.find({age:{$lte:20}})
      • 取出集合中json带有age<=20 相当于 select * from goods where age <= 20;
    • goods.find({age:{$ne:20}})
      • 取出集合中json带有age!=20 相当于 select * from goods where age != 20;
    • goods.find({}, {age: true})
      • 取出集合中所有数据 只显示age字段 相当于 select age from goods
    • goods.find({age: {$gt:20}}, {age: true, name: true})
      • 取出集合中age>20 只显示age,name字段 相当于 select age,name from goods where age> 20
  • 模糊查询
    • goods.find({name: /^张/})
      • 取出集合中json带有name以张开头的 相当于 select * from goods where name like '张%';
    • goods.find({name: /张$/})
      • 取出集合中json带有name以张结尾的 相当于 select * from goods where name like '%张';
    • goods.find({name: /张/})
      • 取出集合中json带有name有张的 相当于 select * from goods where name like '%张%';
  • 排序
    • goods.find().sort({age:1})
      • 以age字段升序 相当于 select * from goods order by age asc
    • goods.find().sort({age:-1})
      • 以age字段降序 相当于 select * from goods order by age desc
  • 分页查询
    • goods.find().limit(5)
      • 取出前5条数据 相当于 select * from goods limit 0,5
    • goods.find().limit(10).skip(5)
      • 取出5-10条数据 相当于 select * from goods limit 5,10
  • 多条件查询
    • goods.find({age:{$gt:20},name: '张三'})
      • 查询age>20并且name='张三' 相当于 select * from goods where age > 20 and name="张三";
    • goods.find({$or:[{age:{$gt:20}},{name: '张三'}]})
      • 查询age>20或者name='张三' 相当于 select * from goods where age > 20 or 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)
posted @ 2022-03-04 18:02  Mjser  阅读(40)  评论(0)    收藏  举报