3 \begin{enumerate}[label=(\alph*)]
4 \item\textbf{New data model}\\
5 It only makes sense to merge books and editions, as everything else would ultimately lead to too many redundancies, instead of a reduction of complexity.
7 \begin{lstlisting}[style=command]
12 "year":"...", - from edition
13 "isbn":"...", - from edition
14 "owned":NumberInt(...) - from edition
24 "book":ObjectId("..."),
25 "from":ISODate("yyyy-mm-dd"),
26 "to":ISODate("yyyy-mm-dd") or "to":null
30 \item\textbf{Queries}\\
31 The ''inserts'' of our data model are in \textbf{vagrant\_provision.sh}.\\
32 It is safe to re-run the following commands.\\
33 They will reset and re-populate the mongodb every time.
34 \begin{lstlisting}[style=command]
36 $ vagrant up --provision
41 \begin{enumerate}[label=(\roman*)]
42 \item\textbf{Listing all people who have borrowed a book for more than two weeks and not
44 \begin{lstlisting}[style=command]
45 mongo adbs --quiet --eval 'db.borrowings.find({$and:[ {"from":{ $lt: new Date(new Date().getTime()-(1000*3600*24*14)) }}, {"to":null}]}, {who:1, _id:0})'
47 \item\textbf{Asking if any edition of a book is currently available to be borrowed.}
48 \begin{lstlisting}[style=command]
49 mongo adbs --quiet --eval 'db.books.aggregate([
50 {$match:{"name":"Fundamentals of Database Systems"}}
52 {$lookup:{"from": "borrowings", "localField": "_id", "foreignField": "book", as: "borrowing"}}
54 {$project:{"name":1,"author":1,"year":1,"isbn":1,"owned":1,_id:1,
55 // "borrowing": {$filter:{input: "$borrowing", as: "borrow", cond: { $eq: [ "$$borrow.to", null ] } }},
56 "borrowing_size": {$size:{$filter:{input: "$borrowing", as: "borrow", cond: { $eq: [ "$$borrow.to", null ] } }}}
59 {$project:{"name":1,"author":1,"year":1,"isbn":1,_id:1,
60 "borrow_avail": {$subtract: ["$owned", "$borrowing_size"] },
61 // "owned":1, "borrowing": 1, "borrowing_size": 1
64 {$match:{"borrow_avail":{$gt:0}}}