mongoDB 常用命令
1 查看帮助文档
help
2 新建一个数据库(切换到某个数据库) use + 数据库名称
use myDB
3 查询当前正在使用哪个数据库
db
4 显示当前有哪些数据库(需要注意的是 当你刚创建了一个 数据库时该数据库为空,则 show dbs 看不到该数据库)
show dbs
5 显示数据库有哪些表
show tables
6 插入数据 使用db.表名.insert() 插入数据 无需关心表名是否存在,也没有字段名的限制
db.mytable.insert({"_id": 1, "name": "java"})
db.mytable.insert({"name": "java", value: "spring"})
7 查询数据 查询数据使用db.表名.find()查询数据
如果不指定条件时则默认查询所有
例如
db.noPK.find()
{ “_id” : ObjectId(“5a50642b908e6b07a84472a2”), “name” : “javascript”, “value” : “vue.js” }
{ “_id” : ObjectId(“5a50703c908e6b07a84472a6”), “name” : “shell”, “type” : “script” }
指定条件查询
db.noPK.find({“name”:”shell”})
{ “_id” : ObjectId(“5a50703c908e6b07a84472a6”), “name” : “shell”, “type” : “script” }
如果希望显示的数据格式化显示,则使用pretty()
例如:> db.noPK.find({"name":"shell"}).pretty() { "_id" : ObjectId("5a50703c908e6b07a84472a6"), "name" : "shell", "type" : "script" }
查询条件
$lt 小于 例如{"age": {$lt:30}}
$lte 小于或等于 例如 {"age": {$lte: 30}}
$gt 大于 例如{"age": {$gt: 30}}
$gte 大于或等于
$ne 不等于
$or 逻辑或 例如 db.user.find({"$or":[{"hobby":"swimming"},{"gender":"female"}]},{"_id":0})
{ "hobby" : "swimming", "gender" : "female" }
{ "hobby" : "swimming", "gender" : "male" }
查询表中一共有多少条数据 使用.count() 方法
例如
db.noPK.find().count()
只查询一条数据 使用findOne()
如果不给参数的话默认返回第一条数据, 如果指定了条件进行查询,返回根据条件查询到的第一条
例如 获取一条数据,不指定条件:db.noPK.findOne()
指定条件
db.noPK.findOne({"name": "javascript"})
如果想查询并删除某条数据可以使用 findOneAndDelete()
同样的如果不指定条件的话 默认删除表中的第一条数据
例如:db.noPK.findOneAndDelete()
指定条件
db.noPK.findOneAndDelete({"name": "was"})
更新数据
使用db.表名.update() 进行更新数据 指定的表必须是存在的
语法如下:
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,就把按条件查出来多条记录全部更新。
例如我们有一张表是 noPK
db.noPK.find()
{ “_id” : ObjectId(“5a50642b908e6b07a84472a2”), “name” : “javascript”, “value” : “vue.js” }
{ “_id” : ObjectId(“5a50655b908e6b07a84472a3”), “name” : “python”, “type” : “script” }
我们需要将name的值是python的更新为shell
需要注意是mongo是区分大小写的当你把python 写错成Python 是无法更新数据的
db.noPK.update({“name”:”Python”}, {$set:{“name”:”shell”}})
WriteResult({ “nMatched” : 0, “nUpserted” : 0, “nModified” : 0 })返回的结果显示更新条目是0 是因为写错成Python
db.noPK.update({“name”:”python”}, {$set:{“name”:”shell”}})
WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })
db.noPK.find()
{ “_id” : ObjectId(“5a50642b908e6b07a84472a2”), “name” : “javascript”, “value” : “vue.js” }
{ “_id” : ObjectId(“5a50655b908e6b07a84472a3”), “name” : “shell”, “type” : “script” }
mongoDB 更新是默认只更新匹配到的第一条数据
如果我们想更新所有的匹配到的数据,则multi 要设置为true
例如:我们的noPk 表 有3个name 都是shell
db.noPK.find()
{ “_id” : ObjectId(“5a50642b908e6b07a84472a2”), “name” : “javascript”, “value” : “vue.js” }
{ “_id” : ObjectId(“5a50655b908e6b07a84472a3”), “name” : “shell”, “type” : “script” }
{ “_id” : ObjectId(“5a506b40908e6b07a84472a4”), “name” : “shell”, “type” : “script” }
{ “_id” : ObjectId(“5a506b9d908e6b07a84472a5”), “name” : “shell”, “type” : “script”, “script_type” : “bash_shell” }
我们要全部更新他们
db.noPK.update({
"name":"shell"}, {$set:{
"name":"Xshell"}}, false, true)
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
删除数据库
use testDropDB
switched to db testDropDB
> db
testDropDB
> db.dropDatabase()
{ "ok" : 1 }
删除表中的数据
删除数据使用
db.表名.remove() 删除表中的数据
删除数据是危险的操作,是无法恢复的
删除数据需要指定条件,如果不指定条件则会清空表
删除数据还有一个justOne的参数,该参数是布尔值,表示是否只删除一条 默认是false
例如我们的表noPK
db.noPK.find()
{ “_id” : ObjectId(“5a50642b908e6b07a84472a2”), “name” : “javascript”, “value” : “vue.js” }
{ “_id” : ObjectId(“5a50655b908e6b07a84472a3”), “name” : “Xshell”, “type” : “script” }
{ “_id” : ObjectId(“5a506b40908e6b07a84472a4”), “name” : “Xshell”, “type” : “script” }
{ “_id” : ObjectId(“5a506b9d908e6b07a84472a5”), “name” : “Xshell”, “type” : “script”, “script_type” : “bash_shell” }
{ “_id” : ObjectId(“5a50703c908e6b07a84472a6”), “name” : “shell”, “type” : “script” }
我们要删除一条数据,指定的条件是name=Xshell
db.noPK.remove({“name”:”Xshell”}, true)
WriteResult({ “nRemoved” : 1 })
db.noPK.find()
{ “_id” : ObjectId(“5a50642b908e6b07a84472a2”), “name” : “javascript”, “value” : “vue.js” }
{ “_id” : ObjectId(“5a506b40908e6b07a84472a4”), “name” : “Xshell”, “type” : “script” }
{ “_id” : ObjectId(“5a506b9d908e6b07a84472a5”), “name” : “Xshell”, “type” : “script”, “script_type” : “bash_shell” }
{ “_id” : ObjectId(“5a50703c908e6b07a84472a6”), “name” : “shell”, “type” : “script” }
通过对比可以看到justOne默认删除是第一条匹配到的数据
还可以通过 deleteOne() 方法来进行删除数据, 如果不指定条件时,默认删除表中的第一条数据
例如:db.noPK.deleteOne()
指定条件删除
db.noPK.deleteOne({"name": "Xshell"})
删除多条数据 使用 deleteMany() 方法来实现,该方法必须指定参数,例如指定id 等于 10 这样就能删除 匹配到的id=10的所有数据
还没有评论,来说两句吧...