【DataBase】MongoDB常用命令
MongoDB 常用命令
登陆和退出
mongo
命令行直接加 MongoDB 服务的 IP 地址,就可以使用默认端口27017
登陆 MongoDB,进入命令行交互环境。使用exit
,回车则退出交互环境。
# mongo 127.0.0.1
MongoDB shell version v3.6.12
connecting to: mongodb://127.0.0.1:27017/test?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0d27a9a2-b6d3-40d7-8af3-e1be57f89fb0") }
MongoDB server version: 3.6.12
数据库级操作
查看数据库
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
wiki 0.001GB
使用指定库
>use wiki
switched to db wiki
查看所有数据集
> show collections
bruteforces
entries
sessions
settings
uplfiles
uplfolders
users
创建数据库
使用use
可以直接创建数据库,不过直到插入数据时,使用 show dbs
才能看到库
> use test
switched to db test
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
wiki 0.001GB
> db.hello.insert({"name":"mongodb"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
wiki 0.001GB
查看当前使用的库
> db
test
删除数据库
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
> db
test
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
wiki 0.001GB
Collection 级操作
新建 collection
> db.createCollection("user"){"ok":1}> show collections
hello
user
效果与使用 db.user.insert({"user":"xxx"})
类似。
删除 collection
> db.user.drop()
true
> show collections
hello
> db.user.drop() ### 再删除已经不存在
false
重命名 collection
> show collections
hello
> db.hello.renameCollection("HELLO"){"ok":1}> show collections
HELLO
查看所有 collection
> show collections
HELLO
创建索引在 HELLO 集合上,建立对 Name 字段的索引,1 代表正序
> db.HELLO.ensureIndex({NAME:1})
Record 级的操作
使用实现存在的 test 数据库做测试,其中有个 Collection:user
插入数据
向 user 插入数据
> db.user.insert({'name':'Gal Gadot','gender':'female','age':28,'salary':11000})
> db.user.insert({'name':'Mikie Hara','gender':'female','age':26,'salary':7000})
也可以使用save
插入
> db.user.save({'name':'Wentworth Earl Miller','gender':'male','age':41,'salary':33000})
查找记录
查看集合所有记录
> db.user.find(){"_id":ObjectId("5ce4f4c33e7e1703c34ec0d1"),"name":"Gal Gadot","gender":"female","age":28,"salary":11000}{"_id":ObjectId("5ce4f4d03e7e1703c34ec0d2"),"name":"Mikie Hara","gender":"female","age":26,"salary":7000}{"_id":ObjectId("5ce4f4dc3e7e1703c34ec0d3"),"name":"Wentworth Earl Miller","gender":"male","age":41,"salary":33000}
查找符合记录的记录
- Exact Equal
> db.user.find({"age":26}){"\_id":ObjectId("5ce4f4d03e7e1703c34ec0d2"),"name":"Mikie Hara","gender":"female","age":26,"salary":7000}
- Great Than
> db.user.find({salary:{\$gt:7000}})
> { "\_id" : ObjectId("5ce4f4c33e7e1703c34ec0d1"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
> { "\_id" : ObjectId("5ce4f4dc3e7e1703c34ec0d3"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- Fuzzy Match
查看名称中包含‘a’的数据
> db.user.find({name:/a/}){"_id":ObjectId("5ce4f4c33e7e1703c34ec0d1"),"name":"Gal Gadot","gender":"female","age":28,"salary":11000}{"_id":ObjectId("5ce4f4d03e7e1703c34ec0d2"),"name":"Mikie Hara","gender":"female","age":26,"salary":7000}{"_id":ObjectId("5ce4f4dc3e7e1703c34ec0d3"),"name":"Wentworth Earl Miller","gender":"male","age":41,"salary":33000}
查询 name 以 W 打头的数据
> db.user.find({name:/^W/}){"_id":ObjectId("5ce4f4dc3e7e1703c34ec0d3"),"name":"Wentworth Earl Miller","gender":"male","age":41,"salary":33000}
- 多条件“与”
查询 age 小于 30,salary 大于 6000 的数据
> db.user.find({age:{$lt:30},salary:{$gt:6000}})
{ "_id" : ObjectId("5ce4f4c33e7e1703c34ec0d1"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("5ce4f4d03e7e1703c34ec0d2"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- 多条件“或”
查询 age 小于 25,或者 salary 大于 10000 的记录
> db.user.find({$or:[{salary:{$gt:10000}},{age:{$lt:25}}]})
{ "_id" : ObjectId("5ce4f4c33e7e1703c34ec0d1"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("5ce4f4dc3e7e1703c34ec0d3"), "name" : "Wentworth Earl Miller", "gender" : "male","age" : 41, "salary" : 33000 }
- 查询一条记录
db.user.findOne({$or:[{salary:{$gt:10000}},{age:{\$lt:25}}]})
{
"\_id" : ObjectId("5ce4f4c33e7e1703c34ec0d1"),
"name" : "Gal Gadot",
"gender" : "female",
"age" : 28,
"salary" : 11000
}
- 查询记录中的指定列
> db.user.find({},{name:1,age:1,salary:1,sex_orientation:true}){"\_id":ObjectId("5ce4f4c33e7e1703c34ec0d1"),"name":"Gal Gadot","age":28,"salary":11000}{"\_id":ObjectId("5ce4f4d03e7e1703c34ec0d2"),"name":"Mikie Hara","age":26,"salary":7000}{"\_id":ObjectId("5ce4f4dc3e7e1703c34ec0d3"),"name":"Wentworth Earl Miller","age":41,"salary":33000}
这里的 1 表示显示此列的意思,也可以用 true 表示。
- 查询指定字段的数据,并去重
> db.user.distinct('gender')["female","male"]
对查询结果集的操作
mongo
也提供了pretty print
工具,db.collection.pretty()
或者是db.collection.forEach(printjson)
- Pretty Print
> db.user.find().pretty(){"\_id":ObjectId("5ce4f4c33e7e1703c34ec0d1"),"name":"Gal Gadot","gender":"female","age":28,"salary":11000}{"\_id":ObjectId("5ce4f4d03e7e1703c34ec0d2"),"name":"Mikie Hara","gender":"female","age":26,"salary":7000}{"\_id":ObjectId("5ce4f4dc3e7e1703c34ec0d3"),"name":"Wentworth Earl Miller","gender":"male","age":41,"salary":33000}
- 指定结果集返回条目
> db.user.find().limit(2)
> { "\_id" : ObjectId("5ce4f4c33e7e1703c34ec0d1"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
> { "\_id" : ObjectId("5ce4f4d03e7e1703c34ec0d2"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- 查询第一条以外的数据
> db.user.find().skip(1){"\_id":ObjectId("5ce4f4d03e7e1703c34ec0d2"),"name":"Mikie Hara","gender":"female","age":26,"salary":7000}{"\_id":ObjectId("5ce4f4dc3e7e1703c34ec0d3"),"name":"Wentworth Earl Miller","gender":"male","age":41,"salary":33000}
- 对结果集排序
升序
> db.user.find().sort({salary:1}){"_id":ObjectId("5ce4f4d03e7e1703c34ec0d2"),"name":"Mikie Hara","gender":"female","age":26,"salary":7000}{"_id":ObjectId("5ce4f4c33e7e1703c34ec0d1"),"name":"Gal Gadot","gender":"female","age":28,"salary":11000}{"_id":ObjectId("5ce4f4dc3e7e1703c34ec0d3"),"name":"Wentworth Earl Miller","gender":"male","age":41,"salary":33000}
降序
> db.user.find().sort({salary:-1}){"_id":ObjectId("5ce4f4dc3e7e1703c34ec0d3"),"name":"Wentworth Earl Miller","gender":"male","age":41,"salary":33000}{"_id":ObjectId("5ce4f4c33e7e1703c34ec0d1"),"name":"Gal Gadot","gender":"female","age":28,"salary":11000}{"_id":ObjectId("5ce4f4d03e7e1703c34ec0d2"),"name":"Mikie Hara","gender":"female","age":26,"salary":7000}
- 统计结果集中的记录数量
> db.user.find().count()
> 3
删除记录
删除整个集合中的所有数据
> db.test.remove()
删除集合中符合过滤条件的数据
> db.test.remove({name:/aws/})
删除符合条件的一条记录
> db.test.remove({name:/aws/},1)
更新操作
db.collection.update(criteria, objNew, upsert, multi )
criteria
:update 的查询条件,类似 sql update 查询内 where 后面的objNew
:update 的对象和一些更新的操作符(如inc...)等,也可以理解为 sql update 查询内 set 后面的。upsert
: 如果不存在 update 的记录,是否插入 objNew,true 为插入,默认是 false,不插入。multi
: mongodb 默认是 false,只更新找到的第一条记录,如果这个参数为 true,就把按条件查出来多条记录全部更新。
> db.user.find()
> { "\_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
> { "\_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
> { "\_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
更新符合过滤条件的信息
> db.user.update({name:'Gal Gadot'},{\$set:{age:23}},false,true)
> db.user.find()
> { "\_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 23, "salary" : 11000 }
> { "\_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
> { "\_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
添加新的字段
> db.user.update({name:'Mikie Hara'},{\$set:{interest:"CBA"}},false,true)
> db.user.find()
> { "\_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 23, "salary" : 11000 }
> { "\_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female","interest" : "CBA", "age" : 26, "salary" : 7000 }
> { "\_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
使用函数 $inc
> db.user.update({gender:'female'},{\$inc:{salary:50}},false,true)
> db.user.find()
> { "\_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 23, "salary" : 11050 }
> { "\_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female","interest" : "CBA", "age" : 26, "salary" : 7050 }
> { "\_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
关于更新操作db.collection.update(criteria, objNew, upsert, multi )
,要说明的是,如果upsert
为true
,那么在没有找到符合更新条件的情况下,mongo 会在集合中插入一条记录其值满足更新条件的记录(其中的 字段只有更新条件中涉及的字段,字段的值满足更新条件),然后将其更新(注意,如果更新条件是$lt 这种不等式条件,那么 upsert 插入的记录只会包含 更新操作涉及的字段,而不会有更新条件中的字段,因为没法为这种字段定值,mongo 索性就不取这些字段)。如果符合条件的记录中没有要更 新的字段,那么 mongo 会为其创建该字段,并更新。