py与mongodb更新

常用到的函数,包括插入,获取等

# coding:utf-8
import pymongo


conn = pymongo.MongoClient('127.0.0.1',port=27017)

def get_db():
    
    db_name = conn.database_names()
    print  db_name
    
def get_data():
    db = conn.xunfeng
    table = db.Result
    xinxi = table.find()
    for i in xinxi:
        print i["ip"]
        print i["info"]
        print i["port"]
        document = [{'ip':i['ip'],'info':i['info'],'port':i['port']}]
        print document
        #db.test.insert(document)
        for i in range(len(document)):
            conn.people.test.insert(document[i])
        
        
def insert_data():
    db = conn.people
    db.user.save({'id':1,'url':'www.baidu.com','age':11})
    data = db.user.find()
    for i in data:
        print i
    
        
        
def update_data():
    db = conn.people
    db.user.update({'url':'www.baidu.com'},
                  {'$set':{'url':'www.baidu.com'},
                   '$set':{'age':11},
                   '$addToSet':{'webshell':1}},
                  upsert = True)
#update_data()


def del_data():
    db = conn.people
    db.user.remove()
    db.test.remove({'22':1})
    
#del_data()

import urllib2
import requests

def https_url():
    url = 'https://baidu.com'
    page_content = ''
    request = urllib2.Request(url)
    html_url = requests.get(url)
    url_code = html_url.status_code

    try:
        response = urllib2.urlopen(request)
        if url_code == 200:
            page_content = response.read()
            print url_code
            print url
        else:
            pass
        
    except Exception, e:
        pass    
#https_url()

def update_data1():
    db = conn.people
    db.user.update({'url1':'www.baidu.com'},
                  {'$set':{'test1':'www.test1.com'},
                   '$set':{'age2':11},
                   '$addToSet':{'webshelltest':123}},
                  upsert = True)
update_data1()


    


    






你可能感兴趣的:(py与mongodb更新)