0% found this document useful (0 votes)
12 views11 pages

Session 16

This document provides a guide on getting started with MongoDB, including creating a database, collections, and inserting documents. It explains how to use Python with the pymongo driver to interact with MongoDB, including checking for existing databases and collections. The document includes code snippets for various operations such as creating a database, creating a collection, and inserting single or multiple documents.

Uploaded by

asgd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

Session 16

This document provides a guide on getting started with MongoDB, including creating a database, collections, and inserting documents. It explains how to use Python with the pymongo driver to interact with MongoDB, including checking for existing databases and collections. The document includes code snippets for various operations such as creating a database, creating a collection, and inserting single or multiple documents.

Uploaded by

asgd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Session-

16
MongoDB Get started

MongoDB Create Database


Overview MongoDB Create Collection

MongoDB Insert
MongoDB Get Started
 MongoDB stores data in JSON-like documents,
which makes the database very flexible and
scalable.
 Can download a free MongoDB database at
https://www.mongodb.com.
 Python needs a MongoDB driver to access the
MongoDB database.
 C:\Users\Your Name\AppData\Local\Programs\
Python\Python36-32\Scripts>python -m pip install
pymongo
MongoDB  MongoDB will create the database if it does
not exist, and make a connection to it.

Create
 import pymongo
myclient =
pymongo.MongoClient("mongodb://localhost:27017/"

Database )
mydb = myclient["mydatabase"]
 Can check if a database exist by listing
all databases in you system.
Create  print(myclient.list_database_names())

Database  Can check a specific database by name


 dblist = myclient.list_database_names()

Continues if "mydatabase" in dblist:


print("The database exists.")
MongoDB Create Collection
 A collection in MongoDB is the same as
a table in SQL databases.
 To create a collection in MongoDB, use database
object and specify the name of the collection you
want to create.
 import pymongo
myclient =
pymongo.MongoClient("mongodb://localhost:2701
7/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
Create Collection Continues
 Can check if a collection exist in a database by
listing all collections
 print(mydb.list_collection_names())
 Can check a specific collection by name.
 collist = mydb.list_collection_names()
if "customers" in collist:
print("The collection exists.")
MongoDB Insert
 To Insert a record or document into a
collection,the insert_one() method is used.
 The first parameter of the insert_one()
method is a dictionary containing the name
and value of each field in the document.
 import pymongo
myclient =
pymongo.MongoClient("mongodb://localhost:27017/"
)
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mydict = { "name": "John", "address": "Highway
37" }
x = mycol.insert_one(mydict)
 The inserted_id holds the id of the inserted
document.
 print(x.inserted_id)
 To Insert multiple documents into collection use
the insert_many() method.
 import pymongo
myclient =
pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]

Insert mycol = mydb["customers"]


mylist = [
{ "name": "Amy", "address": "Apple st 652"},

Continues { "name": "Hannah", "address": "Mountain 21"},


{ "name": "Michael", "address": "Valley 345"},
{ "name": "Sandy", "address": "Ocean blvd 2"},
{ "name": "Betty", "address": "Green Grass 1"},
{ "name": "William", "address": "Central st 954"},
{ "name": "Chuck", "address": "Main Road 989"},
{ "name": "Viola", "address": "Sideway 1633"}]
x = mycol.insert_many(mylist)
#print list of the _id values of the inserted documents:
print(x.inserted_ids)
Thank You

You might also like