mongodb 基础学习

mongodb各个程序功能介绍

bsondump 导出bson结构
mongo 客户端
mongod 服务器
mongodump 整体数据库导出 二进制
mongoexport 导出易识别的json文档或csv文档
mongorestore 数据库整体导入
mongos 路由器(分片时用)

mongodb启动 连接

1
mongod --dbpath=/ptah/to/database --logpath=/path/to/logfile --fork --port 27017

参数解释:

  1. --dbpath数据存储目录
  2. --logpath日志存储文件
  3. --port 允许端口
  4. --fork 后台允许
  5. --smallfiles
1
2
3
4
5
sudo mongod --dbpath=/data/mongodb --logpath=/data/mongodb/mongodb.log --port 27017 --fork

about to fork child process, waiting until server is ready for connections.
forked process: 4237
child process started successfully, parent exiting

mongdb连接,默认连接27017端口

1
2
3
4
5
6
7
8
9
10
11
12
13
[CaseZheng@home mongodb]$ mongo
MongoDB shell version: 3.2.12
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2017-07-12T23:32:29.327+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-07-12T23:32:29.327+0800 I CONTROL [initandlisten]
>

mongodb入门命令

  1. show dbs; show databases; 查看数据库
  2. use databasesname; 选中数据库
  3. show tables; show collection; 查看数据库中的所有表
  4. 创建数据库 隐式创建 use databasesname 如果数据库不存在,则创建
  5. db.createCollection(‘tablename’) 创建表
  6. db.tablename.drop() 删除表
  7. db.dropDatabase() 删除数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
>show databases
local 0.000GB
> use local
switched to db local
> show tables
startup_log
> use test
switched to db test
> db.createCollection('stu')
{ "ok" : 1 }
> show tables
stu
> db.stu.drop()
true
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
>

mongodb增

db.tablename.insert(jsonvalue);
一次可插入多个数据,使用json数组即可。

1
2
3
4
5
6
7
> db.test.insert({"id":2,"name":"casezheng"});
WriteResult({ "nInserted" : 1 })
> show tables;
test
> db.test.find()
{ "_id" : ObjectId("598090b7817bfe1400a371be"), "id" : 2, "name" : "casezheng" }
>

mongodb删

db.tablename.remove(expression);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
> db.test.find()
{ "_id" : ObjectId("598090b7817bfe1400a371be"), "id" : 2, "name" : "casezheng" }
>
> db.test.insert({"id":3,"name":"casezheng"});
WriteResult({ "nInserted" : 1 })
> db.test.insert({"id":4,"name":"casezheng"});
WriteResult({ "nInserted" : 1 })
> db.test.insert({"id":5,"name":"casezheng"});
WriteResult({ "nInserted" : 1 })
> db.test.insert({"id":6,"name":"casezheng"});
WriteResult({ "nInserted" : 1 })
> db.test.remove({"id":2})
WriteResult({ "nRemoved" : 1 })
> db.test.find()
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "id" : 3, "name" : "casezheng" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezheng" }
{ "_id" : ObjectId("598091ee817bfe1400a371c1"), "id" : 5, "name" : "casezheng" }
{ "_id" : ObjectId("598091f2817bfe1400a371c2"), "id" : 6, "name" : "casezheng" }
> db.test.remove({"id":6})
WriteResult({ "nRemoved" : 1 })
> db.test.find()
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "id" : 3, "name" : "casezheng" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezheng" }
{ "_id" : ObjectId("598091ee817bfe1400a371c1"), "id" : 5, "name" : "casezheng" }
>

db.tablename.remove(expression, true);不论匹配多少行只删掉一行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
> db.test.find()
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "id" : 3, "name" : "casezheng" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezheng" }
{ "_id" : ObjectId("598091ee817bfe1400a371c1"), "id" : 5, "name" : "casezheng" }
> db.test.insert({"id":5,"name":"casezheng2"});
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "id" : 3, "name" : "casezheng" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezheng" }
{ "_id" : ObjectId("598091ee817bfe1400a371c1"), "id" : 5, "name" : "casezheng" }
{ "_id" : ObjectId("598092ac817bfe1400a371c3"), "id" : 5, "name" : "casezheng2" }
> db.test.remove({"id":5}, true);
WriteResult({ "nRemoved" : 1 })
> db.test.find()
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "id" : 3, "name" : "casezheng" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezheng" }
{ "_id" : ObjectId("598092ac817bfe1400a371c3"), "id" : 5, "name" : "casezheng2" }
> db.test.insert({"id":5,"name":"casezheng3"});
WriteResult({ "nInserted" : 1 })
> db.test.remove({"id":5});
WriteResult({ "nRemoved" : 2 })
> db.test.find()
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "id" : 3, "name" : "casezheng" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezheng" }
>

mongodb改

db.tablename.update(expression, jsonvalue);将符合expression的行改为jsonvalue

