Wednesday, August 31, 2016

More pymongo Inserts and other Experiments

Inserts


Here is simple pymongo code for inserting a record. Again it is very transparent and not much different that just doing it in the Mongo command line editor.

from pymongo import MongoClient

product={"_id" : 11,
         "product" : "HP All in one computer",
         "price" : 1395.99,
         "quantityAvailable" : 4
         } 

client = MongoClient()
db=client.TechStore
db.productCollection.insert_one(product)

I guess that "product" is what python calls a "dictionary" object. The next trick is to see if I can populate product with variables from user input. This will be essential if I am to create a graphical interface for the database. This proved to be exceptionally easy.

from pymongo import MongoClient
       

client = MongoClient()
db=client.TechStore

maxID=db.productCollection.count()
print (maxID)

name=input("Enter product name: ")
price=input("Enter the price: ")
quantity=input("Enter the quantity available: ")

ID = maxID+1;

product={"_id" : ID,
         "product" : name,
         "price" : price,
         "quantityAvailable" : quantity
         }


db.productCollection.insert_one(product)

cursor = db.productCollection.find()

for document in cursor:
    print(document)
   
insert with prompts

Because I am manually entering the _id, I used the count to get the number of records and add one to it to make the next _Id. This is provisional and obviously fraught with potential problems. The _id fields might not be numbered sequentially. Also, if the data existed in several shards (files spread across servers), there is no guarantee that the count of the records will be accurate. But it works for now.

A few more experiments


For the first experiment I want to retrieve just the names of the products, ask the user to enter the product of their choice and return the information for only that product. The difficulty I had for this was figuring out how to do a null query criteria. The answer was just to use empty braces for the query {} and then specify the field I wished to see. The "_id" : 0 means to suppress the id field

from pymongo import MongoClient
       

client = MongoClient()
db=client.TechStore

cursor = db.productCollection.find({},{"product" :1, "_id" : 0})

for document in cursor:
  print(document)

Here is the result screen.

products

Here is the code with the user choice added and the result set."

from pymongo import MongoClient
       

client = MongoClient()
db=client.TechStore

cursor = db.productCollection.find({},{"product" :1, "_id" : 0})

for document in cursor:
  print(document)

choice = input("Choose the product you wish: ")

cursor = db.productCollection.find({"product" : choice})

for document in cursor:
  print(document)
User choice

Now the last experiment before I venture into creating a graphical interface is to see if I can update the database, subtracting one from quantityAvailable. To do this involves using the $inc increment operator and increment it by -1. Here is the code for the whole thing with the update.

from pymongo import MongoClient
       

client = MongoClient()
db=client.TechStore

cursor = db.productCollection.find({},{"product" :1, "_id" : 0})

for document in cursor:
  print(document)

choice = input("Choose the product you wish: ")

cursor = db.productCollection.find({"product" : choice})

for document in cursor:
  print(document)

  

result = db.productCollection.update(
    {"product" : choice},
    {"$inc" : 
     {"quantityAvailable" : -1}
     }
    )

cursor = db.productCollection.find({"product" : choice})

for document in cursor:
  print(document)
     

Notice that the quantity available is less by one.

The next step is to use tkinker to create a graphical interface to the database.

No comments:

Post a Comment