import pymongo
from pymongo import UpdateOne
collection = client[mongo_db][mongo_collection]
opearations = [
UpdateOne(
dict(myid=d['myid']),
{"$set": d},
upsert=True
) for d in document_list
]
cur = collection.bulk_write(operations)
if cur.acknowledge:
print("succeeded")
Delete Many
import pymongo
from pymongo import UpdateOne
collection = client[mongo_db][mongo_collection]
filter = {}
ret = collection.delete_many(filter)
How to use find with projection
import pymongo
client = pymongo.MongoClient(mongo_uri)
collection = client[mongo_db][mongo_collection]
cur = collection.find(
filter=filter,
projection=dict(_id=False, __v=False),
skip=0,
limit=10
)
results = [c for c in cur]
How to get collection names and counts of documents
cls = [
dict(
name=name,
count_docs=db[name].count_documents({})
) for name in db.list_collection_names()
]