Linux/Python学习论坛-京峰教育

 找回密码
 立即注册

一键登录:

搜索
热搜: 活动 交友 discuz
查看: 876|回复: 0

mongodb的删, 改, 查

[复制链接]

238

主题

288

帖子

1925

积分

超级版主

Rank: 8Rank: 8

积分
1925
QQ
发表于 2015-3-18 13:09:00 | 显示全部楼层 |阅读模式
所有demo数据
root@ubuntu:~# mongo
MongoDB shell version: 2.4.9
connecting to: test
> show dbs
cc        0.0625GB
local        0.03125GB
> use cc
switched to db cc
> show collections;
info
system.indexes
> db.info.find()
{ "_id" : ObjectId("530d8f621d41c810b8314d2b"), "ip" : "1.214.61.19", "port" : { "80" : { "http header" : { "Content-Length" : "450", "X-Powered-By" : "Servlet/3.0; JBossAS-6", "Set-Cookie" : "JSESSIONID=03258FCBF1CAC75E83773025579056DC; Path=/", "Server" : "Apache-Coyote/1.1", "Connection" : "close", "Date" : "Sun, 16 Feb 2014 15:08:46 GMT", "Content-Type" : "text/html;charset=utf-8" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d2c"), "ip" : "1.214.193.74", "port" : { "80" : { "http header" : { "X-Powered-By" : "Servlet 2.4; JBoss-4.2.3.GA (build: SVNTag=JBoss_4_2_3_GA date=200807181417)/JBossWeb-2.0", "Set-Cookie" : "JSESSIONID=8C52673D006D3D7FFEBB9289661A482E; Path=/", "Expires" : "Thu, 01 Jan 1970 00:00:00 GMT", "Server" : "Apache-Coyote/1.1", "Connection" : "close", "Pragma" : "no-cache", "Cache-Control" : "no-cache", "Date" : "Mon, 17 Feb 2014 12:14:30 GMT", "Content-Type" : "text/html;charset=UTF-8" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d2d"), "ip" : "4.30.102.81", "port" : { "80" : { "http header" : { "Content-Length" : "230", "X-Powered-By" : "Servlet 2.4; JBoss-4.0.4.GA (build: CVSTag=JBoss_4_0_4_GA date=200605151000)/Tomcat-5.5", "Server" : "Apache-Coyote/1.1", "Last-Modified" : "Thu, 27 Sep 2007 19:54:45 GMT", "Connection" : "close", "ETag" : "W/\"230-1190922885000\"", "Date" : "Sun, 16 Feb 2014 15:11:18 GMT", "Content-Type" : "text/html" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d2e"), "ip" : "2.139.237.42", "port" : { "80" : { "http header" : { "Content-Length" : "206", "X-Powered-By" : "Servlet 2.4; JBoss-4.0.4.GA (build: CVSTag=JBoss_4_0_4_GA date=200605151000)/Tomcat-5.5", "Server" : "Apache-Coyote/1.1", "Last-Modified" : "Mon, 17 Oct 2011 19:09:18 GMT", "Connection" : "close", "ETag" : "W/\"206-1318878558937\"", "Date" : "Sun, 16 Feb 2014 15:08:21 GMT", "Content-Type" : "text/html" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d2f"), "ip" : "2.33.211.149", "port" : { "80" : { "http header" : { "Connection" : "close", "Cache-Control" : "max-age=0, must-revalidate", "Content-type" : "text/html", "Pragma" : "no-cache", "Server" : "Apache" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d30"), "ip" : "12.176.37.246", "port" : { "80" : { "http header" : { "Content-Length" : "1748", "X-Powered-By" : "Servlet 2.5; JBoss-5.0/JBossWeb-2.1", "Accept-Ranges" : "bytes", "Server" : "Apache-Coyote/1.1", "Last-Modified" : "Fri, 22 May 2009 18:02:46 GMT", "Connection" : "close", "ETag" : "W/\"1748-1243015366000\"", "Date" : "Sun, 16 Feb 2014 15:11:22 GMT", "Content-Type" : "text/html" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d31"), "ip" : "12.29.70.77", "port" : { "80" : { "http header" : { "Content-Length" : "1507", "X-Powered-By" : "Servlet 2.4; JBoss-4.2.2.GA (build: SVNTag=JBoss_4_2_2_GA date=200710221139)/Tomcat-5.5", "Server" : "Apache-Coyote/1.1", "Last-Modified" : "Mon, 22 Oct 2007 15:43:32 GMT", "Connection" : "close", "ETag" : "W/\"1507-1193067812000\"", "Date" : "Sun, 16 Feb 2014 15:11:22 GMT", "Content-Type" : "text/html" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d32"), "ip" : "14.63.248.58", "port" : { "80" : { "http header" : { "Date" : "Sun, 16 Feb 2014 15:11:22 GMT", "Content-Length" : "266", "Content-Type" : "text/html; charset=iso-8859-1", "Connection" : "close", "Server" : "Apache" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d33"), "ip" : "8.225.196.187", "port" : { "80" : { "http header" : { "X-Powered-By" : "Servlet 2.4; JBoss-4.2.3.GA (build: SVNTag=JBoss_4_2_3_GA date=201001210934)/JBossWeb-2.0", "Set-Cookie" : "siteIDCookie=0; Path=/", "Expires" : "0", "Server" : "Apache-Coyote/1.1", "Connection" : "close", "Pragma" : "no-cache", "Cache-control" : "no-store", "Date" : "Sun, 16 Feb 2014 15:11:22 GMT", "Content-Type" : "text/html;charset=UTF-8" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d34"), "ip" : "14.63.244.223", "port" : { "80" : { "http header" : { "Content-Length" : "1614", "X-Powered-By" : "Servlet 2.4; JBoss-4.3.0.GA_CP10 (build: SVNTag=JBPAPP_4_3_0_GA_CP10 date=201107201825)/JBossWeb-2.0", "Server" : "Apache-Coyote/1.1", "Last-Modified" : "Tue, 27 Dec 2011 12:05:32 GMT", "Connection" : "close", "ETag" : "W/\"1614-1324987532000\"", "Date" : "Sun, 16 Feb 2014 15:11:22 GMT", "Content-Type" : "text/html" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d35"), "ip" : "13.15.253.76", "port" : { "80" : { "http header" : { "Content-Length" : "1614", "X-Powered-By" : "Servlet 2.4; JBoss-4.3.0.GA_CP07 (build: SVNTag=JBPAPP_4_3_0_GA_CP07 date=200912081415)/JBossWeb-2.0", "Server" : "Apache-Coyote/1.1", "Last-Modified" : "Fri, 25 Jun 2010 20:15:31 GMT", "Connection" : "close", "ETag" : "W/\"1614-1277496931000\"", "Date" : "Sun, 16 Feb 2014 15:11:22 GMT", "Content-Type" : "text/html" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d36"), "ip" : "14.63.244.247", "port" : { "80" : { "http header" : { "Content-Length" : "1614", "X-Powered-By" : "Servlet 2.4; JBoss-4.3.0.GA_CP10 (build: SVNTag=JBPAPP_4_3_0_GA_CP10 date=201107201825)/JBossWeb-2.0", "Server" : "Apache-Coyote/1.1", "Last-Modified" : "Tue, 27 Dec 2011 12:05:32 GMT", "Connection" : "close", "ETag" : "W/\"1614-1324987532000\"", "Date" : "Sun, 16 Feb 2014 15:11:22 GMT", "Content-Type" : "text/html" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d37"), "ip" : "14.63.244.243", "port" : { "80" : { "http header" : { "Content-Length" : "1614", "X-Powered-By" : "Servlet 2.4; JBoss-4.3.0.GA_CP10 (build: SVNTag=JBPAPP_4_3_0_GA_CP10 date=201107201825)/JBossWeb-2.0", "Server" : "Apache-Coyote/1.1", "Last-Modified" : "Tue, 27 Dec 2011 12:05:32 GMT", "Connection" : "close", "ETag" : "W/\"1614-1324987532000\"", "Date" : "Sun, 16 Feb 2014 15:11:22 GMT", "Content-Type" : "text/html" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d38"), "ip" : "14.63.244.144", "port" : { "80" : { "http header" : { "Content-Length" : "1614", "X-Powered-By" : "Servlet 2.4; JBoss-4.3.0.GA_CP10 (build: SVNTag=JBPAPP_4_3_0_GA_CP10 date=201107201825)/JBossWeb-2.0", "Server" : "Apache-Coyote/1.1", "Last-Modified" : "Tue, 27 Dec 2011 12:05:32 GMT", "Connection" : "close", "ETag" : "W/\"1614-1324987532000\"", "Date" : "Sun, 16 Feb 2014 15:11:25 GMT", "Content-Type" : "text/html" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d39"), "ip" : "14.63.245.31", "port" : { "80" : { "http header" : { "Content-Length" : "130", "X-Powered-By" : "Servlet 2.4; JBoss-4.3.0.GA_CP08 (build: SVNTag=JBPAPP_4_3_0_GA_CP08 date=201004202136)/JBossWeb-2.0", "Set-Cookie" : "JSESSIONID=F6C3A90ABCB5052EDFF3A071FBD78E23.joinsNode02; Path=/", "Server" : "Apache-Coyote/1.1", "Connection" : "close", "Date" : "Sun, 16 Feb 2014 15:11:26 GMT", "Content-Type" : "text/html;charset=UTF-8" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d3a"), "ip" : "14.63.245.38", "port" : { "80" : { "http header" : { "Content-Length" : "130", "X-Powered-By" : "Servlet 2.4; JBoss-4.3.0.GA_CP08 (build: SVNTag=JBPAPP_4_3_0_GA_CP08 date=201004202136)/JBossWeb-2.0", "Set-Cookie" : "JSESSIONID=3969BE5BBD6CFFF9048569A59044B573.joinsNode02; Path=/", "Server" : "Apache-Coyote/1.1", "Connection" : "close", "Date" : "Sun, 16 Feb 2014 15:11:26 GMT", "Content-Type" : "text/html;charset=UTF-8" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d3b"), "ip" : "14.63.245.181", "port" : { "80" : { "http header" : { "Content-Length" : "1614", "X-Powered-By" : "Servlet 2.4; JBoss-4.3.0.GA_CP08 (build: SVNTag=JBPAPP_4_3_0_GA_CP08 date=201004202136)/JBossWeb-2.0", "Set-Cookie" : "WMONID=-xRj2B8iITR; Expires=Mon, 16-Feb-2015 15:11:26 GMT; Path=/", "Server" : "Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8e-fips-rhel5 DAV/2 mod_jk/1.2.32", "Last-Modified" : "Wed, 21 Apr 2010 01:36:48 GMT", "Connection" : "close", "ETag" : "W/\"1614-1271813808000\"", "Date" : "Sun, 16 Feb 2014 15:11:26 GMT", "Content-Type" : "text/html" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d3c"), "ip" : "14.63.245.180", "port" : { "80" : { "http header" : { "Content-Length" : "1614", "X-Powered-By" : "Servlet 2.4; JBoss-4.3.0.GA_CP08 (build: SVNTag=JBPAPP_4_3_0_GA_CP08 date=201004202136)/JBossWeb-2.0", "Set-Cookie" : "WMONID=zu7AEU_cO1C; Expires=Mon, 16-Feb-2015 15:11:26 GMT; Path=/", "Server" : "Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8e-fips-rhel5 DAV/2 mod_jk/1.2.32", "Last-Modified" : "Wed, 21 Apr 2010 01:36:48 GMT", "Connection" : "close", "ETag" : "W/\"1614-1271813808000\"", "Date" : "Sun, 16 Feb 2014 15:11:26 GMT", "Content-Type" : "text/html" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d3d"), "ip" : "14.63.245.32", "port" : { "80" : { "http header" : { "Content-Length" : "130", "X-Powered-By" : "Servlet 2.4; JBoss-4.3.0.GA_CP08 (build: SVNTag=JBPAPP_4_3_0_GA_CP08 date=201004202136)/JBossWeb-2.0", "Set-Cookie" : "JSESSIONID=67DE379AAB9A4C94BC60882A57916C2F.joinsNode02; Path=/", "Server" : "Apache-Coyote/1.1", "Connection" : "close", "Date" : "Sun, 16 Feb 2014 15:11:26 GMT", "Content-Type" : "text/html;charset=UTF-8" }, "service" : "http" } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d3e"), "ip" : "14.63.242.199", "port" : { "80" : { "http header" : { "X-Powered-By" : "Servlet 2.5; JBoss-5.0/JBossWeb-2.1", "Set-Cookie" : "JSESSIONID=5KMCcEVNW-UqYO36a67yKw__.dmzsrm01; Path=/", "Expires" : "Thu, 01 Jan 1970 00:00:00 GMT", "Server" : "Apache-Coyote/1.1", "Connection" : "close", "Pragma" : "no-cache", "Cache-Control" : "no-store", "Date" : "Sun, 16 Feb 2014 15:11:26 GMT", "Content-Type" : "text/html;charset=utf-8" }, "service" : "http" } } }
> ▊




通过ip查所有数据
> db.info.find({"ip": "14.63.242.199"})
{ "_id" : ObjectId("530d8f621d41c810b8314d3e"), "ip" : "14.63.242.199", "port" : { "80" : { "http header" : { "X-Powered-By" : "Servlet 2.5; JBoss-5.0/JBossWeb-2.1", "Set-Cookie" : "JSESSIONID=5KMCcEVNW-UqYO36a67yKw__.dmzsrm01; Path=/", "Expires" : "Thu, 01 Jan 1970 00:00:00 GMT", "Server" : "Apache-Coyote/1.1", "Connection" : "close", "Pragma" : "no-cache", "Cache-Control" : "no-store", "Date" : "Sun, 16 Feb 2014 15:11:26 GMT", "Content-Type" : "text/html;charset=utf-8" }, "service" : "http" } } }
> ▊




查ip, 但是只返回某些数据
> db.info.find({"ip": "14.63.242.199"}, {"port.80.service":1})
{ "_id" : ObjectId("530d8f621d41c810b8314d3e"), "port" : { "80" : { "service" : "http" } } }
> db.info.find({"ip": "14.63.242.199"}, {"port.80.service":1, "port.80.http header.Server": 1})
{ "_id" : ObjectId("530d8f621d41c810b8314d3e"), "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" }, "service" : "http" } } }
> ▊




默认会返回_id, 可以这样让它不返回
> db.info.find({"ip": "14.63.242.199"}, {"port.80.service":1, "port.80.http header.Server": 1, "_id": 0})
{ "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" }, "service" : "http" } } }
> ▊




查询出服务器是Apache的IP以及Server字段的值(正则匹配不区分大小写)
> db.info.find({"port.80.http header.Server": /Apache/}, {"ip": 1, "port.80.http header.Server": 1})
{ "_id" : ObjectId("530d8f621d41c810b8314d2b"), "ip" : "1.214.61.19", "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d2c"), "ip" : "1.214.193.74", "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d2d"), "ip" : "4.30.102.81", "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d2e"), "ip" : "2.139.237.42", "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d2f"), "ip" : "2.33.211.149", "port" : { "80" : { "http header" : { "Server" : "Apache" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d30"), "ip" : "12.176.37.246", "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d31"), "ip" : "12.29.70.77", "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d32"), "ip" : "14.63.248.58", "port" : { "80" : { "http header" : { "Server" : "Apache" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d33"), "ip" : "8.225.196.187", "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d34"), "ip" : "14.63.244.223", "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d35"), "ip" : "13.15.253.76", "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d36"), "ip" : "14.63.244.247", "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d37"), "ip" : "14.63.244.243", "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d38"), "ip" : "14.63.244.144", "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d39"), "ip" : "14.63.245.31", "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d3a"), "ip" : "14.63.245.38", "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d3b"), "ip" : "14.63.245.181", "port" : { "80" : { "http header" : { "Server" : "Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8e-fips-rhel5 DAV/2 mod_jk/1.2.32" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d3c"), "ip" : "14.63.245.180", "port" : { "80" : { "http header" : { "Server" : "Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8e-fips-rhel5 DAV/2 mod_jk/1.2.32" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d3d"), "ip" : "14.63.245.32", "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d3e"), "ip" : "14.63.242.199", "port" : { "80" : { "http header" : { "Server" : "Apache-Coyote/1.1" } } } }
> ▊




