#Download from http://www.mongodb.org/downloads #Create a free account on MongoDB Atlas to get a cluster connection to setup MongoDB Compass # On Anaconda CMD prompt # type pip install pymongo
In [2]:
import pymongo
In [3]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["valdb01"] # create a database on mongodb
MongoDB waits until you have created a collection (table), with at least one document (record) before it actually creates the database (and collection).In [6]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["valdb01"]
mycol = mydb["customers"] #create a collection
Inserting into collection
To insert a record, or document as it is called in MongoDB, into a collection, we use the insert_one() method.
The first parameter of the insert_one() method is a dictionary containing the name(s) and value(s) of each field in the document you want to insert.
In [9]:
#Insert a document (record) in the customers collection
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["valdb01"]
mycol = mydb["customers"]
mydict = { "name": "John", "address": "Highway 37" }
x = mycol.insert_one(mydict)
print(x)
<pymongo.results.InsertOneResult object at 0x0000011D7624EE00>
In [10]:
print(myclient.list_database_names())
['admin', 'config', 'local', 'valdb01']
In [11]:
dblist = myclient.list_database_names()
if "valdb01" in dblist:
print("The database exists.")
The database exists.
In [13]:
#Insert another record in the "customers" collection, and return the value of the _id field:
import pymongo
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['valdb01']
mycol = mydb["customers"]
mydict = { "name": "Peter", "address": "Lowstreet 27" }
x = mycol.insert_one(mydict)
print(x.inserted_id)
61385fc856e0542d940db714
In [14]:
#Insert multiple documents
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["valdb01"]
mycol = mydb["customers"]
mylist = [
{ "name": "Amy", "address": "Apple st 652"},
{ "name": "Hannah", "address": "Mountain 21"},
{ "name": "Michael", "address": "Valley 345"},
{ "name": "Sandy", "address": "Ocean blvd 2"},
{ "name": "Betty", "address": "Green Grass 1"},
{ "name": "Richard", "address": "Sky st 331"},
{ "name": "Susan", "address": "One way 98"},
{ "name": "Vicky", "address": "Yellow Garden 2"},
{ "name": "Ben", "address": "Park Lane 38"},
{ "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)
[ObjectId('6138618356e0542d940db716'), ObjectId('6138618356e0542d940db717'), ObjectId('6138618356e0542d940db718'), ObjectId('6138618356e0542d940db719'), ObjectId('6138618356e0542d940db71a'), ObjectId('6138618356e0542d940db71b'), ObjectId('6138618356e0542d940db71c'), ObjectId('6138618356e0542d940db71d'), ObjectId('6138618356e0542d940db71e'), ObjectId('6138618356e0542d940db71f'), ObjectId('6138618356e0542d940db720'), ObjectId('6138618356e0542d940db721')]
In [15]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["valdb01"]
mycol = mydb["customers"]
mylist = [
{ "_id": 1, "name": "John", "address": "Highway 37"},
{ "_id": 2, "name": "Peter", "address": "Lowstreet 27"},
{ "_id": 3, "name": "Amy", "address": "Apple st 652"},
{ "_id": 4, "name": "Hannah", "address": "Mountain 21"},
{ "_id": 5, "name": "Michael", "address": "Valley 345"},
{ "_id": 6, "name": "Sandy", "address": "Ocean blvd 2"},
{ "_id": 7, "name": "Betty", "address": "Green Grass 1"},
{ "_id": 8, "name": "Richard", "address": "Sky st 331"},
{ "_id": 9, "name": "Susan", "address": "One way 98"},
{ "_id": 10, "name": "Vicky", "address": "Yellow Garden 2"},
{ "_id": 11, "name": "Ben", "address": "Park Lane 38"},
{ "_id": 12, "name": "William", "address": "Central st 954"},
{ "_id": 13, "name": "Chuck", "address": "Main Road 989"},
{ "_id": 14, "name": "Viola", "address": "Sideway 1633"}
]
x = mycol.insert_many(mylist)
#print list of the _id values of the inserted documents:
print(x.inserted_ids)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
In [ ]: