MongoDB的CRUD操作

2017-08-20  本文已影响0人  Eve0

数据库相关


表(集合)相关

显示所有的表/集合: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+版本新特性

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)其他操作

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

3.更新操作update

4.删除操作remove

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

上一篇下一篇

猜你喜欢

热点阅读