> show dbs admin 0.000GB local 0.000GB pmall 0.000GB
没有数据的数据库是不显示的正则表达式
> db test
若是指定的库名存在则切换,不存在则建立sql
> use pmall switched to db pmall
> db.dropDatabase() { "dropped" : "pmall_test", "ok" : 1 }
> use admin switched to db admin > db.auth("root","111111") 1
用户会所有建立在admin库的system.users集合中mongodb
> use pmall switched to db pmall > db.createUser( ... { ... user:"imlichao", ... pwd:"111111", ... roles:["read","readWrite","dbAdmin","userAdmin"] ... } ... ) Successfully added user: { "user" : "imlichao", "roles" : [ "read", "readWrite", "dbAdmin", "userAdmin" ] }
权限解释数据库
read:容许用户读取指定数据库 readWrite:容许用户读写指定数据库 dbAdmin:容许用户在指定数据库中执行管理函数,如索引建立、删除,查看统计或访问system.profile userAdmin:容许用户向system.users集合写入,能够找指定数据库里建立、删除和管理用户 clusterAdmin:只在admin数据库中可用,赋予用户全部分片和复制集相关函数的管理权限。 readAnyDatabase:只在admin数据库中可用,赋予用户全部数据库的读权限 readWriteAnyDatabase:只在admin数据库中可用,赋予用户全部数据库的读写权限 userAdminAnyDatabase:只在admin数据库中可用,赋予用户全部数据库的userAdmin权限 dbAdminAnyDatabase:只在admin数据库中可用,赋予用户全部数据库的dbAdmin权限。 root:只在admin数据库中可用。超级帐号,超级权限 |
> db.createCollection("myuser") { "ok" : 1 }
> db.createCollection("myuser") { "ok" : 1 } > db.myuser.drop() true
> show collections myuser system.users
> db.myuser.find() { "_id" : ObjectId("5b30a7ffec543cee8e9e70e3"), "username" : "imlichao", "password" : "111111" } { "_id" : ObjectId("5b30a8dcec543cee8e9e70e5"), "username" : "imlichao1", "password" : "111111" } { "_id" : ObjectId("5b30a8e1ec543cee8e9e70e6"), "username" : "imlichao2", "password" : "111111" } { "_id" : ObjectId("5b30a8e6ec543cee8e9e70e7"), "username" : "imlichao3", "password" : "111111" }
> db.myuser.insert( ... { ... username:"imlichao", ... password:"111111" ... } ... ) WriteResult({ "nInserted" : 1 })
若是collection name是一个不存在的集合,则会自动建立这个集合。函数
语法格式:编码
db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } )
参数说明:spa
更新找到的第一个记录code
> db.myuser.update( ... { ... "username":"imlichao" ... }, ... { ... $set:{"password":"222222"} ... } ... ) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
更新所有记录 对象
> db.myuser.update( ... { ... "username":"imlichao" ... }, ... { ... $set:{"password":"222222"} ... }, ... { ... multi:true ... } ... ) WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
save() 方法经过传入的文档来替换已有文档。blog
语法格式:
db.collection.save( <document>, { writeConcern: <document> } )
参数说明:
> db.myuser.save( ... { ... "_id":ObjectId("5b3190330d9d1b3044553c1a"), ... "username":"miaomiao", ... "password":"888888" ... } ... ) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.myuser.deleteOne( ... { ... "username":"imlichao" ... } ... ) { "acknowledged" : true, "deletedCount" : 1 }
> db.myuser.deleteMany( ... { ... "username":"imlichao1" ... } ... ) { "acknowledged" : true, "deletedCount" : 1 }
语法格式:
db.collection.find(query, projection)
条件查询语法:
若是你熟悉常规的 SQL 数据,经过下表能够更好的理解 MongoDB 的条件语句查询:
操做 | 格式 | 范例 | RDBMS中的相似语句 |
---|---|---|---|
等于 | {<key>:<value> } |
db.col.find({"by":"菜鸟教程"}).pretty() |
where by = '菜鸟教程' |
小于 | {<key>:{$lt:<value>}} |
db.col.find({"likes":{$lt:50}}).pretty() |
where likes < 50 |
小于或等于 | {<key>:{$lte:<value>}} |
db.col.find({"likes":{$lte:50}}).pretty() |
where likes <= 50 |
大于 | {<key>:{$gt:<value>}} |
db.col.find({"likes":{$gt:50}}).pretty() |
where likes > 50 |
大于或等于 | {<key>:{$gte:<value>}} |
db.col.find({"likes":{$gte:50}}).pretty() |
where likes >= 50 |
不等于 | {<key>:{$ne:<value>}} |
db.col.find({"likes":{$ne:50}}).pretty() |
where likes != 50 |
与 | {<key>:<value>,<key>:<value> } |
db.col.find({"by":"a", "by1":"b"}).pretty() | where by ='a'and by1 ='b' |
或 | {$or:[{<key>:<value> }, {<key>:<value> }]} |
db.col.find({$or:[{"by":"a"},{"by1": "b"}]}).pretty() | where by ='a'or by1 ='b' |
类型查询:
$type操做符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。
> db.myuser.find({"username" : {$type : 2}}).pretty() { "_id" : ObjectId("5b30a8e1ec543cee8e9e70e6"), "username" : "imlichao2", "password" : "111111" } { "_id" : ObjectId("5b30a8e6ec543cee8e9e70e7"), "username" : "imlichao3", "password" : "111111" } { "_id" : ObjectId("5b3190330d9d1b3044553c1a"), "username" : "miaomiao", "password" : "888888" }
类型及编码对应:
型 | 数字 | 备注 |
---|---|---|
Double | 1 | |
String | 2 | |
Object | 3 | |
Array | 4 | |
Binary data | 5 | |
Undefined | 6 | 已废弃。 |
Object id | 7 | |
Boolean | 8 | |
Date | 9 | |
Null | 10 | |
Regular Expression | 11 | |
JavaScript | 13 | |
Symbol | 14 | |
JavaScript (with scope) | 15 | |
32-bit integer | 16 | |
Timestamp | 17 | |
64-bit integer | 18 | |
Min key | 255 | Query with -1. |
Max key | 127 |
正则表达式(模糊查询):
使用$regex操做符
> db.myuser.find({ ... "username":{$regex:"iM",$options:"$i"} ... }).pretty() { "_id" : ObjectId("5b30a8e1ec543cee8e9e70e6"), "username" : "imlichao2", "password" : "111111" } { "_id" : ObjectId("5b30a8e6ec543cee8e9e70e7"), "username" : "imlichao3", "password" : "111111" }
$options的$i表明不区分大小写
不使用$regex操做符
> db.myuser.find({ ... "username":/iM/i ... }).pretty() { "_id" : ObjectId("5b30a8e1ec543cee8e9e70e6"), "username" : "imlichao2", "password" : "111111" } { "_id" : ObjectId("5b30a8e6ec543cee8e9e70e7"), "username" : "imlichao3", "password" : "111111" }
插入变量
正则表达式中使用变量。必定要使用eval将组合的字符串进行转换,不能直接将字符串拼接后传入给表达式。不然没有报错信息,只是结果为空!实例以下:
var name=eval("/" + 变量值key +"/i");
如下是模糊查询包含title关键词, 且不区分大小写:
title:eval("/"+title+"/i") // 等同于 title:{$regex:title,$Option:"$i"}
提取字段:
若不指定 projection,则默认返回全部键,指定 projection 格式以下,有两种模式
db.collection.find(query, {title: 1, by: 1}) // inclusion模式 指定返回的键,不返回其余键 db.collection.find(query, {title: 0, by: 0}) // exclusion模式 指定不返回的键,返回其余键
_id 键默认返回,须要主动指定 _id:0 才会隐藏
两种模式不可混用(由于这样的话没法推断其余键是否应返回)
db.collection.find(query, {title: 1, by: 0}) // 错误
只能全1或全0,除了在inclusion模式时能够指定_id为0
db.collection.find(query, {_id:0, title: 1, by: 1}) // 正确
pretty() 方法以格式化的方式来显示全部文档。
> db.myuser.find().pretty() { "_id" : ObjectId("5b30a8e1ec543cee8e9e70e6"), "username" : "imlichao2", "password" : "111111" } { "_id" : ObjectId("5b30a8e6ec543cee8e9e70e7"), "username" : "imlichao3", "password" : "111111" } { "_id" : ObjectId("5b3190330d9d1b3044553c1a"), "username" : "miaomiao", "password" : "888888" }
> db.myuser.find().limit(2) { "_id" : ObjectId("5b30a8e1ec543cee8e9e70e6"), "username" : "imlichao2", "password" : "111111" } { "_id" : ObjectId("5b30a8e6ec543cee8e9e70e7"), "username" : "imlichao3", "password" : "111111" }
> db.myuser.find().skip(2) { "_id" : ObjectId("5b3190330d9d1b3044553c1a"), "username" : "miaomiao", "password" : "888888" }
正序使用1,倒序使用-1
> db.myuser.find().sort({"_id":1}) { "_id" : ObjectId("5b30a8e1ec543cee8e9e70e6"), "username" : "imlichao2", "password" : "111111" } { "_id" : ObjectId("5b30a8e6ec543cee8e9e70e7"), "username" : "imlichao3", "password" : "111111" } { "_id" : ObjectId("5b3190330d9d1b3044553c1a"), "username" : "miaomiao", "password" : "888888" } > db.myuser.find().sort({"_id":-1}) { "_id" : ObjectId("5b3190330d9d1b3044553c1a"), "username" : "miaomiao", "password" : "888888" } { "_id" : ObjectId("5b30a8e6ec543cee8e9e70e7"), "username" : "imlichao3", "password" : "111111" } { "_id" : ObjectId("5b30a8e1ec543cee8e9e70e6"), "username" : "imlichao2", "password" : "111111" }
经过在字段上建立索引能够提升数据库查询和排序效率。
> db.test1.createIndex({"username":1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
建立正序索引使用1,倒序索引使用-1。排序时规则跟索引一致,此索引才会生效。例如索引为1正序索引,只有在正序排序时才有效。
咱们可使用多个字段建立联合索引
> db.test1.createIndex({"username":1,"password":-1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 3, "numIndexesAfter" : 4, "ok" : 1 }
> db.test1.getIndexes() [ { "v" : 2, "key" : { "username" : 1 }, "name" : "username_1", "ns" : "pmall.test1" }, { "v" : 2, "key" : { "username" : 1, "password" : -1 }, "name" : "username_1_password_-1", "ns" : "pmall.test1" } ]
> db.test1.dropIndex("username_-1") { "nIndexesWas" : 4, "ok" : 1 }