鄱阳有做百度网站的,手机网站页面文字做多大,wordpress编辑器字体大小,物流网站建设PyMongo--非关系型数据库mongodb入门#xff08;一步一步 版#xff09; 本文主要内容#xff1a; 1.简要介绍mongodb 2.Pymongo 3.mongo shell 4.我的mongodb入门之旅 1.简要介绍mongodb MongoDB是一个基于分布式文件存储的数据库。由C语言编写。旨在为WEB应用提供可扩展的…PyMongo--非关系型数据库mongodb入门一步一步 版 本文主要内容 1.简要介绍mongodb 2.Pymongo 3.mongo shell 4.我的mongodb入门之旅 1.简要介绍mongodb MongoDB是一个基于分布式文件存储的数据库。由C语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。MongoDB是一个介于关系数据库和非关系数据库之间的产品是非关系数据库当中功能最丰富最像关系数据库的。他支持的数据结构非常松散是类似json的bjson格式因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大其语法有点类似于面向对象的查询语言几乎可以实现类似关系数据库单表查询的绝大部分功能而且还支持对数据建立索引。园里有博友的介绍写的比较详细还介绍了mongo的安装 http://www.cnblogs.com/lipan/archive/2011/03/08/1966463.html 2.Pymongo 在说pymongo之前先看看一篇博文特别适合有数据库操作经验的看 http://www.cnblogs.com/xffy1028/archive/2011/12/03/2272837.html PyMongo 是 MongoDB 的 Python 接口开发包。 3.mongo shell 学mongo非常有必要学mongo shell 因为mongo shell 可以更加便捷的直接操作数据库特别适合检查。 这里园里有一篇总结的挺好 http://www.blogjava.net/waterjava/archive/2011/03/23/346819.html 这个shell和Mongodb里的很多操作都有相似之处不要孤立起来学 4.我的mongodb入门之旅 以上这些信息我们不可能一开始就掌握稍微有个大致的了解就行。 OK下面开始我的mongo入门之旅了这才是今天的重点 4.1 使用 mongo -port XXXX 登录shell 由于服务器中的mongo端口已经更改了 4.2 use test_for_new (建立一个新的数据库我在shell里没找到建立的命令试了下这个use命令这个也能间接建立数据库) 4.3 show dbs 显示系统的数据库名称 和对应大小 show collections 显示当前使用的数据库中的collections应该是集合的意思这个collection对应于关系数据库中表。show tables 有同样效果 db 查看当前使用数据库的名称 4.4 同样没找到建立collections的现成命令实验后发现。这个数据库的使用就和python里的变量一样不需事先申明。 直接用就行了没有的话会自动建立。可以用dbs.collections的形式像面向对象吧例如 db.collection1.insert({a:1,b:1}) 怎一个爽字了得这个可以理解成python里的dict。还有如果嫌命令太长可以事先将 colldb.collection1 (赋值给一个临时变量) coll.insert({a:1,b:1}) 这插入格式为什么是这样先放放 咱不急。 使用coll.find()就可以看到collection1的所有数据了使用coll.drop()就可以删除该collection及里面的数据了。那么多插一些数据到collection1吧 4.5 关于插入必须要注意几点。其一因为是非关系的所以不会严格限制格式底层的存储像dict 所以可以 coll.insert({a:1,b:1,c:1}) 其二即便你再coll.insert({a:1,b:1})一下他也不会报错而且出来的是两条该数据不信的话用coll.find({a:1,b:1})试试 事实上系统会增加一个唯一标识字段“_id”用于区分数据 这点和versant数据库到很像有点面向对象的味道 4.6 删除a1,b1的数据 coll.remove({a:1,b:1}) 注意了 删的是俩条数据哦 4.7 将b1的所有数据的a改成1 coll.update({b:1},{$set:{a:1}}) 4.8 OK增删改查都有了 shell差不多就介绍这么多了。下面要用pymongo 关于pymongo的介绍我想没必要再这样一步一步来了否则就有污蔑大众智商的嫌疑了。直接给代码吧我尽量多写些有意义的注释。 1 #! /usr/bin/env python2 # -*- coding: utf-8 -*-3 import pymongo4 5 class PyConnect(object):6 7 def __init__(self, host, port):8 #conn 类型class pymongo.connection.Connection9 try:
10 self.conn pymongo.Connection(host, port)
11 except Error:
12 print connect to %s:%s fail %(host, port)
13 exit(0)
14
15 def __del__(self):
16 self.conn.close()
17
18 def use(self, dbname):19 # 这种[]获取方式同样适用于shell,下面的collection也一样
20 #db 类型class pymongo.database.Database
21 self.db self.conn[dbname]
22
23 def setCollection(self, collection):
24 if not self.db:
25 print don\t assign database
26 exit(0)
27 else:
28 self.coll self.db[collection]
29
30 def find(self, query {}):
31 #注意这里query是dict类型
32 if type(query) is not dict:
33 print the type of query isn\t dict
34 exit(0)
35 try:
36 #result类型class pymongo.cursor.Cursor
37 if not self.coll:
38 print don\t assign collection
39 else:
40 result self.coll.find(query)
41 except NameError:
42 print some fields name are wrong in ,query
43 exit(0)
44 return result
45
46 def insert(self, data):47 if type(data) is not dict:
48 print the type of insert data isn\t dict
49 exit(0)
50 #insert会返回新插入数据的_id
51 self.coll.insert(data)
52
53 def remove(self, data):
54 if type(data) is not dict:
55 print the type of remove data isn\t dict
56 exit(0)
57 #remove无返回值
58 self.coll.remove(data)
59
60 def update(self, data, setdata):
61 if type(data) is not dict or type(setdata) is not dict:
62 print the type of update and data isn\t dict
63 exit(0)
64 #update无返回值
65 self.coll.update(data,{$set:setdata})
66
67 if __name__ __main__:
68 connect PyConnect(localhost, 27017)
69 connect.use(test_for_new)
70 connect.setCollection(collection1)
71 connect.insert({a:10, b:1})
72 result connect.find()
73 connect.update({a:10, b:1}, {b:10})
74 #x也是dict类型非常好75 for x in result:
76 if c in x:
77 print x[_id], x[a], x[b], x[c]
78 else:
79 print x[_id], x[a], x[b]
80 connect.remove({a:10})
4.9 补充在调用self.conn[dbname]和self.db[collection].find(query)的时候要是能再加个存在性判断就好了不然很容易出问题。5.0 好了本文到这里就结束了最后留个问题。在以上代码中最终输出结果里为什么是a10,b10而不是a10,b1