设为首页 加入收藏

TOP

PyMongo--非关系型数据库mongodb入门(二)
2014-11-24 08:23:59 来源: 作者: 【 】 浏览:3
Tags:PyMongo-- 关系型 数据库 mongodb 入门
me 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) www.2cto.com
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: www.2cto.com
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  好了,本文到这里就结束了,最后留个问题。在以上代码中最终输出结果里为什么是a=10,b=10,而不是a=10,b=1
作者 2010Freeze
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇关系数据库的几种设计范式介绍 下一篇MongoDB简介

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis压力测试实战 - (2025-12-27 09:20:24)
·高并发一上来,微服 (2025-12-27 09:20:21)
·Redis 高可用架构深 (2025-12-27 09:20:18)
·Linux 系统监控 的完 (2025-12-27 08:52:29)
·一口气总结,25 个 L (2025-12-27 08:52:27)