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会为其创建该字段,并更新。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值