删除整条数据
> db.info.remove({"ip" : "14.63.242.199"})
> db.info.find({"ip" : "14.63.242.199"})
> ▊




删除某个键值对
> db.info.update({"ip" : "14.63.245.38"}, {$unset: {"port.80.http header":1}})
> db.info.find({"ip" : "14.63.245.38"})
{ "_id" : ObjectId("530d8f621d41c810b8314d3a"), "ip" : "14.63.245.38", "port" : { "80" : { "service" : "http" } } }
> ▊




更新数据的某字段
> db.info.find({"ip" : "14.63.245.38"})
{ "_id" : ObjectId("530d8f621d41c810b8314d3a"), "ip" : "14.63.245.38", "port" : { "80" : { "http header" : { "Content-Length" : "130", "X-Powered-By" : "Servlet 2.4; JBoss-4.3.0.GA_CP08 (build: SVNTag=JBPAPP_4_3_0_GA_CP08 date=201004202136)/JBossWeb-2.0", "Set-Cookie" : "JSESSIONID=3969BE5BBD6CFFF9048569A59044B573.joinsNode02; Path=/", "Server" : "Apache-Coyote/1.1", "Connection" : "close", "Date" : "Sun, 16 Feb 2014 15:11:26 GMT", "Content-Type" : "text/html;charset=UTF-8" }, "service" : "http" } } }
> db.info.update({"ip" : "14.63.245.38"}, {$set: {"port.80.http header": "Hello CC!"}})
> db.info.find({"ip" : "14.63.245.38"})
{ "_id" : ObjectId("530d8f621d41c810b8314d3a"), "ip" : "14.63.245.38", "port" : { "80" : { "http header" : "Hello CC!", "service" : "http" } } }
> ▊




统计数据的个数
> db.info.count()
19
> db.info.count({"port.80.http header.Server": /Apache/})
18
> db.info.count({"port.80.http header.Server": /Apache\/[0-9\.]+/})
2
> db.info.find({"port.80.http header.Server": /Apache\/[0-9\.]+/}, {"port.80.http header.Server": 1})
{ "_id" : ObjectId("530d8f621d41c810b8314d3b"), "port" : { "80" : { "http header" : { "Server" : "Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8e-fips-rhel5 DAV/2 mod_jk/1.2.32" } } } }
{ "_id" : ObjectId("530d8f621d41c810b8314d3c"), "port" : { "80" : { "http header" : { "Server" : "Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8e-fips-rhel5 DAV/2 mod_jk/1.2.32" } } } }
> ▊

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|京峰教育,只为有梦想的人 ( 京ICP备15013173号 )

GMT+8, 2020-2-25 14:18 , Processed in 0.023670 second(s), 11 queries , Redis On.

快速回复 返回顶部 返回列表