MongoDB 是一个高性能,开源,无模式的文档型数据库,采用C++开发,是当前NoSQL数据库中比较热门的一种,在许多场景下可用于替代传统的关系型数据库或键/值存储方式
MongoDB使用BSON作为数据存储和传输的格式,BSON是一种类似JSON的二进制序列化文档,支持嵌套对象和数组。MongoDB很像MySQL,document对应MySQL的row,collection对应MySQL的table
MongoDB服务端可运行在Linux、Windows或OS X平台,支持32位和64位应用,默认端口为27017。推荐运行在64位平台,因为MongoDB在32位模式运行时支持的最大文件尺寸为2GB
1,特点和功能
1) 特点
2) 功能
2, 适用场合
mongoDB 下载, 最新版本mongodb-linux-x86_64-2.2.3.tgz
mongoDB 安装:
tar zxvfmongodb-linux-x86_64-2.2.3.tgz
cdmongodb-linux-x86_64-2.2.3
sudo mkdir /opt/mongodb-2.2.3
sudo ./bin/mongod --dbpath=/opt/mongodb-2.2.3/
安装完毕后,在浏览器URL栏输入:http://localhost:27017/,出现下面内容则表示安装成功!
4, 启动关闭
1) 默认启动
sudo ./bin/mongod (默认保存文件目录为/data/db/, 没有则新建 sudo mkdir -p/data/db/, 默认端口为27017)
2)指定目录启动
sudo ./bin/mongod --dbpath=/opt/mongodb-2.2.3/ (保存文件目录为/opt/mongodb-2.2.3/ , 没有则新建)
3) 指定端口启动
sudo ./bin/mongod --dbpath=/opt/mongodb-2.2.3/ -p 27000 (端口修改为指定27000),启动输出如下:
MongoDB starting : pid=4222 port=27000 dbpath=/opt/mongodb-2.2.3/ 32-bit host=ubuntu
4) 后台启动命令
sudo ./bin/mongod -shardsvr -replSet shard1 -port=27017 -dbpath=/opt/mongodb-2.2.3/ -logpath=/opt/mongodb-2.2.3/log -logappend -rest -fork
5) 后台关闭命令
或者
> db.adminCommand({shutdown : 1, force : true})
> //or
> db.shutdownServer({force : true})
> db.adminCommand(shutdown : 1, force : true, timeoutsec : 5) // 超时关闭
> //or
> db.shutdownServer({force : true, timeoutsec : 5})
6) 客户端启动
默认启动:./bin/mongo
指定端口启动:./bin/mongo --port 27000
7) 备份与恢复
$ sudo ./bin/mongodump -d foo -o /opt/mongodb-2.2.3/bk/ // 备份到bk目录 connected to: 127.0.0.1 Tue Feb 5 20:58:13 DATABASE: foo to /opt/mongodb-2.2.3/bk/foo Tue Feb 5 20:58:13 foo.foo_test to /opt/mongodb-2.2.3/bk/foo/foo_test.bson Tue Feb 5 20:58:13 1 objects Tue Feb 5 20:58:13 Metadata for foo.foo_test to /opt/mongodb-2.2.3/bk/foo/foo_test.metadata.json $ ./bin/mongorestore -d foo -c foo_test /opt/mongodb-2.2.3/bk/foo/foo_test.bson // 恢复foo_test集合 connected to: 127.0.0.1 Tue Feb 5 20:58:20 /opt/mongodb-2.2.3/bk/foo/foo_test.bson Tue Feb 5 20:58:20 going into namespace [foo.foo_test] Tue Feb 5 20:58:20 warning: Restoring to foo.foo_test without dropping. Restored data will be inserted without raising errors; check your server log Tue Feb 5 20:58:20 WARNING: collection foo.foo_test exists with different options than are in the metadata.json file and not using --drop. Options in the metadata file will be ignored. 1 objects found Tue Feb 5 20:58:20 Creating index: { key: { _id: 1 }, ns: "foo.foo_test", name: "_id_" }
5, 命令示例
1) 启动服务端
sudo ./bin/mongod --dbpath=/opt/mongodb-2.2.3/
2) 启动客户端
./bin/mongo
3) 客户端简单命令
帮助: help
显示数据库:show dbs;
选择数据库:use test;
插入数据:db.foo.save({homer:2});
查询:db.foo.find();
结果:{ "_id" : ObjectId("511078ad857b699e9eaaf516"), "homer" : 2 }
更新:db.foo.update({"homer":2},{"homer":3}); // ( {}, {} ) 第一个{}是查询条件,第二个{}是重新赋值
结果:{ "_id" : ObjectId("511078ad857b699e9eaaf516"), "homer" : 3 }
删除:db.foo.remove({"homer":3});
复合插入:
> t={"homer":2, "name" : {"name1" : "yang", "name2" : "gang"} }
{ "homer" : 2, "name" : { "name1" : "yang", "name2" : "gang" } }
> db.foo.insert(t);
> db.foo.find();
{ "_id" : ObjectId("5110d3e0af69ad089b234001"), "homer" : 2, "name" : { "name1" : "yang", "name2" : "gang" } }
复合查询:
> db.foo.find(); //先查看集合中所有记录
{ "_id" : ObjectId("5110ca1aaf69ad089b233fff"), "homer" : 222 }
{ "_id" : ObjectId("5110caffaf69ad089b234000"), "homer" : 222 }
{ "_id" : ObjectId("5110d3e0af69ad089b234001"), "homer" : 2, "name" : { "name1" : "yang", "name2" : "gang" } }
>
> db.foo.find({"homer":2}); // 查询包含特定内容的记录
{ "_id" : ObjectId("5110d3e0af69ad089b234001"), "homer" : 2, "name" : { "name1" : "yang", "name2" : "gang" } }
MongoDB创建完整示例:
> use test2 // test2数据库不存在也可以执行,但不会立刻创建,需要执行insert操作时才创建 switched to db test2 > show dbs // 没有test2,表明不是立刻创建 local (empty) test 0.0625GB > > db // 显示当前正在使用的数据库,test2不存在但正在使用中 test2 > db.createCollection("t2_test") // 创建集合 { "ok" : 1 } > t={"name": "yanggang", "sex" : "male", "lover" : { "lover1" : "little dog", "lover2" : "fish" }} // 初始化集合数据 { "name" : "yanggang", "sex" : "male", "lover" : { "lover1" : "little dog", "lover2" : "fish" } } > db.t2_test.insert(t) // 插入数据,此刻才会创建test2数据库 > db.t2_test.find() // 查询 { "_id" : ObjectId("5110d685af69ad089b234002"), "name" : "yanggang", "sex" : "male", "lover" : { "lover1" : "little dog", "lover2" : "fish" } } > show dbs // 查看数据库,多了test2 local (empty) test 0.0625GB test2 0.0625GB
MongoDB删除示例:
> show collections // 显示集合 system.indexes t2_test > db.t2_test.drop() // 删除集合t2_test true > show collections // 验证删除成功 system.indexes > db.test2.remove() > show dbs // 显示数据库 local (empty) test 0.0625GB test2 0.0625GB > db // 显示当前数据库 test2 > db.dropDatabase() // 删除数据库 { "dropped" : "test2", "ok" : 1 } > show dbs // 验证删除成功 local (empty) test 0.0625GB
MongoDB 索引
> db.foo.find() { "_id" : ObjectId("5110ca1aaf69ad089b233fff"), "homer" : 222 } { "_id" : ObjectId("5110caffaf69ad089b234000"), "homer" : 222 } { "_id" : ObjectId("5110d3e0af69ad089b234001"), "homer" : 2, "name" : { "name1" : "yang", "name2" : "gang" } } > db.foo.getIndexes() // 查看索引(_id为默认,不可删除和修改) [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.foo", "name" : "_id_" } ] > db.foo.ensureIndex({"homer":1}) // 新建索引 > db.foo.getIndexes() // 验证新建索引 [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.foo", "name" : "_id_" }, { "v" : 1, "key" : { "homer" : 1 }, "ns" : "test.foo", "name" : "homer_1" } ] > db.foo.dropIndex({"homer":1}) // 删除索引 { "nIndexesWas" : 2, "ok" : 1 } > db.foo.getIndexes() // 验证删除索引 [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.foo", "name" : "_id_" } ]
6, 应用示例
> db.foo_test.insert({"homer":1}) > for(var i = 1; i<=5; i++) db.foo_test.save({"homer":2, "score":i}) // 数组生成 > db.foo_test.find() { "_id" : ObjectId("5111016cc0e386db7d954ae7"), "homer" : 1 } { "_id" : ObjectId("5111030f0e2d3a50c56b49a9"), "homer" : 2, "score" : 6 } { "_id" : ObjectId("511103200e2d3a50c56b49aa"), "homer" : 2, "score" : 1 } { "_id" : ObjectId("511103200e2d3a50c56b49ab"), "homer" : 2, "score" : 2 } { "_id" : ObjectId("511103200e2d3a50c56b49ac"), "homer" : 2, "score" : 3 } { "_id" : ObjectId("511103200e2d3a50c56b49ad"), "homer" : 2, "score" : 4 } { "_id" : ObjectId("511103200e2d3a50c56b49ae"), "homer" : 2, "score" : 5 } > > var cursor = db.foo_test.find(); while(cursor.hasNext())printjson(cursor.next()); // 遍历数组 { "_id" : ObjectId("5111016cc0e386db7d954ae7"), "homer" : 1 } { "_id" : ObjectId("5111030f0e2d3a50c56b49a9"), "homer" : 2, "score" : 6 } { "_id" : ObjectId("511103200e2d3a50c56b49aa"), "homer" : 2, "score" : 1 } { "_id" : ObjectId("511103200e2d3a50c56b49ab"), "homer" : 2, "score" : 2 } { "_id" : ObjectId("511103200e2d3a50c56b49ac"), "homer" : 2, "score" : 3 } { "_id" : ObjectId("511103200e2d3a50c56b49ad"), "homer" : 2, "score" : 4 } { "_id" : ObjectId("511103200e2d3a50c56b49ae"), "homer" : 2, "score" : 5 } > > var cursor = db.foo_test.find(); printjson(cursor[4]) // 查询(迭代) { "_id" : ObjectId("511103200e2d3a50c56b49ac"), "homer" : 2, "score" : 3 } > var arr = db.foo_test.find().toArray(); arr[5] // 查询(数组) { "_id" : ObjectId("511103200e2d3a50c56b49ad"), "homer" : 2, "score" : 4 } > db.foo_test.find().limit(3) // 查询(前三) { "_id" : ObjectId("5111016cc0e386db7d954ae7"), "homer" : 1 } { "_id" : ObjectId("5111030f0e2d3a50c56b49a9"), "homer" : 2, "score" : 6 } { "_id" : ObjectId("511103200e2d3a50c56b49aa"), "homer" : 2, "score" : 1 }
参考推荐:
MongoDB (官方)