MQL – Perform Find in Database

#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('6138663a6461436a279a77d2'), ObjectId('6138663a6461436a279a77d3'), ObjectId('6138663a6461436a279a77d4'), ObjectId('6138663a6461436a279a77d5'), ObjectId('6138663a6461436a279a77d6'), ObjectId('6138663a6461436a279a77d7'), ObjectId('6138663a6461436a279a77d8'), ObjectId('6138663a6461436a279a77d9'), ObjectId('6138663a6461436a279a77da'), ObjectId('6138663a6461436a279a77db'), ObjectId('6138663a6461436a279a77dc'), ObjectId('6138663a6461436a279a77dd')]

In [16]:

#Print all documents in in database valdb01
import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["valdb01"]
mycol = mydb["customers"]

for x in mycol.find():
  print(x)
{'_id': ObjectId('61385e5156e0542d940db712'), 'name': 'John', 'address': 'Highway 37'}
{'_id': ObjectId('61385fc856e0542d940db714'), 'name': 'Peter', 'address': 'Lowstreet 27'}
{'_id': ObjectId('6138618356e0542d940db716'), 'name': 'Amy', 'address': 'Apple st 652'}
{'_id': ObjectId('6138618356e0542d940db717'), 'name': 'Hannah', 'address': 'Mountain 21'}
{'_id': ObjectId('6138618356e0542d940db718'), 'name': 'Michael', 'address': 'Valley 345'}
{'_id': ObjectId('6138618356e0542d940db719'), 'name': 'Sandy', 'address': 'Ocean blvd 2'}
{'_id': ObjectId('6138618356e0542d940db71a'), 'name': 'Betty', 'address': 'Green Grass 1'}
{'_id': ObjectId('6138618356e0542d940db71b'), 'name': 'Richard', 'address': 'Sky st 331'}
{'_id': ObjectId('6138618356e0542d940db71c'), 'name': 'Susan', 'address': 'One way 98'}
{'_id': ObjectId('6138618356e0542d940db71d'), 'name': 'Vicky', 'address': 'Yellow Garden 2'}
{'_id': ObjectId('6138618356e0542d940db71e'), 'name': 'Ben', 'address': 'Park Lane 38'}
{'_id': ObjectId('6138618356e0542d940db71f'), 'name': 'William', 'address': 'Central st 954'}
{'_id': ObjectId('6138618356e0542d940db720'), 'name': 'Chuck', 'address': 'Main Road 989'}
{'_id': ObjectId('6138618356e0542d940db721'), 'name': 'Viola', 'address': 'Sideway 1633'}
{'_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'}
{'_id': ObjectId('6138663a6461436a279a77d2'), 'name': 'Amy', 'address': 'Apple st 652'}
{'_id': ObjectId('6138663a6461436a279a77d3'), 'name': 'Hannah', 'address': 'Mountain 21'}
{'_id': ObjectId('6138663a6461436a279a77d4'), 'name': 'Michael', 'address': 'Valley 345'}
{'_id': ObjectId('6138663a6461436a279a77d5'), 'name': 'Sandy', 'address': 'Ocean blvd 2'}
{'_id': ObjectId('6138663a6461436a279a77d6'), 'name': 'Betty', 'address': 'Green Grass 1'}
{'_id': ObjectId('6138663a6461436a279a77d7'), 'name': 'Richard', 'address': 'Sky st 331'}
{'_id': ObjectId('6138663a6461436a279a77d8'), 'name': 'Susan', 'address': 'One way 98'}
{'_id': ObjectId('6138663a6461436a279a77d9'), 'name': 'Vicky', 'address': 'Yellow Garden 2'}
{'_id': ObjectId('6138663a6461436a279a77da'), 'name': 'Ben', 'address': 'Park Lane 38'}
{'_id': ObjectId('6138663a6461436a279a77db'), 'name': 'William', 'address': 'Central st 954'}
{'_id': ObjectId('6138663a6461436a279a77dc'), 'name': 'Chuck', 'address': 'Main Road 989'}
{'_id': ObjectId('6138663a6461436a279a77dd'), 'name': 'Viola', 'address': 'Sideway 1633'}

