-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmongodb_auth.py
51 lines (42 loc) · 1.48 KB
/
mongodb_auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Import the necessary libraries
from pymongo import MongoClient
from pymongo.server_api import ServerApi
import credentials
import certifi
#import urllib.parse
#password=urllib.parse.quote_plus(credentials.mongodb_password)
#uri= "mongodb+srv://" + mongodb_username + ":" + password + mongodb_uri
uri = credentials.mongodb_uri
# Function to purge the collection
def purge_collection(collection):
result = collection.delete_many({})
return('Deleted {} documents from collection.'.format(result.deleted_count))
""" def addData(data,collection):
ids=[]
for item in data:
# Insert a document
doc = item
result = collection.insert_one(doc)
print("Inserted document with id : ",result.inserted_id)
ids.append(result.inserted_id)
return(ids)
"""
def addData(data,collection):
# Insert all documents
result = collection.insert_many(data)
return(len(result.inserted_ids))
def authenticatedb(dbname='maindb'):
# Create a new client and connect to the server
client = MongoClient(uri, server_api=ServerApi('1'),tlsCAFile=certifi.where())
# Send a ping to confirm a successful connection
try:
client.admin.command('ping')
import sys
#print (sys._getframe(1).f_code.co_name)
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
# Get the database
return client[dbname]
if __name__ == '__main__':
authenticatedb()