Download NoSQL database for classified dverts

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Microsoft Jet Database Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

Versant Object Database wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
A NoSQL database for classified Adverts
Imagine you want to build a database to hold the details of classified adverts. A simple relational
model might have a table to hold items with fields for Classification, Description, Price, etc. The
problem is that different products have different qualities. Cars have model and mileage, clothes
have size and brand, etc. You can’t have one big table with every possible quality. You could have a
table per classification, but that means a lot of work every time a new classification is added, plus
the queries would all need to be re-written to look in the newly created tables.
An alternative solution is to use a MongoDB document store. Each advert would be a document in
its own right, with whatever qualities were needed for the product it represented.
For example:
{"Manufacturer":"Skoda","Colour":"White","Model":"Octavia","Milage":
59083,"Price":1636,"Classification":"Motor Cars"}
In this practical, we will build such a system. The data is already in JSON format for you, to save you
the trouble of transforming it. The first thing to do is get it into your database. There are two files
you can download from the course practicals web page. They are cars.json and clothes.json. They
are both in JSON array format.
http://docs.mongodb.org/manual/core/import-export/
Has the information you need to read the file with more information here:
http://docs.mongodb.org/manual/reference/program/mongoimport/ (read about --jsonArray).
You could also just import it using uMongo if you like. Make sure you import BOTH files into the
SAME collection.
Using the MongoDB client command line, we will now write some queries to access the data.
1. Write a query to return all of the Motor Cars for sale
This is a simple find()
2. Write a query to return all of the Motor Cars with prices over £2000
See http://docs.mongodb.org/manual/reference/operator/query-comparison/
3. Write a query to return just the price of all of the Red Skodas
Add a term to the query to retrieve ONLY the price field
4. Write a query to list all of the possible unique classifications
This will help http://docs.mongodb.org/manual/reference/method/db.collection.distinct/
5. Find the most expensive car. You can do it four ways, try all four:
a. Select price, sort, limit returned results to one document
This is perhaps the most straight forward, but is it the most efficient?
b. Use the group() function
This will involve a Javascript reduce function to find the max from the array of value
it receives, see
http://docs.mongodb.org/manual/reference/method/db.collection.group/
c. Use Map reduce
See http://docs.mongodb.org/manual/core/map-reduce/
d. Use the aggregation function
Use the aggregation pipeline to $match Motor Cars, $group by classification and use
$max to find the maximum
6. Write a query to list every possible field across all classifications
Use map reduce. Map emits the field name (key) and null. Reduce does nothing. Why does
this work? You can improve it further by putting the results of the map reduce into a new
collection and doing a distinct select in it.
7. Write a query to list all of the possible fields to be filled in the Clothing classification
Same as above, but add a selection on Classification.
You will find this Stackoverflow question of interest for the 2 questions above.
http://stackoverflow.com/questions/2298870/mongodb-get-names-of-all-keys-in-collection
8. The data doesn’t tell us whether or not an object has been sold. Write a query to update the
database so that the Blue Trousers with a price of 34.8 have been sold
9. Now write a query to list all Clothing that has not been sold
http://docs.mongodb.org/manual/reference/operator/query/ne/