你的位置:首页 > 数据库类
db.listCommands()
来看当前MongoDB支持的所有命令
同样可通过运行命令db.runCommand({"listCommands" :1})来查询所有命令db.loadServerScripts()
加载在system.js集合中的额所有脚本
db.logout()
//退出
db.printCollectionStats()
//显示文档信息
zzzz
{
"ns" : "zxq.zzzz",
"count" : 8044831,
"size" : 450510504,
"avgObjSize" : 55.99999602229059,
"storageSize" : 582864896,
"numExtents" : 18,
"nindexes" : 1,
"lastExtentSize" : 153874432,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 0,
"totalIndexSize" : 261157792,
"indexSizes" : {
"_id_" : 261157792
},
"ok" : 1
}db.printReplicationInfo()
db.printShardingStatus()
db.printSlaveReplicationInfo()
显示复制相关信息
db.removeUser(username)
//删除用户
> db.system.users.find()
{ "_id" : ObjectId("57a4f607da8f119aa039cf35"),
"user" : "zxq", "readOnly" : false, "pwd" : "3e7c9de5121b9c6b4c6c289b9c61c8f3" }
{ "_id" : ObjectId("57a4f81edb4b33684ccac174"),
"user" : "readonly", "readOnly" : true, "pwd" : "853f224e6274e549814cd58e9248293e" }
> db.removeUser("readonly")
> db.system.users.find()
{ "_id" : ObjectId("57a4f607da8f119aa039cf35"),
"user" : "zxq", "readOnly" : false, "pwd" : "3e7c9de5121b9c6b4c6c289b9c61c8f3" }db.repairDatabase()
//dbrepairDatabase,不仅能整理碎片还可以回收磁盘空间
> db.repairDatabase()
> db.runCommand({repairDatabase :1})
> 期间会产生锁,建议关闭应用后再进行此操作
> 所需要的磁盘剩余空间需求很大
> 最后一点,这命令能不用最好别用db.resetError()
清除错误记录
db.runCommand(cmdObj)
> db.runCommand({repairDatabase :1})db.serverStatus()
//服务器信息
//修改服务器最大连接数
> db.serverStatus().connectionsdb
{ "current" : 1, "available" : 818, "totalCreated" : NumberLong(6) }
修改系统参数:
vi /etc/rc.local
ulimit -n 30000
vi /etc/profile
ulimit -n 30000
[root@primary ~]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 22892
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 30000 //已经生效
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 22892
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[root@primary ~]#
配置文件
bpath=/mongodb/db/
logpath=/mongodb/logs/mongod.log
port=27017
logappend=true
fork = true
directoryperdb=true
#auth=true
maxConns=30000
或
[root@primary bin]# ./mongod --dbpath=./db --maxConns=20000
查看服务器最大连接数
> db.serverStatus().connections;
{ "current" : 2, "available" : 19998, "totalCreated" : NumberLong(3) }
>db.shutdownServer()
//关闭服务 > use admin switched to db admin > db.shutdownServer() Sat Aug 6 18:53:47.539 DBClientCursor::init call() failed server should be down... Sat Aug 6 18:53:47.548 trying reconnect to 127.0.0.1:27017 Sat Aug 6 18:53:47.548 reconnect 127.0.0.1:27017 failed couldn't connect to server 127.0.0.1:27017
db.stats()
//显示当前DB信息
> db.stats()
{
"db" : "test",
"collections" : 4,
"objects" : 101,
"avgObjSize" : 54.53465346534654,
"dataSize" : 5508,
"storageSize" : 32768,
"numExtents" : 5,
"indexes" : 2,
"indexSize" : 16352,
"fileSize" : 201326592,
"nsSizeMB" : 16,
"dataFileVersion" : {
"major" : 4,
"minor" : 5
},
"ok" : 1
}db.version()
//显示当前版本 > db.version() 2.4.9 >
六、增、删、改、查
隐式文档创建(文档如果存在,则插入键值,如果不存在,创建文档并插入键值
> db.zxq.insert({id:1,name:"zxq"})
//查询文档内容
> db.zxq.find()
{ "_id" : ObjectId("57c1c2818ee7bef4894d54b0"), "id" : 1, "name" : "zxq" }
//求文档条数
> db.zxq.count()
1
>
//3.2 新版功能
db.zxq.insertOne(
{
name: "sue",
age: 19,
status: "P"
}
)
//插入多条数据
db.zxq.insertMany(
[
{ name: "bob", age: 42, status: "A", },
{ name: "ahn", age: 22, status: "A", },
{ name: "xi", age: 34, status: "D", }
]
)
//例子
> db.zxq.find()
> db.zxq.insertMany(
... [
... { name: "bob", age: 42, status: "A", },
... { name: "ahn", age: 22, status: "A", },
... { name: "xi", age: 34, status: "D", }
... ]
... )
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("57c2b637b854b5b8198eebd3"),
ObjectId("57c2b637b854b5b8198eebd4"),
ObjectId("57c2b637b854b5b8198eebd5")
]
}
> db.zxq.find()
{ "_id" : ObjectId("57c2b637b854b5b8198eebd3"), "name" : "bob", "age" : 42, "status" : "A" }
{ "_id" : ObjectId("57c2b637b854b5b8198eebd4"), "name" : "ahn", "age" : 22, "status" : "A" }
{ "_id" : ObjectId("57c2b637b854b5b8198eebd5"), "name" : "xi", "age" : 34, "status" : "D" }
//多个条件查询
> db.zxq.find({age:{$in:[42,22,34]}})
{ "_id" : ObjectId("57c2b637b854b5b8198eebd3"), "name" : "bob", "age" : 42, "status" : "A" }
{ "_id" : ObjectId("57c2b637b854b5b8198eebd4"), "name" : "ahn", "age" : 22, "status" : "A" }
{ "_id" : ObjectId("57c2b637b854b5b8198eebd5"), "name" : "xi", "age" : 34, "status" : "D" }
{ "_id" : ObjectId("57c2b7eeb854b5b8198eebd6"), "name" : "bob", "age" : 42, "status" : "A" }
{ "_id" : ObjectId("57c2b7eeb854b5b8198eebd7"), "name" : "ahn", "age" : 22, "status" : "A" }
{ "_id" : ObjectId("57c2b7eeb854b5b8198eebd8"), "name" : "xi", "age" : 34, "status" : "D" }
>
> db.zxq.find({_id:{$in:[1,2,3]}})
{ "_id" : 1, "highScore" : 800, "lowScore" : 150 }
{ "_id" : 2, "zxq" : 100 }
{ "_id" : 3, "zxq" : 200查询记录
//查询所有的内容
> db.users.find( {} )
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7b"), "name" : "bob", "age" : 42, "status" : "A" }
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7c"), "name" : "ahn", "age" : 22, "status" : "A" }
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7d"), "name" : "xi", "age" : 34, "status" : "D" }
> db.users.find()
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7b"), "name" : "bob", "age" : 42, "status" : "A" }
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7c"), "name" : "ahn", "age" : 22, "status" : "A" }
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7d"), "name" : "xi", "age" : 34, "status" : "D" }
//单项查询
> db.users.find({age:42})
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7b"), "name" : "bob", "age" : 42, "status" : "A" }
//多条件查询
> db.users.find({age:{$in:[42,22]}})
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7b"), "name" : "bob", "age" : 42, "status" : "A" }
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7c"), "name" : "ahn", "age" : 22, "status" : "A" }
>$lt 小于
> db.users.find({age:{$lt:30}})
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7c"), "name" : "ahn", "age" : 22, "status" : "A" }
>$or 或条件
> db.users.find({$or:[{name:"bob" },{age:{$lt:30}}]})
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7b"), "name" : "bob", "age" : 42, "status" : "A" }
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7c"), "name" : "ahn", "age" : 22, "status" : "A" }
>与条件
> db.users.find({name:"ahn","age" : 22})
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7c"), "name" : "ahn", "age" : 22, "status" : "A" }
> db.users.find({name:"ahn",$or:[{age:{$lt:30}},{status:"A"}]})
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7c"), "name" : "ahn", "age" : 22, "status" : "A" }
>$gt 大于
> db.users.find({age:{$gt:15,$lt:35}})
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7c"), "name" : "ahn", "age" : 22, "status" : "A" }
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7d"), "name" : "xi", "age" : 34, "status" : "D" }查询值为Null或不存在的字段
db.users.insert(
[
{ "_id" : 900, "name" : null },
{ "_id" : 901 }
]
)
> db.users.find()
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7b"), "name" : "bob", "age" : 42, "status" : "A" }
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7c"), "name" : "ahn", "age" : 22, "status" : "A" }
{ "_id" : ObjectId("57c2ba213b5e3ba4c68d0d7d"), "name" : "xi", "age" : 34, "status" : "D" }
{ "_id" : 900, "name" : null }
{ "_id" : 901 }
>
> db.users.find( { name: null } )
{ "_id" : 900, "name" : null }
{ "_id" : 901 }
>类型筛查(包含值是 null 的 name 字段的文档,亦即条目字段的值是BSON类型中的Null(即 10 ):
> db.users.find( { name : { $type: 10 } } )
{ "_id" : 900, "name" : null }存在性筛查
> db.users.find({name:{$exists:false}})
{ "_id" : 901 }$where
> db.zxq.find()
{ "_id" : ObjectId("57c2ce75b854b5b8198eebdb"), "id" : 3, "name" : "c" }
{ "_id" : ObjectId("57c2d006b854b5b8198eebdd"), "id" : 2, "name" : "b" }
{ "_id" : ObjectId("57c2d006b854b5b8198eebde"), "id" : 3, "name" : "c" }
> db.zxq.find({$where:function(){return this.id==3 && this.name=="c"}})
{ "_id" : ObjectId("57c2ce75b854b5b8198eebdb"), "id" : 3, "name" : "c" }
{ "_id" : ObjectId("57c2d006b854b5b8198eebde"), "id" : 3, "name" : "c" }
>
> db.zxq.find({$where:function(){ var i=1; return (this.id/i)==3 }})
{ "_id" : ObjectId("57c2ce75b854b5b8198eebdb"), "id" : 3, "name" : "c" }
{ "_id" : ObjectId("57c2d006b854b5b8198eebde"), "id" : 3, "name" : "c" }
> db.zxq.find({$where:function(){return (this.id/1)==3 }})
{ "_id" : ObjectId("57c2ce75b854b5b8198eebdb"), "id" : 3, "name" : "c" }
{ "_id" : ObjectId("57c2d006b854b5b8198eebde"), "id" : 3, "name" : "c" }
> db.zxq.find({$where:function(){return ( this.id>1&& this.id<3);}})
{ "_id" : ObjectId("57c2d006b854b5b8198eebdd"), "id" : 2, "name" : "b" }
>含有ltlte gtgte $eq
< <= > >= =
> db.zzz.find();
{ "_id" : ObjectId("579ea974a40cd03459b6ac27"), "id" : 1, "name" : "a" }
{ "_id" : ObjectId("579eacf9d3b6760862b1b5c5"), "id" : 2, "name" : "b" }
{ "_id" : ObjectId("579ead08d3b6760862b1b5c6"), "id" : 3, "name" : "c" }
{ "_id" : ObjectId("579ead24d3b6760862b1b5c7"), "id" : 3, "name" : "c" }
范围查找案例:
db.zzz.find({id:{$gte:1,$lte:2}})
> db.zzz.find({id:{$gte:1,$lte:2}})
{ "_id" : ObjectId("579ea974a40cd03459b6ac27"), "id" : 1, "name" : "a" }
{ "_id" : ObjectId("579eacf9d3b6760862b1b5c5"), "id" : 2, "name" : "b" }OR查询:
innin $or
> var id={"id":{"$in":[1,2]}}
> db.zzz.find(id)
$in查询
> var id={"id":{"$in":[1,2]}}
> db.zzz.find(id)
{ "_id" : ObjectId("579ea974a40cd03459b6ac27"), "id" : 1, "name" : "a" }
{ "_id" : ObjectId("579eacf9d3b6760862b1b5c5"), "id" : 2, "name" : "b" }
$nin查询
> db.zzz.find({id:{"$nin":[1,2]}})
{ "_id" : ObjectId("579ead08d3b6760862b1b5c6"), "id" : 3, "name" : "c" }
{ "_id" : ObjectId("579ead24d3b6760862b1b5c7"), "id" : 3, "name" : "c" }
$OR查询
> var id={"$or":[{"id":1},{"id":2}]}
> db.zzz.find(id)
> db.zzz.find({"$or":[{"id":1},{"id":2}]})
{ "_id" : ObjectId("579ea974a40cd03459b6ac27"), "id" : 1, "name" : "a" }
{ "_id" : ObjectId("579eacf9d3b6760862b1b5c5"), "id" : 2, "name" : "b" }
>正则表达式:(类似like)
db.zzz.find({“name”:/a/})
> db.zzz.find()db.zzz.find()
{ "_id" : ObjectId("579ea974a40cd03459b6ac27"), "id" : 1, "name" : "a" }
{ "_id" : ObjectId("579eacf9d3b6760862b1b5c5"), "id" : 2, "name" : "b" }
{ "_id" : ObjectId("579ead08d3b6760862b1b5c6"), "id" : 3, "name" : "c" }
{ "_id" : ObjectId("579ead24d3b6760862b1b5c7"), "id" : 3, "name" : "c" }
{ "_id" : ObjectId("579ebb3ee7e548e1d8ec6e22"), "id" : 4, "name" : "aa" }
{ "_id" : ObjectId("579ebb51e7e548e1d8ec6e23"), "id" : 5, "name" : "abc" }
> db.zzz.find({"name":/a/}) --带有a的
{ "_id" : ObjectId("579ea974a40cd03459b6ac27"), "id" : 1, "name" : "a" }
{ "_id" : ObjectId("579ebb3ee7e548e1d8ec6e22"), "id" : 4, "name" : "aa" }
{ "_id" : ObjectId("579ebb51e7e548e1d8ec6e23"), "id" : 5, "name" : "abc" }
> db.zzz.find({"name":/a$/}) ---以a开头的
{ "_id" : ObjectId("579ea974a40cd03459b6ac27"), "id" : 1, "name" : "a" }
{ "_id" : ObjectId("579ebb3ee7e548e1d8ec6e22"), "id" : 4, "name" : "aa" }
>查询数组
> var single={"name":"jack",address:["anhui","shanghai","beijing"]}
> db.mytest.insert(single)
> db.mytest.find()
{ "_id" : ObjectId("567632624163f2ea4dd06e96"), "name" : "jack", "address" : [ "anhui", "shanghai", "beijing" ] }all操作(检索数组中需要包含多个元素,all数组中必须同时包含的条件)
> db.mytest.find({"address":{"$all":["anhui","shanghai"]}})
{ "_id" : ObjectId("567632624163f2ea4dd06e96"), "name" : "jack", "address" : [ "anhui", "shanghai", "beijing" ] }
> db.mytest.find({"address":{"$all":["anhui","s"]}})size操作(可以通过size获取数组的长度,但是$size不能和比较操作符联合使用)
> db.mytest.find({"address":{"$size":2}})
> db.mytest.find({"address":{"$size":3}})
{ "_id" : ObjectId("567632624163f2ea4dd06e96"), "name" : "jack", "address" : [ "anhui", "shanghai", "beijing" ] }slice操作(通过slice返回数组中的部分数据。”$slice”:2表示数组中的前两个元素)
> db.mytest.find({"name":"jack"},{"address":{"$slice":1}})
{ "_id" : ObjectId("567632624163f2ea4dd06e96"), "name" : "jack", "address" : [ "anhui" ] }
> db.mytest.find({"name":"jack"},{"address":{"$slice":2}})
{ "_id" : ObjectId("567632624163f2ea4dd06e96"), "name" : "jack", "address" : [ "anhui", "shanghai" ] }未完待续......
- 上一篇:MongoDB入门学习(一)
- 下一篇:MongoDB入门学习(三)

- 发表评论
- 查看评论
【暂无评论!】发表评论: