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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值