1
2
3
4
5
6
7
8
 db.test.find()
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "id" : 3, "name" : "casezheng" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezheng" }
> db.test.update({"id":3}, {"name":"casezhenghaha"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.find()
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "name" : "casezhenghaha" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezheng" }

可使用谓词set设置其中某个列的值

1
2
3
4
5
6
7
8
9
10
> db.test.find()
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "name" : "casezhenghaha" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezheng" }
{ "_id" : ObjectId("59809440817bfe1400a371c5"), "id" : 5, "name" : "casezheng" }
> db.test.update({"id":4}, {$set:{"name":"casezhenghehehe"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.find()
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "name" : "casezhenghaha" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezhenghehehe" }
{ "_id" : ObjectId("59809440817bfe1400a371c5"), "id" : 5, "name" : "casezheng" }

unset 删除某个列
rename 重命名某个列
inc 增长某个列

mongodb查

db.tablename.find(expression);
db.tablename.findOne(expression);
查询所有

1
2
3
4
5
6
7
8
9
> db.test.find()
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "name" : "casezhenghaha" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezhenghehehe" }
{ "_id" : ObjectId("59809440817bfe1400a371c5"), "id" : 5, "name" : "casezheng" }
{ "_id" : ObjectId("598095c0817bfe1400a371c6"), "id" : 5, "name" : "casezheng4" }
{ "_id" : ObjectId("598095c3817bfe1400a371c7"), "id" : 5, "name" : "casezheng45" }
{ "_id" : ObjectId("598095c7817bfe1400a371c8"), "id" : 3, "name" : "casezheng45" }
{ "_id" : ObjectId("598095ca817bfe1400a371c9"), "id" : 2, "name" : "casezheng45" }
{ "_id" : ObjectId("598095cd817bfe1400a371ca"), "id" : 1, "name" : "casezheng45" }

查询单独的某列

1
2
> db.test.find({"id":4}, {"name":1})
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "name" : "casezhenghehehe" }

findOne只查询一行

1
2
3
4
5
6
7
8
9
10
> db.test.find({"id":5})
{ "_id" : ObjectId("59809440817bfe1400a371c5"), "id" : 5, "name" : "casezheng" }
{ "_id" : ObjectId("598095c0817bfe1400a371c6"), "id" : 5, "name" : "casezheng4" }
{ "_id" : ObjectId("598095c3817bfe1400a371c7"), "id" : 5, "name" : "casezheng45" }
> db.test.findOne({"id":5})
{
"_id" : ObjectId("59809440817bfe1400a371c5"),
"id" : 5,
"name" : "casezheng"
}

mongodb查询表达式

1
2
3
4
5
6
7
8
9
10
> db.test.find()
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "name" : "casezhenghaha" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezhenghehehe" }
{ "_id" : ObjectId("59809440817bfe1400a371c5"), "id" : 5, "name" : "casezheng" }
{ "_id" : ObjectId("598095c0817bfe1400a371c6"), "id" : 5, "name" : "casezheng4" }
{ "_id" : ObjectId("598095c3817bfe1400a371c7"), "id" : 5, "name" : "casezheng45" }
{ "_id" : ObjectId("598095c7817bfe1400a371c8"), "id" : 3, "name" : "casezheng45" }
{ "_id" : ObjectId("598095ca817bfe1400a371c9"), "id" : 2, "name" : "casezheng45" }
{ "_id" : ObjectId("598095cd817bfe1400a371ca"), "id" : 1, "name" : "casezheng45" }
>

不等于ne

1
2
3
4
5
6
> db.test.find({"id":{$ne:5}})
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "name" : "casezhenghaha" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezhenghehehe" }
{ "_id" : ObjectId("598095c7817bfe1400a371c8"), "id" : 3, "name" : "casezheng45" }
{ "_id" : ObjectId("598095ca817bfe1400a371c9"), "id" : 2, "name" : "casezheng45" }
{ "_id" : ObjectId("598095cd817bfe1400a371ca"), "id" : 1, "name" : "casezheng45" }

in nin

1
2
3
4
5
6
7
8
9
10
> db.test.find({"id":{$in:[4, 5, 3]}})
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezhenghehehe" }
{ "_id" : ObjectId("59809440817bfe1400a371c5"), "id" : 5, "name" : "casezheng" }
{ "_id" : ObjectId("598095c0817bfe1400a371c6"), "id" : 5, "name" : "casezheng4" }
{ "_id" : ObjectId("598095c3817bfe1400a371c7"), "id" : 5, "name" : "casezheng45" }
{ "_id" : ObjectId("598095c7817bfe1400a371c8"), "id" : 3, "name" : "casezheng45" }
> db.test.find({"id":{$nin:[4, 5, 3]}})
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "name" : "casezhenghaha" }
{ "_id" : ObjectId("598095ca817bfe1400a371c9"), "id" : 2, "name" : "casezheng45" }
{ "_id" : ObjectId("598095cd817bfe1400a371ca"), "id" : 1, "name" : "casezheng45" }

all
取出field列是数组,且至少包含v1,v2值

1
{fiels:{$all:[v1, v2]}}

exi存在某个属性

1
2
3
4
5
6
7
8
> db.test.find({"id":{$exists:1}})
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezhenghehehe" }
{ "_id" : ObjectId("59809440817bfe1400a371c5"), "id" : 5, "name" : "casezheng" }
{ "_id" : ObjectId("598095c0817bfe1400a371c6"), "id" : 5, "name" : "casezheng4" }
{ "_id" : ObjectId("598095c3817bfe1400a371c7"), "id" : 5, "name" : "casezheng45" }
{ "_id" : ObjectId("598095c7817bfe1400a371c8"), "id" : 3, "name" : "casezheng45" }
{ "_id" : ObjectId("598095ca817bfe1400a371c9"), "id" : 2, "name" : "casezheng45" }
{ "_id" : ObjectId("598095cd817bfe1400a371ca"), "id" : 1, "name" : "casezheng45" }

与,写在一个json里面即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
> db.test.find()
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "name" : "casezhenghaha" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezhenghehehe" }
{ "_id" : ObjectId("59809440817bfe1400a371c5"), "id" : 5, "name" : "casezheng" }
{ "_id" : ObjectId("598095c0817bfe1400a371c6"), "id" : 5, "name" : "casezheng4" }
{ "_id" : ObjectId("598095c3817bfe1400a371c7"), "id" : 5, "name" : "casezheng45" }
{ "_id" : ObjectId("598095c7817bfe1400a371c8"), "id" : 3, "name" : "casezheng45" }
{ "_id" : ObjectId("598095ca817bfe1400a371c9"), "id" : 2, "name" : "casezheng45" }
{ "_id" : ObjectId("598095cd817bfe1400a371ca"), "id" : 1, "name" : "casezheng45" }
{ "_id" : ObjectId("59809b69817bfe1400a371cb"), "id" : 10 }
> db.test.find({"id":{$ne:5}, "name":{$exists:1}})
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "name" : "casezhenghaha" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezhenghehehe" }
{ "_id" : ObjectId("598095c7817bfe1400a371c8"), "id" : 3, "name" : "casezheng45" }
{ "_id" : ObjectId("598095ca817bfe1400a371c9"), "id" : 2, "name" : "casezheng45" }
{ "_id" : ObjectId("598095cd817bfe1400a371ca"), "id" : 1, "name" : "casezheng45" }
>

nor 所有条件都不满足的文档为真返回

1
2
3
4
5
6
7
8
> db.test.find({$nor:[{"id":5},{"name":"casezheng"}]});
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "name" : "casezhenghaha" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezhenghehehe" }
{ "_id" : ObjectId("598095c7817bfe1400a371c8"), "id" : 3, "name" : "casezheng45" }
{ "_id" : ObjectId("598095ca817bfe1400a371c9"), "id" : 2, "name" : "casezheng45" }
{ "_id" : ObjectId("598095cd817bfe1400a371ca"), "id" : 1, "name" : "casezheng45" }
{ "_id" : ObjectId("59809b69817bfe1400a371cb"), "id" : 10 }
>

where速度慢

mongodb游标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
> var cursor = db.test.find()
> while(cursor.hasNext()){
... printjsononeline(cursor.next())
... }
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "name" : "casezhenghaha" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezhenghehehe" }
{ "_id" : ObjectId("59809440817bfe1400a371c5"), "id" : 5, "name" : "casezheng" }
{ "_id" : ObjectId("598095c0817bfe1400a371c6"), "id" : 5, "name" : "casezheng4" }
{ "_id" : ObjectId("598095c3817bfe1400a371c7"), "id" : 5, "name" : "casezheng45" }
{ "_id" : ObjectId("598095c7817bfe1400a371c8"), "id" : 3, "name" : "casezheng45" }
{ "_id" : ObjectId("598095ca817bfe1400a371c9"), "id" : 2, "name" : "casezheng45" }
{ "_id" : ObjectId("598095cd817bfe1400a371ca"), "id" : 1, "name" : "casezheng45" }
{ "_id" : ObjectId("59809b69817bfe1400a371cb"), "id" : 10 }
> var cursor = db.test.find()
> cursor.forEach(function(obj){printjsononeline(obj)});
{ "_id" : ObjectId("598091e7817bfe1400a371bf"), "name" : "casezhenghaha" }
{ "_id" : ObjectId("598091eb817bfe1400a371c0"), "id" : 4, "name" : "casezhenghehehe" }
{ "_id" : ObjectId("59809440817bfe1400a371c5"), "id" : 5, "name" : "casezheng" }
{ "_id" : ObjectId("598095c0817bfe1400a371c6"), "id" : 5, "name" : "casezheng4" }
{ "_id" : ObjectId("598095c3817bfe1400a371c7"), "id" : 5, "name" : "casezheng45" }
{ "_id" : ObjectId("598095c7817bfe1400a371c8"), "id" : 3, "name" : "casezheng45" }
{ "_id" : ObjectId("598095ca817bfe1400a371c9"), "id" : 2, "name" : "casezheng45" }
{ "_id" : ObjectId("598095cd817bfe1400a371ca"), "id" : 1, "name" : "casezheng45" }
{ "_id" : ObjectId("59809b69817bfe1400a371cb"), "id" : 10 }

mongodb分组

maoReduce