mongodb命令总结


mongodb 命令总结

class MongoOper:
    def __init__(self):
        self.client = MongoClient()
        self.db = object()
        self.setdb = ('webclasy')
        self.dbauth('zw','lzw')

    def setdb(self,dbname):
        self.db = self.client([dbname])
    
    def duauth(self,uname,upasswd):
         self.db.authenticate(uname,upasswd)     

使用前先实例化一个MongoOper对象mo,将指针(姑且先这么叫)定位到需要操作的集合上

point = mo.db[your_collection_name]

之后便在该point上进行各种操作

  1. 插某个记录入
    pont.insert(the_obj)
    
  2. 删除
    point.remove({‘url’:your_url}) 注:’url’是你要删除的字段所满足的条件
  3. 更新
    point.update({‘_id’:your_id},{‘$set’:{‘chi_num’:your_value,’another’:your_value})
  4. 查找最大值
    maxob = point.aggregate([{‘$match’:{}},{‘$group’:{‘_id’:’max’,’max_value’:{‘$max’:’$theKth’}}}]
    返回的maxob,获取结果的方式为
    theKth = 0
    for m in maxob:
     print i
     i += 1
     theKth = m['max_value']
     if theKth == None:
         theKth = 0
     break
    
dfas /
Published under (CC) BY-NC-SA in categories Project  tagged with Linux