In [18]:

#Returned names and addresses not the ids
import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["valdb01"]
mycol = mydb["customers"]

for x in mycol.find({},{ "_id": 0, "name": 1, "address": 1 }):
  print(x)
{'name': 'John', 'address': 'Highway 37'}
{'name': 'Peter', 'address': 'Lowstreet 27'}
{'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'}
{'name': 'John', 'address': 'Highway 37'}
{'name': 'Peter', 'address': 'Lowstreet 27'}
{'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'}
{'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'}

In [19]:

#This will exclude all addresses
import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["valdb01"]
mycol = mydb["customers"]

for x in mycol.find({},{ "address": 0 }):
  print(x)
{'_id': ObjectId('61385e5156e0542d940db712'), 'name': 'John'}
{'_id': ObjectId('61385fc856e0542d940db714'), 'name': 'Peter'}
{'_id': ObjectId('6138618356e0542d940db716'), 'name': 'Amy'}
{'_id': ObjectId('6138618356e0542d940db717'), 'name': 'Hannah'}
{'_id': ObjectId('6138618356e0542d940db718'), 'name': 'Michael'}
{'_id': ObjectId('6138618356e0542d940db719'), 'name': 'Sandy'}
{'_id': ObjectId('6138618356e0542d940db71a'), 'name': 'Betty'}
{'_id': ObjectId('6138618356e0542d940db71b'), 'name': 'Richard'}
{'_id': ObjectId('6138618356e0542d940db71c'), 'name': 'Susan'}
{'_id': ObjectId('6138618356e0542d940db71d'), 'name': 'Vicky'}
{'_id': ObjectId('6138618356e0542d940db71e'), 'name': 'Ben'}
{'_id': ObjectId('6138618356e0542d940db71f'), 'name': 'William'}
{'_id': ObjectId('6138618356e0542d940db720'), 'name': 'Chuck'}
{'_id': ObjectId('6138618356e0542d940db721'), 'name': 'Viola'}
{'_id': 1, 'name': 'John'}
{'_id': 2, 'name': 'Peter'}
{'_id': 3, 'name': 'Amy'}
{'_id': 4, 'name': 'Hannah'}
{'_id': 5, 'name': 'Michael'}
{'_id': 6, 'name': 'Sandy'}
{'_id': 7, 'name': 'Betty'}
{'_id': 8, 'name': 'Richard'}
{'_id': 9, 'name': 'Susan'}
{'_id': 10, 'name': 'Vicky'}
{'_id': 11, 'name': 'Ben'}
{'_id': 12, 'name': 'William'}
{'_id': 13, 'name': 'Chuck'}
{'_id': 14, 'name': 'Viola'}
{'_id': ObjectId('6138663a6461436a279a77d2'), 'name': 'Amy'}
{'_id': ObjectId('6138663a6461436a279a77d3'), 'name': 'Hannah'}
{'_id': ObjectId('6138663a6461436a279a77d4'), 'name': 'Michael'}
{'_id': ObjectId('6138663a6461436a279a77d5'), 'name': 'Sandy'}
{'_id': ObjectId('6138663a6461436a279a77d6'), 'name': 'Betty'}
{'_id': ObjectId('6138663a6461436a279a77d7'), 'name': 'Richard'}
{'_id': ObjectId('6138663a6461436a279a77d8'), 'name': 'Susan'}
{'_id': ObjectId('6138663a6461436a279a77d9'), 'name': 'Vicky'}
{'_id': ObjectId('6138663a6461436a279a77da'), 'name': 'Ben'}
{'_id': ObjectId('6138663a6461436a279a77db'), 'name': 'William'}
{'_id': ObjectId('6138663a6461436a279a77dc'), 'name': 'Chuck'}
{'_id': ObjectId('6138663a6461436a279a77dd'), 'name': 'Viola'}

In [ ]: