-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathA5T8MongoDB.py
30 lines (27 loc) · 964 Bytes
/
A5T8MongoDB.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 291 A5 Task 8
# Written by Bowen Xiao
# Given a listing_id at run-time (e.g., using command line prompt or via an application parameter) find the host_name, rental_price and the most recent review for that listing.
from pymongo import MongoClient
import sys
print("Task 8")
# open db
c = MongoClient()
db = c.A5db
collection = db.listings
# useing command line prompt or via an application parameter
if len(sys.argv) > 1:
myinput = int(sys.argv[1])
else:
myinput = int(input("Please enter a listing id: "))
lists = collection.aggregate([
{"$lookup": {"from": "reviews", "localField": "id",
"foreignField": "listing_id", "as": "list_view"}},
{"$match": {"id": myinput}},
{"$project": {"_id": 0, "host_name": 1, "price": 1,
"list_view.date": 1, "list_view.comments": 1}},
{"$unwind": "$list_view"},
{"$sort": {"list_view.date": -1}},
{"$limit": 1}])
# print result
for i in lists:
print(i)