Overview
For this exploration, I am going to create a new database that stores products sold by an electronic store. I am going to keep it quite simple and not go into the full product detail that a real store would have. I just want to have some records with some numbers in them, so I can explore, calculations and aggregation. I am also going to add a second collection which will consist of product reviews. It will be tied to the first by the _id of the product.
I will name the database "TechStore." The collection of products I will name "productCollection." The reviews will be stored in "reviewCollection." Here is the JavaScript for the products. I am inserting the records as part of an array. Each separate element of the array will be inserted as its own record.
[ {_id : 1, product: "IPad", price : 894.50, quantityAvailable : 13}, {_id : 2, product: "Chrome Book", price : 245.99, quantityAvailable: 23}, {_id : 3, product: "Bose Lap Top Speakers", price : 89.50, quantityAvailable : 10}, {_id : 4, product: "Blue Tooth Game Controller", price : 149.99, quantityAvailable : 3}, {_id : 5, product: "Star Wars Mouse Pad", price : 1.50, quantityAvailable : 100}, {_id : 6, product: "Dell XPS Desk Top Computer", price : 945.00, quantityAvailable : 7}, {_id : 7, product: "Microsoft Surface Pro", price : 1250.75, quantityAvailable : 9}, {_id : 8, product: "Norton Anti Virus", price : 75.50, quantityAvailable : 2}, {_id : 9, product: "Mechanical Keyboard", price : 125.50, quantityAvailable : 3}, {_id : 10, product: "Android Tablet", price : 345.23, quantityAvailable : 5} ]
First, of course I started the server.
Then I start the command center. To create the database I say "use TechStore," then I will insert into "productCollection."
Now I will paste in our records:
Now to test that they are inserted we will use the find() method.
Updates
Let's first update one of the records. Let's reduce the price of the mouse pad to $1.00. To do this you use the update() function. Here is the code:
db.productCollection.update( {_id : 5 }, { $set : {price : 1.00} } )
Here is the screenshot:
Let's check to see if it has indeed been updated.
Just as an aside, if we didn't want to see all of the columns in the return, we can specify the set of columns we wish to see as follows:
In the second set, "1" means include and "0" exclude. The _id is always included by default and that is why you have to manually exclude it if you don't want to see it.
For updates, if you want to change more than on value, you simply separate the sets by commas.
db.productCollection.update( {_id : 5 }, { $set : {price : 1.00, quantity : 90} } )
calculations and Aggregation
In MongoDB, calculations and aggregations both use Expressions. Expression are key words that start with "$." We have used a couple already such as "$gt" and "$set." To multiply two numbers you would use the "$multiply" expression and to add the "$add" expression, etc. Let's try to determine the total value of items in our collection. To do so we will multiply the price times the quantityAvailable. Here is the JavaScript:
db.productCollection.aggregate( [ {$project : { product:1, price:1, quantityAvailable:1, total : {$multiply : ["$price", "$quantityAvailable"]} } } ] )
Here is the screen shot of the results:
Let's try a couple of aggregate functions. Let's try to get the average price. It is necessary to group by null to get the average of all the records. Here is the code:
db.productCollection.aggregate( [ {$group : { _id : null, averagePrice : { $avg : "$price" } } } ] ) )
Here is the screen shot of the results:
Let's try to find the highest price. We will use the "$max" operator. Here is the code:
db.productCollection.aggregate( [ {$group : { _id : null, highestPrice : { $max : "$price" } } } ] )
We could, of course, do much more with aggregation, but this is enough for the gist.
A Second Collection
Finally, I am going to add a second collection to the database that will contain reviews of our products. The records are a little more complex. They include a reviewer object inside the review.
{ _id : 1, product_id : 2, rating : 3.5, reviewDate: "7/24/2016", title: "Good if the web is all you need", review : "Chrome books are cheap and they work well as long as you have a good connection and as long as you can do everything you need to do on the web. This is easier than it used to be because of all the cloud storage and cloud apps. Still, it is a little disconcerting to not have files on one's own machine.", reviewer : { userName : "donald", email : "donald@gmail.com" } }
The "product_id" field relates to the "_id" field of the product. If we want to see all the reviews for Chrome books, we search for all with the "{product_id : 2 }."
I will insert this record. Next time I hope to create an application that talks to this database and allows a user to look at products and reviews and add their own.
No comments:
Post a Comment