PyMongo--非关系型数据库mongodb入门(2)

来源:未知 责任编辑:责任编辑 发表时间:2014-04-20 03:42 点击:

 
    这点和versant数据库到很像,有点面向对象的味道
4.6       删除a=1,b=1的数据
    coll.remove({a:1,b:1}) 注意了 删的是俩条数据哦
4.7  将b=1的所有数据的a改成1
     coll.update({b:1},{$set:{a:1}})
4.8      OK,增删改查都有了 shell差不多就介绍这么多了。下面要用pymongo
    关于pymongo的介绍,我想没必要再这样一步一步来了,否则就有污蔑大众智商的嫌疑了。直接给代码吧,我尽量多写些有意义的注释。
 
 1 #! /usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 import pymongo
 4 
 5 class PyConnect(object):
 6     
 7     def __init__(self, host, port):
 8         #conn 类型<class 'pymongo.connection.Connection'>
 9         try:
10             self.conn = pymongo.Connection(host, port)
11         except  Error:  www.2cto.com  
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'
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • Request.ServerVariables 参数大全
  • 执行全文索引时出现权限不足的解决方法
  • 导入excel文件处理流程节点的解决方案
  • 查看sql修改痕迹(SQL Change Tracking on Table)
  • App数据层设计及云存储使用指南
  • PostgreSQL启动过程中的那些事三:加载GUC参数
  • MongoDB安装为Windows服务方法与注意事项
  • Percolator与分布式事务思考(二)
  • 写给MongoDB开发者的50条建议Tip1
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1