Created
March 20, 2022 07:17
-
-
Save shashwata27/a02309a8652946928f335d6f88f36165 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pymongo import MongoClient | |
cur=MongoClient(MONGO_HOST,MONGO_PORT) | |
# create db, if db dosen't exsists | |
db=cur[DB_NAME] | |
# create collection, if db dosen't exsists | |
col=db[COLLECTION_NAME] | |
# create documents, | |
# collection and db only created when we insert at least one doc | |
col.insert_one({name:"shash"}) | |
col.insert_mane(list_of_json_objects) | |
#list names | |
cur.list_database_names() | |
db.list_collections_names() | |
# finding from collections | |
# returns all documents with name attribute equals to "shash" | |
col.find({name:"shash"}) | |
col.find_one({name:"shash"}) | |
# deleting documents, might use regex for many | |
myquery = { "address": "Mountain 21" } | |
col.delete_one(myquery) | |
col.delete_many(myquery) | |
# deleting db and collections | |
cur.drop_database() | |
db.drop_collection() | |
db.drop()# alias for drop_collections |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment