Last active
August 29, 2015 14:03
-
-
Save saintc0d3r/260942723fbb73868ce8 to your computer and use it in GitHub Desktop.
[DRAFT] CRUD (Insert-Find-Update-Remove) in mongodb (using Mongo Shell)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's say we have a store database | |
use store | |
// And we want to use store to keep our products information | |
db.products.insert({'brand': 'Intel', 'category':'CPUs-Processors-Desktop', 'description':'Intel Core i7-4960X Ivy Bridge-E 6-Core 3.6GHz (Turbo 4GHz) LGA 2011 130W Desktop Processor BX80633i74960X', 'price': 1049.99, 'quantity':12, 'last_updated': new Date(2014,06,26) }) | |
db.products.insert({'brand': 'ASUS', 'category':'Intel Motherboards', 'description':'ASUS Sabertooth X79 LGA 2011 Intel X79 SATA 6Gb/s USB 3.0 ATX Intel Motherboard', 'price': 314.99, 'quantity':24, 'last_updated': new Date(2014,04,20) }) | |
db.products.insert({'brand': 'Corsair', 'category':'Desktop Memory', 'description':'CORSAIR Dominator Platinum 16GB (2 x 8GB) 240-Pin DDR3 SDRAM DDR3 2400 Desktop Memory Model CMD16GX3M2A2400C10', 'price': 274.99, 'quantity':120, 'last_updated': new Date(2014,06,29) }) | |
db.products.insert({'brand': 'Zotac', 'category':'Desktop Graphics Card', 'description':'ZOTAC ZT-70301-10P GeForce GTX 770 2GB 256-Bit GDDR5 PCI Express 3.0 Video Card', 'price': 371.90, 'quantity':60, 'last_updated': new Date(2014,06,27) }) | |
db.products.insert({'brand': 'Gigabyte', 'category':'Desktop Graphics Card', 'description':'GIGABYTE GV-N770OC-4GD GeForce GTX 770 4GB 256-Bit GDDR5 PCI Express 3.0 HDCP Ready WindForce 3X 450W Video Card', 'price': 369.99, 'quantity':30, 'last_updated': new Date(2014,06,29) }) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// findOne => Find one document in the collection | |
// Let's say, we have a store database which was created in insert.js. | |
// We want to get a product document in the products collection | |
db.products.findOne() | |
// You should see the query returns JSON Document for 'Intel Core i7-4960X' | |
// Next, we want to get a product document where brand = Zotac | |
// Solution: findOne's 1st argument takes object to define searching's criteria | |
// SQL Equivalent: select * from products where brand = 'Zotac' | |
var query = {'brand': 'Zotac'} | |
db.products.findOne(query) | |
// We should see the JSON document where 'brand' field's value is 'Zotac' | |
// Supposed, we don't want to get _id, last_updated & category fields from the prior query's returned result. | |
// Solution: the findOne's 2nd argument take object called 'projection'. | |
// This object is used to specify which fields should not be included in the query result & which field(s) that should be. | |
var projection = {'_id':0, 'last_updated':0, 'category':0} | |
db.products.findOne(query, projection) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using the products collection, we want to return all of the documents instead of just one document using findOne. | |
// Solution: use 'find' to achieve this goal. | |
db.products.find() | |
// Tips: To prettify the returned result of find Query, call pretty() after the find() | |
db.products.find().pretty() | |
// Same as findOne, find's 1st argument takes object to define searchiing's criteria. | |
// And the 2nd argument takes object that is used to specify which fields should not be included in the query's result & which field(s) that should be taken in. | |
var query = {'category': 'Desktop Graphics Card'} | |
var projection = {'_id':0, 'last_updated':0, 'category':0} | |
db.products.find(query, projection) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on the prior products collection, we want to get a list of documents where the price is greater than $300.00 | |
// Solution: Use $gt in the query object to specify such that criteria | |
var query={'price': {$gt: 300.00}} | |
db.products.find(query).pretty() | |
// I want to get a list of products where the price range is between $300-$400 | |
// Solution: use $lt along with $gt | |
query.price = {$gt:300.00, $lt: 400.00} | |
db.products.find(query).pretty() | |
// beside $gt & $lt, there are also $gte & $lte which represent greater than equal & less than equal criteria | |
query.quantity = {$gte:24} | |
db.products.find(query).pretty() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Supposed, we want to get document(s) from products collection where the description field contain 'i7' | |
// Solution: Use $regex as the find's query | |
var query = {'description': {$regex: 'i7'}} | |
db.products.find(query).pretty() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment