python操作mongodb 简单Demo-mongodb -version 3.4.0

由于公司的mongodb部署于Docker  所以 url  大家看不懂

1.如果想查询 集合中 所有文档  用 find  方法  然后  进行遍历   不遍历 会 出现一个 cusr对象  不是想要的json结果

2. 如果集合中中只有一个文档   公司业务 确实有这样的情况  那么用find_one即可 且 不用遍历  就是所要的json结果

# --coding:utf-8--

from pymongo import MongoClient


#client = MongoClient('mongodb://mongo-0.mongo.public:27017')
#client = MongoClient('localhost', 27017)




def fieldmap_insert(data):
    db = client['log_fieldmap']
    db.authenticate('usr', 'pwd')
    domain = data[0]
    collection = db[domain]
    result = collection.save(data[3])
    client.close()




def rowrelate_insert(data):
    db = client['log_rowrelate']
    db.authenticate('usr', 'pwd')
    collection = db['rowrelate']
    result = collection.save(data)
    client.close()




def fieldmap_find(table, id):
    db = client['log_fieldmap']
     db.authenticate('usr', 'pwd')
    collection = db[table]
    content = collection.find_one({'_id': id})
    return content
    client.close()




def fieldmap_find_2(table):
    db = client['log_fieldmap']
    db.authenticate('usr', 'pwd')
    collection = db[table]
    content = collection.find()
    return content
    client.close()




def rowrelate_find(id):
    db = client['log_rowrelate']
     db.authenticate('usr', 'pwd')
    collection = db['rowrelate']
    content = collection.find_one({'_id': id})
    return content
    client.close()
发布了83 篇原创文章 · 获赞 19 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/u013939918/article/details/77948175