由于公司的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()