MongoDB的CRUD操作


数据库相关

  • 显示所有的数据库:show dbs
  • 切换数据库(若不存在,则直接创建):use test
  • 删除数据库:db.dropDatabase()

表(集合)相关

显示所有的表/集合:show tables or show collections
删除表:db.userlist.drop()

1. 插入数据insert

db.userlist.insert({name:'eve0',age:18})
循环插入数据:
for(i =0;i< 10;i++) db.userlist.insert({name:i,age:i+10})
插入的数据显示:

{ "_id" : ObjectId("5997f095440b8ac5f13b92e7"), "name" : 0, "age" : 10 }
{ "_id" : ObjectId("5997f095440b8ac5f13b92e8"), "name" : 1, "age" : 11 }
{ "_id" : ObjectId("5997f095440b8ac5f13b92e9"), "name" : 2, "age" : 12 }
{ "_id" : ObjectId("5997f095440b8ac5f13b92ea"), "name" : 3, "age" : 13 }
{ "_id" : ObjectId("5997f095440b8ac5f13b92eb"), "name" : 4, "age" : 14 }
{ "_id" : ObjectId("5997f095440b8ac5f13b92ec"), "name" : 5, "age" : 15 }
{ "_id" : ObjectId("5997f095440b8ac5f13b92ed"), "name" : 6, "age" : 16 }
{ "_id" : ObjectId("5997f095440b8ac5f13b92ee"), "name" : 7, "age" : 17 }
{ "_id" : ObjectId("5997f095440b8ac5f13b92ef"), "name" : 8, "age" : 18 }
{ "_id" : ObjectId("5997f095440b8ac5f13b92f0"), "name" : 9, "age" : 19 }

3.2+版本新特性

  • 插入单个文档
    db.userlist.insertOne({name:'aaa',age:13})
  • 插入多个文档
    db.userlist.insertMany([{name:'lily',age:12},{name:'peter',age:13}])

2. 查询操作find

db.userlist.find({age:18})

(1) 过滤操作skip

age=18结果集中过滤掉前一个记录db.userlist.find({age:18}).skip(1)

> db.userlist.find({age:18})
{ "_id" : ObjectId("5997ee7b440b8ac5f13b92e4"), "name" : "eve0", "age" : 18 }
{ "_id" : ObjectId("5997f095440b8ac5f13b92ef"), "name" : 8, "age" : 18 }
{ "_id" : ObjectId("5997f26f440b8ac5f13b92f1"), "name" : "amy", "age" : 18 }
{ "_id" : ObjectId("5997f27b440b8ac5f13b92f2"), "name" : "lily", "age" : 18 }
> db.userlist.find({age:18}).skip(1)
{ "_id" : ObjectId("5997f095440b8ac5f13b92ef"), "name" : 8, "age" : 18 }
{ "_id" : ObjectId("5997f26f440b8ac5f13b92f1"), "name" : "amy", "age" : 18 }
{ "_id" : ObjectId("5997f27b440b8ac5f13b92f2"), "name" : "lily", "age" : 18 }
(2) 查询个数count

db.userlist.find({age:18}).count()

(3) 限制返回条数limit

db.userlist.find({age:18}).limit(2)#返回查询结果的前两条数据

(4) 排序字段sort

根据name字段排序,1为正序排序,-1为倒序排序db.userlist.find({age:18}).sort({name:1})

> db.userlist.find({age:18}).sort({name:1})
{ "_id" : ObjectId("5997f095440b8ac5f13b92ef"), "name" : 8, "age" : 18 }
{ "_id" : ObjectId("5997f26f440b8ac5f13b92f1"), "name" : "amy", "age" : 18 }
{ "_id" : ObjectId("5997ee7b440b8ac5f13b92e4"), "name" : "eve0", "age" : 18 }
{ "_id" : ObjectId("5997f27b440b8ac5f13b92f2"), "name" : "lily", "age" : 18 }
> db.userlist.find({age:18}).sort({name:-1})
{ "_id" : ObjectId("5997f27b440b8ac5f13b92f2"), "name" : "lily", "age" : 18 }
{ "_id" : ObjectId("5997ee7b440b8ac5f13b92e4"), "name" : "eve0", "age" : 18 }
{ "_id" : ObjectId("5997f26f440b8ac5f13b92f1"), "name" : "amy", "age" : 18 }
{ "_id" : ObjectId("5997f095440b8ac5f13b92ef"), "name" : 8, "age" : 18 }
(5) 范围操作in

db.userlist.find({name:{$in:['lily','aaa']}})

(6) 与操作and

db.userlist.find({name:'aaa',age:13})

(7) 或操作or

db.userlist.find({$or:[{name:'aaa'},{name:'lily'}]})

(8) 模糊查询-正则表达式

三种格式:

{ <field>: { $regex: /pattern/, $options: '<options>' } }
{ <field>: { $regex: 'pattern', $options: '<options>' } }
{ <field>: { $regex: /pattern/<options> } }
(9)其他操作
  • $eq equal eg:db.userlist.find({name:{$eq:'aaa'}});
  • $gt greater than eg:db.userlist.find({age:{$gt:13}})
  • $gte 大于等于 eg:db.userlist.find({age:{$gte:13}})
  • $lt 小于 eg:db.userlist.find({age:{$lt:13}})
  • $lte 小于等于 eg:db.userlist.find({age:{$lte:13}})
  • $ne 不等于 eg:db.userlist.find({age:{$ne:13}})
  • $nin not in eg:db.userlist.find({name:{$nin:['lily','aaa']}})
  • $mod取模 eg:db.userlist.find({age:{$mod:[12,1]}});第一个参数指的是除数,第二个参数指的是余数
    更多操作点击

此外,查询中的这些限制条件可以叠加使用如db.userlist.find({age:18}).skip(1).limit(2).sort({name:1})表示的是查询age=18的结果集中,过滤前一条数据,限制返回2条数据,根据name正序

3.更新操作update

  • 全部更新
    db.userlist.update({name:8,age:18},{name:'hello',age:27})#第一个参数指的是原数据,第二个参数指的是要修改后的数据
  • 部分更新
    db.userlist.update({name:'hello'},{$set:{age:28}})
  • 更新某条不存在的数据,第三个参数指的是如果不存在则创建
    db.userlist.update({name:'world'},{$set:{age:36}},true)
    查询该条记录显示结果:
    { "_id" : ObjectId("599812ca399eb62bee175c46"), "name" : "world", "age" : 36 }
  • 更新多条数据
    db.userlist.update({age:18},{$set:{class:22}},false,true)

4.删除操作remove

要求必须有参数
db.userlist.remove({name:'eve0'})

  • 删除一个
    db.userlist.deleteOne({age:13});
  • 删除多个
    db.userlist.deleteMany({age:13});

推荐阅读更多精彩内容