This file contains hidden or 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
// Suppose, we have 10000000 documents stored in bar collections (foo database) | |
use foo | |
for(var i = 0; i< 10000000; i++)(db.bar.insert({'a':i, 'b':i, 'c':i})) | |
// Let's pretend that we have an application that accesses foo.bar and perform this query | |
db.bar.find({'a':506785}) | |
// Now, we want to peek how long did the query finish its job through peeking the mongod's log | |
// Focus on the command line box that runs mongod. You should notice a line that similar to mine at below: | |
// 2014-06-30T04:13:08.193+0800 [conn1] query foobar.foo query: { a: 555666.0 } planSummary: COLLSCAN |
This file contains hidden or 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
// There are 3 levels profilings in mongodb: Lvl 0, 1 & 2. | |
// Level 0: The profiler is turned off. | |
// Level 1: The profiler would log slow queries | |
// Level 2: The profiler would log all queries (good choice for development phase) | |
// The profiling's settings could be enabled by suppling certain parameters when starting mongod service. | |
// Here's the example: I want to start mongod & also enable level 1 profiling where it would log any queries that took 10 ms & more | |
mongod --profile 1 --slowms 10 | |
// Suppose, we have 10000000 documents stored in bar collections (foo database) |
This file contains hidden or 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 where the database has products collection with several documents in it | |
use store | |
db.products.insert({'sku': '119-4567-7895', 'price':359.99, 'description': 'Intel Core i7-4770K Haswell Quad-Core 3.5GHz LGA 1150 84W Desktop Processor Intel HD Graphics BX80646I74770K', 'category':'CPU-Processors-Desktops', 'brand': 'Intel', 'reviews': [{'author': 'Mr.T', 'pros':"I play video game and I do video editiing. When it's time to compress movie and work on it, it's really fast.", 'cons': "The temperature is on the high side, but I think it's the way they run."}]}) | |
db.products.insert({'sku': '121-6478-7234', 'price':374.99, 'description': 'ASUS GTX770-DC2OC-2GD5 GeForce GTX 770 2GB 256-Bit GDDR5 PCI Express 3.0 HDCP Ready SLI Support Video Card', 'category': 'Desktop Graphics Cards','brand': 'ASUS', 'reviews': [{'author': 'Slyder', 'pros':"Plug and play super fast works great so far nothing wrong with the way it's been working", 'cons':"Nothing yet"}]}) | |
db.products.insert({'sku': ' |
This file contains hidden or 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 |
This file contains hidden or 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
// We use the latest version of store database as shown in 9_aggregate_double_grouping.js | |
// Let's say , we want to reshape products list as follow: | |
// _id -------------> <excluded> | |
// manufacturer ----> maker <lower case> | |
// name ------------> item | |
// category & price-> details: -> Category, price, discounted_price <Discounted price by 10%> | |
// Also, the outputs are sorted by maker field | |
db.products.aggregate([ | |
{ | |
$project: { |
This file contains hidden or 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 | |
db = connect("localhost:27017/store"); | |
var collectionNames = db.getCollectionNames(); | |
for(var i=0; i < collectionNames.length; i++){ | |
if (collectionNames[i] == "products"){ | |
print("[INFO] - Products collection is exists. Dropping it now."); | |
db.products.drop(); | |
break; | |
} |
This file contains hidden or 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
# NOTES: Adjust replSet, dbpath, port parameters as you see them fit on your system environments. | |
# Creates data directories for 3 mongod servers | |
mkdir -p /data/rs1 /data/rs2 /data/rs3 | |
# Creates 3 mongod servers in local machine | |
mongod --replSet rs1 --logpath "node-1.log" --dbpath /data/rs1 --port 27018 --fork | |
mongod --replSet rs1 --logpath "node-2.log" --dbpath /data/rs2 --port 27019 --fork | |
mongod --replSet rs1 --logpath "node-3.log" --dbpath /data/rs3 --port 27020 --fork |
This file contains hidden or 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
/** | |
* Simple code to demonstrate CRUD function on a document against an replica set. | |
* Run this command in this source code's directory to install required dependency: npm install mongodb | |
*/ | |
var MongoClient = require('mongodb').MongoClient; | |
var assert = require('assert'); | |
// Notice that the difference of connection string for connecting to a replica set and a single mongodb server, | |
var connection_string = "mongodb://127.0.0.1:27018, 127.0.0.1:27019, 127.0.0.1:27020/restaurant"; |
This file contains hidden or 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
/** | |
* A sample demo to demonstrate Mongodb Replica Set's failover capability. | |
* Instructions: | |
* 1. Run your replica set servers. | |
* 2. Run this app (note: adjust the replica set's hosts & ports as you see them fit in your environments.) | |
* 3. Shutdown your replica set's primary node. | |
* 4. Watch the outputs of this running app. Confirm that the inserts are defered ( no errors being throws) when the primary node is downed and | |
* then come up again after a new primary node is elected in the replica set. | |
*/ |
This file contains hidden or 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
/** | |
* Simple code to demonstrate write concerns setting when inserting documents into replica set. | |
* Run this command to install required dependency, in this source code's directory: npm install mongodb | |
*/ | |
var MongoClient = require('mongodb').MongoClient; | |
var assert = require('assert'); | |
// Notice the url query string in the below connection string to replica set. We define write concern = 1 as the default write concern setting. | |
// w=1 means, the driver would returns success once a document has been successfully inserted into the Primary Node. |