#!/usr/bin/env python# coding: utf-8
# In[63]:
import pymongo;
# In[64]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
# In[65]:
# Check if database exist
# In[66]:
print(myclient.list_database_names())
# In[67]:
dblist = myclient.list_database_names()if "mydatabase" in dblist: print("The database exists.")
# In[68]:
# Create a collection
# In[69]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]
mycol = mydb["customers"]
# In[70]:
#collection exist
# In[71]:
print(mydb.list_collection_names())
# In[72]:
collist = mydb.list_collection_names()if "customers" in collist: print("The collection exists.")
# In[73]:
# Insert into collection
# In[74]:
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)
# In[75]:
# return id of the inserted record
# In[76]:
mydict = { "name": "Peter", "address": "Lowstreet 27" }x = mycol.insert_one(mydict)print(x.inserted_id)
# In[77]:
# How to insert multiple documents
# In[78]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]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)
# In[79]:
#insert many documents and also provide the id to each record/document
# In[80]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]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)
# In[81]:
# find one
# In[82]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]
x = mycol.find_one()
print(x)
# In[83]:
# Select * i.e. find
# In[84]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]
for x in mycol.find(): print(x)
# In[23]:
# find some columns
# In[26]:
import pymongo;
myclient = pymongo.MongoClient("mongodb://localhost:27017/");mydb = myclient["mydatabase"];mycol = mydb["customers"];
for x in mycol.find({},{ "_id": 0, "name": 1, "address": 1 }): print(x)
# In[27]:
# Exclude address from the result
# In[28]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]
for x in mycol.find({},{ "address": 0 }): print(x)
# In[29]:
#error if both 0 and 1 in the same object
# In[30]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]
for x in mycol.find({},{ "name": 1, "address": 0 }): print(x)
# In[31]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]
for x in mycol.find({},{ "name": 1 }): print(x)
# In[32]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]
myquery = { "address": { "$gt": "S" } }
mydoc = mycol.find(myquery)
for x in mydoc: print(x)
# In[33]:
# advanced query with regular expressions
# In[34]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]
myquery = { "address": { "$regex": "^S" } }
mydoc = mycol.find(myquery)
for x in mydoc: print(x)
# In[35]:
# Sort
# In[36]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]
mydoc = mycol.find().sort("name")
for x in mydoc: print(x)
# In[37]:
# reverse, alphabetically
# In[38]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]
mydoc = mycol.find().sort("name", -1)
for x in mydoc: print(x)
# In[39]:
#delete_one, only the first
# In[40]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]
myquery = { "address": "Mountain 21" }
mycol.delete_one(myquery)
# In[41]:
# delete_many
# In[42]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]
myquery = { "address": {"$regex": "^S"} }
x = mycol.delete_many(myquery)
print(x.deleted_count, " documents deleted.")
# In[43]:
# delete all documents
# In[44]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]
x = mycol.delete_many({})
print(x.deleted_count, " documents deleted.")
# In[45]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]
mycol.drop()
# In[46]:
# update data: update one
# In[47]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]
myquery = { "address": "Valley 345" }newvalues = { "$set": { "address": "Canyon 123" } }
mycol.update_one(myquery, newvalues)
#print "customers" after the update:for x in mycol.find(): print(x)
# In[48]:
#update many
# In[49]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]
myquery = { "address": { "$regex": "^S" } }newvalues = { "$set": { "name": "Minnie" } }
x = mycol.update_many(myquery, newvalues)
print(x.modified_count, "documents updated.")
# In[50]:
# limit
# In[85]:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient["mydatabase"]mycol = mydb["customers"]
myresult = mycol.find().limit(5)
#print the result:for x in myresult: print(x)
# In[ ]:
Ref: W3Schools