Guest User

Untitled

a guest
Oct 21st, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. from pymongo import MongoClient
  2. # pprint library is used to make the output look more pretty
  3. from pprint import pprint
  4.  
  5. mongodb_url = "mongodb://localhost:27017"
  6.  
  7. # connect to MongoDB
  8. client = MongoClient(mongodb_url)
  9. db=client.Northwind
  10. # Issue the serverStatus command and print the results
  11. serverStatusResult=db.command("serverStatus")
  12. #pprint(serverStatusResult)
  13.  
  14. # NOTE: The code is really well commented, each step is explained in some details.
  15. # This would not be done for code in production but this is a code used to illustrate learning
  16. # and therefore we decided explain each step in details. Of course this makes the code less
  17. # readable but it should make it easier to understand the flow and the actions taken by each step
  18.  
  19. orders = db["order-details"].aggregate([
  20. # lookup is used to outer join the order_details and the orders tables
  21. {
  22. "$lookup":
  23. {
  24. "from" : "orders",
  25. "localField" : "OrderID",
  26. "foreignField" : "OrderID",
  27. "as" : "order"
  28. }
  29. },
  30. # Only get records that have CustomerID = ALFKI
  31. { "$match" : { "order.CustomerID":"ALFKI" } },
  32. # Group records together by OrderId
  33. # and when grouping increment a orderID counter by one for each record that is combined
  34. { "$group" : {"_id":"$OrderID", "OrderID_counter":{"$sum":1}} },
  35. # Use match to filter out the lines which contain orderID counter greater than (gt) 1
  36. {
  37. "$match": {
  38. "OrderID_counter": {
  39. "$gt": 1
  40. }
  41. }
  42. },
  43. # Lookup is used to outer join the order-details table with the result from previous operations
  44. {
  45. "$lookup":
  46. {
  47. "from" : "order-details",
  48. "localField" : "_id",
  49. "foreignField" : "OrderID",
  50. "as" : "order"
  51. }
  52. },
  53. # Here unwind deconstructs the array field "order" created by the previous lookup
  54. # and outputs a document for each element in the order array
  55. { "$unwind" : "$order" },
  56. # Use project to extract the OrderID and the ProductID information for each document
  57. { "$project" : {"OrderId":"$_id", "ProductID":"$order.ProductID", "_id":0} },
  58. # Lookup is used to outer join the product table with the result from previous operations
  59. # this is done to get the product name for each product ID
  60. {
  61. "$lookup":
  62. {
  63. "from" : "products",
  64. "localField" : "ProductID",
  65. "foreignField" : "ProductID",
  66. "as" : "productinfo"
  67. }
  68. },
  69. # Here unwind deconstructs the array field "productinfo" created by the previous lookup
  70. # and outputs a document for each element in the "productinfo" array
  71. { "$unwind" : "$productinfo" },
  72. # Finally the project operation extracts only the information that is relevant for the wanted result
  73. { "$project" : {"OrderId":1, "ProductID":1, "ProductName":"$productinfo.ProductName"} },
  74. ])
  75.  
  76. # Print the result in a nice way
  77. print "Results for exercise 4.2 MongoDB"
  78. print
  79. print "{OrderID: <10} {ProductID: <12} {ProductName: <14}".format(
  80. OrderID = "Order ID",ProductID = "Product ID", ProductName = "Product Name")
  81. print "----------------------------------------"
  82. counter = 0
  83. for order in orders:
  84. counter += 1
  85. print "{OrderID: <10} {ProductID: <12} {ProductName: <14}".format(
  86. OrderID = order['OrderId'],ProductID = order['ProductID'], ProductName = order['ProductName'].encode('utf-8'))
  87.  
  88. print
  89. print "Total number of lines: {}".format(counter)
Add Comment
Please, Sign In to add comment