下面开始mongodb的实战啦,首先下载软件包,mongodb-linux-i686-2.0.2.tar(推荐使用64位的,实验用32位也可以),用ftp工具上传到linux虚拟主机,解压就可以用了。
进入到bin,有个mongod脚本,./mongod运行,会看到如下信息:
Thu Jan 5 10:16:12 [initandlisten] db version v2.0.2, pdfile version 4.5
Thu Jan 5 10:16:12 [initandlisten] git version: 514b122d308928517f5841888ceaa42 46a7f18e3
Thu Jan 5 10:16:12 [initandlisten] build info: Linux domU-12-31-39-01-70-B4 2.6 .21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_41
Thu Jan 5 10:16:12 [initandlisten] options: {}
Thu Jan 5 10:16:12 [initandlisten] waiting for connections on port 27017
Thu Jan 5 10:16:12 [websvr] admin web console waiting for connections on port 28017
Thu Jan 5 10:17:12 [clientcursormon] mem (MB) res:12 virt:92 mapped:0
Thu Jan 5 10:21:51 [initandlisten] connection accepted from 127.0.0.1:47890 #1
Thu Jan 5 10:22:04 [conn1] command admin.$cmd command: { listDatabases: 1.0 } n toreturn:1 reslen:175 533ms
Thu Jan 5 10:22:12 [clientcursormon] mem (MB) res:28 virt:125 mapped:32
Thu Jan 5 10:27:12 [clientcursormon] mem (MB) res:28 virt:125 mapped:32
Thu Jan 5 10:32:12 [clientcursormon] mem (MB) res:28 virt:125 mapped:32
Thu Jan 5 10:37:12 [clientcursormon] mem (MB) res:28 virt:125 mapped:32
Thu Jan 5 10:37:51 [conn1] end connection 127.0.0.1:47890
Thu Jan 5 10:40:40 [initandlisten] connection accepted from 192.168.1.108:3325 #2
Thu Jan 5 10:40:40 [conn2] end connection 192.168.1.108:3325
Thu Jan 5 10:42:13 [clientcursormon] mem (MB) res:28 virt:125 mapped:32
说明mongoddb服务端已经启动,然后通过端口28017访问管理页面,打开另一个终端,进入到bin目录,./mongo启动mongo客户端,不要忘了建立 /data/db 目录。mongodb默认连接的是test库,接着可以像操作mysql一样操作mongodb啦。
插入一个文档(相当于关系数据库的一行):
- /**
- *db相当于关系数据库的当前选择的库,foos相当于关系数据库的表,没有则自动创建
- */
- db.foos.insert({'bar':'test'})
查找所有文档(相当于关系型数据库的所有行数据)
- /**
- *条件查询参考手册,这里查询所有
- */
- db.foos.find()
更新文档(相当于更新关系数据库的行数据)
事先在foo集合(关系型数据库的表)中插入这么多数据,查找全部,将第4个a的值改为400
db.foo.find()
{ "_id" : ObjectId("4f03bf4e6d561d59c65493cd"), "a" : 1 }
{ "_id" : ObjectId("4f03bf646d561d59c65493ce"), "a" : 2 }
{ "_id" : ObjectId("4f03bf6c6d561d59c65493cf"), "a" : 3 }
{ "_id" : ObjectId("4f03bf796d561d59c65493d0"), "a" : 4 }
{ "_id" : ObjectId("4f03bf7d6d561d59c65493d1"), "a" : 5 }
{ "_id" : ObjectId("4f03bf806d561d59c65493d2"), "a" : 6 }
{ "_id" : ObjectId("4f03bf856d561d59c65493d3"), "a" : 7 }
{ "_id" : ObjectId("4f03bf896d561d59c65493d4"), "a" : 8 }
{ "_id" : ObjectId("4f03bf8c6d561d59c65493d5"), "a" : 9 }
{ "_id" : ObjectId("4f03bf906d561d59c65493d6"), "a" : 10 }
{ "_id" : ObjectId("4f0421757f8b9aa10e000000"), "a" : "php" }
{ "_id" : ObjectId("4f0421787f8b9aa10e000001"), "a" : "php" }
{ "_id" : ObjectId("4f0421797f8b9aa10e000002"), "a" : "php" }
{ "_id" : ObjectId("4f04220f7f8b9aa30e000000"), "a" : "php" }
{ "_id" : ObjectId("4f0422107f8b9aa30e000001"), "a" : "php" }
{ "_id" : ObjectId("4f0422127f8b9aa30e000002"), "a" : "php" }
{ "_id" : ObjectId("4f0422137f8b9aa30e000003"), "a" : "php" }
{ "_id" : ObjectId("4f0422137f8b9aa30e000004"), "a" : "php" }
{ "_id" : ObjectId("4f0422157f8b9aa30e000005"), "a" : "php" }
{ "_id" : ObjectId("4f04222f7f8b9aa40e000000"), "a" : "php" }
/*注意文档的_id必须唯一,否则更新失败*/
data={'a':400}
db.foo.update({'_id':'4f03bf796d561d59c65493d0'},data)
删除数据:
- /**
- *删除foos中的所有数据,但本身的集合被保留,也不会删除原有的索引
- */
- db.foos.remove()
- /*只删除a值等于php的所有文档*/
- db.foos.remove({'a':'php'})
至此mongodb简单的增删改查操作完毕,复杂操作查看官方文档及其它其它一些资料,mongodb权威指南pdf版的不错。
阅读(4167) | 评论(0) | 转发(0) |