Professional Documents
Culture Documents
• NoSQL
NoSQL databases (aka "not only SQL") are non-tabular databases and store
data differently than relational tables. NoSQL databases come in a variety of
types based on their data model. The main types are document, key-value,
wide-column, and graph. They provide flexible schemas and scale easily with
large amounts of data and high user loads.
# Creating database
mydb = myclient["xyz"]
mycol = mydb["test"]
print(mydb.list_collection_names())
• In MongoDB, a collection is not created until it gets content, so if
this is your first time creating a collection, you should complete the
create document before you check if the collection exists!
• You can check if a collection exist in a database by listing all
collections:
• mydict = { "name": "John", "address": "Highway 37" }
• x = mycol.insert_one(mydict)
• print(x.inserted_id)
Method Description
It is used to insert a single
db.collection.insertOne()
document in the collection.
It is used to insert multiple
db.collection.insertMany()
documents in the collection.
It is used to create an empty
db.createCollection()
collection.
• Read Operations –
• The Read operations are used to retrieve documents from the
collection, or in other words, read operations are used to query a
collection for a document. You can perform read operation using
the following method provided by the MongoDB:
Method Description
It is used to update a single document in
db.collection.updateOne() the collection that satisfy the given
criteria.
It is used to update multiple documents in
db.collection.updateMany() the collection that satisfy the given
criteria.
It is used to replace single document in
db.collection.replaceOne() the collection that satisfy the given
criteria.
• Delete Operations –
• The delete operation are used to delete or remove the documents
from a collection. You can perform delete operations using the
following methods provided by the MongoDB:
Method Description
• Example—
• import pymongo
• myclient = pymongo.MongoClient("mongodb://localhost:27017/")
• #create database
• mydb = myclient["employee"]
• #crate collection
• mycol = mydb["emp"]
• #add records
• mydict = [{ "name": "Rahul", "empid": "0001" , "designation":"developer"},
• {"name": "Amit", "empid": "0002" , "designation":"lead"},
• {"name": "Vijay", "empid": "0003" , "designation":"tester"}]
• x = mycol.insert_many(mydict)
• #find records
• myquery = { "name": "Rahul" }
• mydoc = mycol.find(myquery)
• for x in mydoc:
• print(x)
• #Delete records
• mycol.delete_one(myquery)
• x = mycol.delete_many(myquery)
• print(x.deleted_count, " documents deleted.")
• #Update records
• myquery = { "empid": "0003" }
• newvalues = { "$set": { "name": "Santosh" } }
• mycol.update_one(myquery, newvalues)
• for x in mycol.find():
• print(x)