import pymongo
|
|
mongo_host = "127.0.0.1"
|
mongo_port = 27017
|
|
|
def _getdb():
|
client = pymongo.MongoClient(host=mongo_host, port=27017)
|
db = client.gp
|
return db
|
|
|
def save(dn_name, datas):
|
try:
|
db = _getdb()
|
collections = db[dn_name]
|
for data in datas:
|
collections.delete_one({"_id": data["_id"]})
|
collections.insert_one(data)
|
except:
|
pass
|
|
def save_one(dn_name, data):
|
try:
|
db = _getdb()
|
collections = db[dn_name]
|
collections.delete_one({"_id": data["_id"]})
|
collections.insert_one(data)
|
except:
|
pass
|
|
|
def find(dn_name, where_dict):
|
try:
|
db = _getdb()
|
collections = db[dn_name]
|
result = collections.find(where_dict)
|
print(result)
|
return result
|
except:
|
pass
|
|
def count(dn_name, where_dict):
|
try:
|
db = _getdb()
|
collections = db[dn_name]
|
result = collections.count_documents(where_dict)
|
print(result)
|
return result
|
except:
|
pass
|
|
|
if __name__ == '__main__':
|
|
result = find("ths-industry-codes", {'$or': [{'_id': '300007'}, {'_id': '300008'}]})
|
for a in result:
|
print(a)
|