Categories: MongoDB

MongoDB Hello World Example

<p>In this tutorial we will take first step to write Hello World Example in the MongoDB&period; Here a quick guide to show you how to do basic operations like create&comma; update&comma; find&comma; delete record and indexing in MongoDB&period; This example is using MongoDB 2&period;2&period;2&comma; running on Window 7 OS 64 bit&comma; both MongoDB client and server console are run on localhost&comma; same machine&period;<&sol;p>&NewLine;<h2><b>1&period; Install MongoDB-<&sol;b><&sol;h2>&NewLine;<p>Just after downloading the MongoDB zip from its official site and unzip these file and locate to the c&colon;&sol;mongodb&sol; folder&period; Open command prompt and change directory to the c&colon;&sol;mongodb&sol;bin&sol;<br &sol;>&NewLine;and run the following command<&sol;p>&NewLine;<p><b>c&colon;&sol;mongodb&sol;bin&gt&semi;mongod<&sol;b><&sol;p>&NewLine;<p>then you will get the following screen<br &sol;>&NewLine;<img class&equals;"aligncenter" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;01&sol;mongodbcmd&period;png" border&equals;"0" &sol;><&sol;p>&NewLine;<h2><b>2&period; Connect MongoDB-<&sol;b><&sol;h2>&NewLine;<p>To connect MongoDB&comma; uses<&sol;p>&NewLine;<p><b>c&colon;&sol;mongodb&sol;bin&gt&semi;mongo<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; left&semi; float&colon; left&semi; margin-bottom&colon; 1em&semi; margin-right&colon; 1em&semi; text-align&colon; center&semi;"><img class&equals;"aligncenter" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;01&sol;mongo&period;png" width&equals;"400" height&equals;"127" border&equals;"0" &sol;><&sol;div>&NewLine;<h2><b>3&period; Create a database or table-<&sol;b><&sol;h2>&NewLine;<p>In MongoDB&comma; both database and table are created automatically when the first time data is inserted&period; Uses use database-name&comma; to switch to your database &lpar;even this is not created yet&rpar;&period;<&sol;p>&NewLine;<p>In below example&comma; after you inserted a single record&comma; database &&num;8220&semi;dineshonjavaDB&&num;8221&semi;&comma; and table &&num;8220&semi;employees&&num;8221&semi; are created on the fly&period;<&sol;p>&NewLine;<pre class&equals;"highlight">C&colon;mongodbbin&gt&semi;mongo &NewLine;MongoDB shell version&colon; 2&period;2&period;1 &NewLine;connecting to&colon; test &NewLine;&gt&semi; &NewLine;&gt&semi; use dineshonjavaDB &NewLine;switched to db dineshonjavaDB &NewLine;&gt&semi; db&period;employees&period;insert&lpar;&lbrace;empName&colon;"Dinesh"&comma; age&colon;"26"&comma; salary&colon;"50000"&rcub;&rpar; &NewLine;&gt&semi; db&period;employees&period;find&lpar;&rpar; &NewLine;&lbrace; "&lowbar;id" &colon; ObjectId&lpar;"5103cc6d9246bd2515d07374"&rpar;&comma; "empName" &colon; "Dinesh"&comma; "age" &colon; "2 &NewLine;6"&comma; "salary" &colon; "50000" &rcub; &NewLine;&gt&semi; &NewLine;<&sol;pre>&NewLine;<p><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;01&sol;heelo&period;png" width&equals;"600" height&equals;"233" border&equals;"0" &sol;><br &sol;>&NewLine;Three database commands&period;<&sol;p>&NewLine;<ol>&NewLine;<li><b>show dbs<&sol;b> – List all databases&period;<&sol;li>&NewLine;<li><b>use db&lowbar;name <&sol;b>– Switches to db&lowbar;name&period;<&sol;li>&NewLine;<li><b>show collections<&sol;b> – List all tables in the current selected database&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p><b>NOTE&colon; <&sol;b>In <b>MongoDB<&sol;b>&comma; <b>collection<&sol;b> means <b>table<&sol;b> in SQL&period;<&sol;p>&NewLine;<h2><b>4&period; Insert A Record-<&sol;b><&sol;h2>&NewLine;<p>To insert a record&comma;<br &sol;>&NewLine;uses db&period;tablename&period;insert&lpar;&lbrace;data&rcub;&rpar;<br &sol;>&NewLine;or db&period;tablename&period;save&lpar;&lbrace;data&rcub;&rpar;<&sol;p>&NewLine;<pre class&equals;"highlight">&gt&semi; db&period;employees&period;save&lpar;&lbrace;empName&colon;"Sweety"&comma; age&colon;"23"&comma;salary&colon;"30000"&rcub;&rpar; &NewLine;&gt&semi; db&period;employees&period;find&lpar;&rpar; &NewLine;&lbrace; "&lowbar;id" &colon; ObjectId&lpar;"5103cc6d9246bd2515d07374"&rpar;&comma; "empName" &colon; "Dinesh"&comma; "age" &colon; "2 &NewLine;6"&comma; "salary" &colon; "50000" &rcub; &NewLine;&lbrace; "&lowbar;id" &colon; ObjectId&lpar;"5103cee89246bd2515d07375"&rpar;&comma; "empName" &colon; "Sweety"&comma; "age" &colon; "2 &NewLine;3"&comma; "salary" &colon; "30000" &rcub; &NewLine;&gt&semi; &NewLine;<&sol;pre>&NewLine;<h2><b>5&period; Update A Record-<&sol;b><&sol;h2>&NewLine;<p>To update a record&comma; uses <b>db&period;tablename&period;update&lpar;&lbrace;criteria&rcub;&comma;&lbrace;&dollar;set&colon; &lbrace;new value&rcub;&rcub;&rpar;<&sol;b>&period;<br &sol;>&NewLine;In below example&comma; the salary of empName&colon; &&num;8220&semi;Dinesh&&num;8221&semi; is updated&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&gt&semi; db&period;employees&period;update&lpar;&lbrace;empName&colon;"Dinesh"&rcub;&comma;&lbrace;&dollar;set&colon;&lbrace;salary&colon;"70000"&rcub;&rcub;&rpar; &NewLine;&gt&semi; db&period;employees&period;find&lpar;&rpar; &NewLine;&lbrace; "&lowbar;id" &colon; ObjectId&lpar;"5103cc6d9246bd2515d07374"&rpar;&comma; "empName" &colon; "Dinesh"&comma; "age" &colon; "2 &NewLine;6"&comma; "salary" &colon; "70000" &rcub; &NewLine;&lbrace; "&lowbar;id" &colon; ObjectId&lpar;"5103cee89246bd2515d07375"&rpar;&comma; "empName" &colon; "Sweety"&comma; "age" &colon; "2 &NewLine;3"&comma; "salary" &colon; "30000" &rcub; &NewLine;&gt&semi; &NewLine;<&sol;pre>&NewLine;<h2><b>6&period; Find Records-<&sol;b><&sol;h2>&NewLine;<p>To find or query records&comma; uses <b>db&period;tablename&period;find&lpar;&lbrace;criteria&rcub;&rpar;&period;<br &sol;>&NewLine;<&sol;b><br &sol;>&NewLine;List all records from table &&num;8220&semi;employees&&num;8221&semi;&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&gt&semi; db&period;employees&period;find&lpar;&rpar; &NewLine;&lbrace; "&lowbar;id" &colon; ObjectId&lpar;"5103cc6d9246bd2515d07374"&rpar;&comma; "empName" &colon; "Dinesh"&comma; "age" &colon; "2 &NewLine;6"&comma; "salary" &colon; "70000" &rcub; &NewLine;&lbrace; "&lowbar;id" &colon; ObjectId&lpar;"5103cee89246bd2515d07375"&rpar;&comma; "empName" &colon; "Sweety"&comma; "age" &colon; "2 &NewLine;3"&comma; "salary" &colon; "30000" &rcub; &NewLine;&gt&semi; &NewLine;<&sol;pre>&NewLine;<p>Find records where employee name is &&num;8220&semi;Dinesh&&num;8221&semi;<&sol;p>&NewLine;<pre class&equals;"highlight">&gt&semi; db&period;employees&period;find&lpar;&lbrace;empName&colon;"Dinesh"&rcub;&rpar; &NewLine;&lbrace; "&lowbar;id" &colon; ObjectId&lpar;"5103cc6d9246bd2515d07374"&rpar;&comma; "empName" &colon; "Dinesh"&comma; "age" &colon; "2 &NewLine;6"&comma; "salary" &colon; "70000" &rcub; &NewLine;&gt&semi; &NewLine;<&sol;pre>&NewLine;<h2><b>7&period; Delete Record-<&sol;b><&sol;h2>&NewLine;<p>To delete a record&comma; uses <b>db&period;tablename&period;remove&lpar;&lbrace;criteria&rcub;&rpar;<&sol;b>&period;<br &sol;>&NewLine;In below example&comma; the record of employee name &&num;8220&semi;Sweety&&num;8221&semi; is deleted&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&gt&semi; db&period;employees&period;remove&lpar;&lbrace;empName&colon;"Sweety"&rcub;&rpar; &NewLine;&gt&semi; db&period;employees&period;find&lpar;&rpar; &NewLine;&lbrace; "&lowbar;id" &colon; ObjectId&lpar;"5103cc6d9246bd2515d07374"&rpar;&comma; "empName" &colon; "Dinesh"&comma; "age" &colon; "2 &NewLine;6"&comma; "salary" &colon; "70000" &rcub; &NewLine;&gt&semi; &NewLine;<&sol;pre>&NewLine;<p><b>Note&colon;<&sol;b><br &sol;>&NewLine;To delete all records from a table&comma; uses <b>db&period;tablename&period;remove&lpar;&rpar;<&sol;b>&period;<br &sol;>&NewLine;To drop the table&comma; uses <b>db&period;tablename&period;drop&lpar;&rpar;<&sol;b>&period;<&sol;p>&NewLine;<h2><b>8&period; Indexing-<&sol;b><&sol;h2>&NewLine;<p>Index may help you increase the speed of querying data&period;<&sol;p>&NewLine;<p>List all indexes of table &&num;8220&semi;employees&&num;8221&semi;&comma; by default the column &&num;8220&semi;&lowbar;id&&num;8221&semi; is always the primary key and created automatically&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&gt&semi; db&period;employees&period;getIndexes&lpar;&rpar; &NewLine;&lbrack; &NewLine; &lbrace; &NewLine; "v" &colon; 1&comma; &NewLine; "key" &colon; &lbrace; &NewLine; "&lowbar;id" &colon; 1 &NewLine; &rcub;&comma; &NewLine; "ns" &colon; "dineshonjavaDB&period;employees"&comma; &NewLine; "name" &colon; "&lowbar;id&lowbar;" &NewLine; &rcub; &NewLine;&rsqb; &NewLine;&gt&semi; &NewLine;<&sol;pre>&NewLine;<p>To create an index&comma; uses <b>db&period;tablename&period;ensureIndex&lpar;column&rpar;<&sol;b>&period;<br &sol;>&NewLine;In below example&comma; an index is created on column &&num;8220&semi;empName&&num;8221&semi;&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&gt&semi; db&period;employees&period;ensureIndex&lpar;&lbrace;empName&colon;1&rcub;&rpar; &NewLine;&gt&semi; db&period;employees&period;getIndexes&lpar;&rpar; &NewLine;&lbrack; &NewLine; &lbrace; &NewLine; "v" &colon; 1&comma; &NewLine; "key" &colon; &lbrace; &NewLine; "&lowbar;id" &colon; 1 &NewLine; &rcub;&comma; &NewLine; "ns" &colon; "dineshonjavaDB&period;employees"&comma; &NewLine; "name" &colon; "&lowbar;id&lowbar;" &NewLine; &rcub;&comma; &NewLine; &lbrace; &NewLine; "v" &colon; 1&comma; &NewLine; "key" &colon; &lbrace; &NewLine; "empName" &colon; 1 &NewLine; &rcub;&comma; &NewLine; "ns" &colon; "dineshonjavaDB&period;employees"&comma; &NewLine; "name" &colon; "empName&lowbar;1" &NewLine; &rcub; &NewLine;&rsqb; &NewLine;&gt&semi; &NewLine;<&sol;pre>&NewLine;<p>To drop an index&comma; uses <b>db&period;tablename&period;dropIndex&lpar;column&rpar;<&sol;b>&period;<br &sol;>&NewLine;In below example&comma; the index on column &&num;8220&semi;empName&&num;8221&semi; is deleted or dropped&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&gt&semi; db&period;employees&period;dropIndex&lpar;&lbrace;empName&colon;1&rcub;&rpar; &NewLine;&lbrace; "nIndexesWas" &colon; 2&comma; "ok" &colon; 1 &rcub; &NewLine;&gt&semi; db&period;employees&period;getIndexes&lpar;&rpar; &NewLine;&lbrack; &NewLine; &lbrace; &NewLine; "v" &colon; 1&comma; &NewLine; "key" &colon; &lbrace; &NewLine; "&lowbar;id" &colon; 1 &NewLine; &rcub;&comma; &NewLine; "ns" &colon; "dineshonjavaDB&period;employees"&comma; &NewLine; "name" &colon; "&lowbar;id&lowbar;" &NewLine; &rcub; &NewLine;&rsqb; &NewLine;&gt&semi; &NewLine;<&sol;pre>&NewLine;<p>To create an unique index&comma; uses <b>db&period;tablename&period;ensureIndex&lpar;&lbrace;column&rcub;&comma;&lbrace;unique&colon;true&rcub;&rpar;<&sol;b>&period;<br &sol;>&NewLine;In below example&comma; an unique index is created on column &&num;8220&semi;empName&&num;8221&semi;&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&gt&semi; db&period;employees&period;ensureIndex&lpar;&lbrace;empName&colon;1&rcub;&comma;&lbrace;unique&colon;true&rcub;&rpar;&semi; &NewLine;&gt&semi; db&period;employees&period;getIndexes&lpar;&rpar; &NewLine;&lbrack; &NewLine; &lbrace; &NewLine; "v" &colon; 1&comma; &NewLine; "key" &colon; &lbrace; &NewLine; "&lowbar;id" &colon; 1 &NewLine; &rcub;&comma; &NewLine; "ns" &colon; "dineshonjavaDB&period;employees"&comma; &NewLine; "name" &colon; "&lowbar;id&lowbar;" &NewLine; &rcub;&comma; &NewLine; &lbrace; &NewLine; "v" &colon; 1&comma; &NewLine; "key" &colon; &lbrace; &NewLine; "empName" &colon; 1 &NewLine; &rcub;&comma; &NewLine; "unique" &colon; true&comma; &NewLine; "ns" &colon; "dineshonjavaDB&period;employees"&comma; &NewLine; "name" &colon; "empName&lowbar;1" &NewLine; &rcub; &NewLine;&rsqb; &NewLine;&gt&semi; &NewLine;<&sol;pre>&NewLine;<h2>10&period; Help-<&sol;h2>&NewLine;<p>At last&comma; uses help&lpar;&rpar; to guide you how to do things in MongoDB&period;<br &sol;>&NewLine;<b>help –<&sol;b> All available commands&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&gt&semi; help &NewLine; db&period;help&lpar;&rpar; help on db methods &NewLine; db&period;mycoll&period;help&lpar;&rpar; help on collection methods &NewLine; sh&period;help&lpar;&rpar; sharding helpers &NewLine; rs&period;help&lpar;&rpar; replica set helpers &NewLine; help admin administrative help &NewLine; help connect connecting to a db help &NewLine; help keys key shortcuts &NewLine; help misc misc things to know &NewLine; help mr mapreduce &NewLine; &NewLine; show dbs show database names &NewLine; show collections show collections in current database &NewLine; show users show users in current database &NewLine; show profile show most recent system&period;profile entries wit &NewLine;h time &gt&semi;&equals; 1ms &NewLine; show logs show the accessible logger names &NewLine; show log &lbrack;name&rsqb; prints out the last segment of log in memor &NewLine;y&comma; 'global' is default &NewLine; use set current database &NewLine; db&period;foo&period;find&lpar;&rpar; list objects in collection foo &NewLine; db&period;foo&period;find&lpar; &lbrace; a &colon; 1 &rcub; &rpar; list objects in foo where a &equals;&equals; 1 &NewLine; it result of the last line evaluated&semi; use to f &NewLine;urther iterate &NewLine; DBQuery&period;shellBatchSize &equals; x set default number of items to display on s &NewLine;hell &NewLine; exit quit the mongo shell &NewLine;&gt&semi; &NewLine;<&sol;pre>&NewLine;<p><b>db&period;help&lpar;&rpar; <&sol;b>– Shows help on db&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&gt&semi; db&period;help&lpar;&rpar; &NewLine;DB methods&colon; &NewLine; db&period;addUser&lpar;username&comma; password&lbrack;&comma; readOnly&equals;false&rsqb;&rpar; &NewLine; db&period;adminCommand&lpar;nameOrDocument&rpar; - switches to 'admin' db&comma; and runs comma &NewLine;nd &lbrack; just calls db&period;runCommand&lpar;&period;&period;&period;&rpar; &rsqb; &NewLine; db&period;auth&lpar;username&comma; password&rpar; &NewLine; db&period;cloneDatabase&lpar;fromhost&rpar; &NewLine; db&period;commandHelp&lpar;name&rpar; returns the help for the command &NewLine; db&period;copyDatabase&lpar;fromdb&comma; todb&comma; fromhost&rpar; &NewLine; db&period;createCollection&lpar;name&comma; &lbrace; size &colon; &period;&period;&period;&comma; capped &colon; &period;&period;&period;&comma; max &colon; &period;&period;&period; &rcub; &rpar; &NewLine; db&period;currentOp&lpar;&rpar; displays currently executing operations in the db &NewLine; db&period;dropDatabase&lpar;&rpar; &NewLine; db&period;eval&lpar;func&comma; args&rpar; run code server-side &NewLine; db&period;fsyncLock&lpar;&rpar; flush data to disk and lock server for backups &NewLine; db&period;fsyncUnlock&lpar;&rpar; unlocks server following a db&period;fsyncLock&lpar;&rpar; &NewLine; db&period;getCollection&lpar;cname&rpar; same as db&lbrack;'cname'&rsqb; or db&period;cname &NewLine; db&period;getCollectionNames&lpar;&rpar; &NewLine; db&period;getLastError&lpar;&rpar; - just returns the err msg string &NewLine; db&period;getLastErrorObj&lpar;&rpar; - return full status object &NewLine; db&period;getMongo&lpar;&rpar; get the server connection object &NewLine; db&period;getMongo&lpar;&rpar;&period;setSlaveOk&lpar;&rpar; allow queries on a replication slave server &NewLine; db&period;getName&lpar;&rpar; &NewLine; db&period;getPrevError&lpar;&rpar; &NewLine; db&period;getProfilingLevel&lpar;&rpar; - deprecated &NewLine; db&period;getProfilingStatus&lpar;&rpar; - returns if profiling is on and slow threshold &NewLine; db&period;getReplicationInfo&lpar;&rpar; &NewLine; db&period;getSiblingDB&lpar;name&rpar; get the db at the same server as this one &NewLine; db&period;hostInfo&lpar;&rpar; get details about the server's host &NewLine; db&period;isMaster&lpar;&rpar; check replica primary status &NewLine; db&period;killOp&lpar;opid&rpar; kills the current operation in the db &NewLine; db&period;listCommands&lpar;&rpar; lists all the db commands &NewLine; db&period;loadServerScripts&lpar;&rpar; loads all the scripts in db&period;system&period;js &NewLine; db&period;logout&lpar;&rpar; &NewLine; db&period;printCollectionStats&lpar;&rpar; &NewLine; db&period;printReplicationInfo&lpar;&rpar; &NewLine; db&period;printShardingStatus&lpar;&rpar; &NewLine; db&period;printSlaveReplicationInfo&lpar;&rpar; &NewLine; db&period;removeUser&lpar;username&rpar; &NewLine; db&period;repairDatabase&lpar;&rpar; &NewLine; db&period;resetError&lpar;&rpar; &NewLine; db&period;runCommand&lpar;cmdObj&rpar; run a database command&period; if cmdObj is a string&comma; tu &NewLine;rns it into &lbrace; cmdObj &colon; 1 &rcub; &NewLine; db&period;serverStatus&lpar;&rpar; &NewLine; db&period;setProfilingLevel&lpar;level&comma;&rpar; 0&equals;off 1&equals;slow 2&equals;all &NewLine; db&period;setVerboseShell&lpar;flag&rpar; display extra information in shell output &NewLine; db&period;shutdownServer&lpar;&rpar; &NewLine; db&period;stats&lpar;&rpar; &NewLine; db&period;version&lpar;&rpar; current version of the server &NewLine;&gt&semi; &NewLine;<&sol;pre>&NewLine;<p><b>db&period;collection&period;help&lpar;&rpar;<&sol;b> – Shows help on collection &lpar;table&rpar;&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&gt&semi; db&period;employees&period;help&lpar;&rpar; &NewLine;DBCollection help &NewLine; db&period;employees&period;find&lpar;&rpar;&period;help&lpar;&rpar; - show DBCursor help &NewLine; db&period;employees&period;count&lpar;&rpar; &NewLine; db&period;employees&period;copyTo&lpar;newColl&rpar; - duplicates collection by copying all docu &NewLine;ments to newColl&semi; no indexes are copied&period; &NewLine; db&period;employees&period;convertToCapped&lpar;maxBytes&rpar; - calls &lbrace;convertToCapped&colon;'employe &NewLine;es'&comma; size&colon;maxBytes&rcub;&rcub; command &NewLine; db&period;employees&period;dataSize&lpar;&rpar; &NewLine; db&period;employees&period;distinct&lpar; key &rpar; - eg&period; db&period;employees&period;distinct&lpar; 'x' &rpar; &NewLine; db&period;employees&period;drop&lpar;&rpar; drop the collection &NewLine; db&period;employees&period;dropIndex&lpar;name&rpar; &NewLine; db&period;employees&period;dropIndexes&lpar;&rpar; &NewLine; db&period;employees&period;ensureIndex&lpar;keypattern&lbrack;&comma;options&rsqb;&rpar; - options is an object wi &NewLine;th these possible fields&colon; name&comma; unique&comma; dropDups &NewLine; db&period;employees&period;reIndex&lpar;&rpar; &NewLine; db&period;employees&period;find&lpar;&lbrack;query&rsqb;&comma;&lbrack;fields&rsqb;&rpar; - query is an optional query filter&period; &NewLine; fields is optional set of fields to return&period; &NewLine; e&period;g&period; db&period;employees&period;find&lpar; &lbrace;x &NewLine;&colon;77&rcub; &comma; &lbrace;name&colon;1&comma; x&colon;1&rcub; &rpar; &NewLine; db&period;employees&period;find&lpar;&period;&period;&period;&rpar;&period;count&lpar;&rpar; &NewLine; db&period;employees&period;find&lpar;&period;&period;&period;&rpar;&period;limit&lpar;n&rpar; &NewLine; db&period;employees&period;find&lpar;&period;&period;&period;&rpar;&period;skip&lpar;n&rpar; &NewLine; db&period;employees&period;find&lpar;&period;&period;&period;&rpar;&period;sort&lpar;&period;&period;&period;&rpar; &NewLine; db&period;employees&period;findOne&lpar;&lbrack;query&rsqb;&rpar; &NewLine; db&period;employees&period;findAndModify&lpar; &lbrace; update &colon; &period;&period;&period; &comma; remove &colon; bool &lbrack;&comma; query&colon; &lbrace;&rcub;&comma; &NewLine; sort&colon; &lbrace;&rcub;&comma; 'new'&colon; false&rsqb; &rcub; &rpar; &NewLine; db&period;employees&period;getDB&lpar;&rpar; get DB object associated with collection &NewLine; db&period;employees&period;getIndexes&lpar;&rpar; &NewLine; db&period;employees&period;group&lpar; &lbrace; key &colon; &period;&period;&period;&comma; initial&colon; &period;&period;&period;&comma; reduce &colon; &period;&period;&period;&lbrack;&comma; cond&colon; &period;&period;&period;&rsqb; &NewLine; &rcub; &rpar; &NewLine; db&period;employees&period;insert&lpar;obj&rpar; &NewLine; db&period;employees&period;mapReduce&lpar; mapFunction &comma; reduceFunction &comma; &NewLine; &rpar; &NewLine; db&period;employees&period;remove&lpar;query&rpar; &NewLine; db&period;employees&period;renameCollection&lpar; newName &comma; &rpar; renames the coll &NewLine;ection&period; &NewLine; db&period;employees&period;runCommand&lpar; name &comma; &rpar; runs a db command with the g &NewLine;iven name where the first param is the collection name &NewLine; db&period;employees&period;save&lpar;obj&rpar; &NewLine; db&period;employees&period;stats&lpar;&rpar; &NewLine; db&period;employees&period;storageSize&lpar;&rpar; - includes free space allocated to this colle &NewLine;ction &NewLine; db&period;employees&period;totalIndexSize&lpar;&rpar; - size in bytes of all the indexes &NewLine; db&period;employees&period;totalSize&lpar;&rpar; - storage allocated for all data and indexes &NewLine; db&period;employees&period;update&lpar;query&comma; object&lbrack;&comma; upsert&lowbar;bool&comma; multi&lowbar;bool&rsqb;&rpar; - instead &NewLine;of two flags&comma; you can pass an object with fields&colon; upsert&comma; multi &NewLine; db&period;employees&period;validate&lpar; &rpar; - SLOW &NewLine; db&period;employees&period;getShardVersion&lpar;&rpar; - only for use with sharding &NewLine; db&period;employees&period;getShardDistribution&lpar;&rpar; - prints statistics about data distr &NewLine;ibution in the cluster &NewLine; db&period;employees&period;getSplitKeysForChunks&lpar; &rpar; - calculates split &NewLine;points over all chunks and returns splitter function &NewLine;&gt&semi; &NewLine;<&sol;pre>&NewLine;<p><b> db&period;collection&period;function&period;help&lpar;&rpar;<&sol;b> – Shows help on function&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&gt&semi; db&period;employees&period;find&lpar;&rpar;&period;help&lpar;&rpar; &NewLine;find&lpar;&rpar; modifiers &NewLine; &period;sort&lpar; &lbrace;&period;&period;&period;&rcub; &rpar; &NewLine; &period;limit&lpar; n &rpar; &NewLine; &period;skip&lpar; n &rpar; &NewLine; &period;count&lpar;&rpar; - total &num; of objects matching query&comma; ignores skip&comma;limit &NewLine; &period;size&lpar;&rpar; - total &num; of objects cursor would return&comma; honors skip&comma;limit &NewLine; &period;explain&lpar;&lbrack;verbose&rsqb;&rpar; &NewLine; &period;hint&lpar;&period;&period;&period;&rpar; &NewLine; &period;addOption&lpar;n&rpar; - adds op&lowbar;query options -- see wire protocol &NewLine; &period;&lowbar;addSpecial&lpar;name&comma; value&rpar; - http&colon;&sol;&sol;dochub&period;mongodb&period;org&sol;core&sol;advancedqueri &NewLine;es&num;AdvancedQueries-Metaqueryoperators &NewLine; &period;batchSize&lpar;n&rpar; - sets the number of docs to return per getMore &NewLine; &period;showDiskLoc&lpar;&rpar; - adds a &dollar;diskLoc field to each returned object &NewLine; &period;min&lpar;idxDoc&rpar; &NewLine; &period;max&lpar;idxDoc&rpar; &NewLine; &NewLine;Cursor methods &NewLine; &period;toArray&lpar;&rpar; - iterates through docs and returns an array of the results &NewLine; &period;forEach&lpar; func &rpar; &NewLine; &period;map&lpar; func &rpar; &NewLine; &period;hasNext&lpar;&rpar; &NewLine; &period;next&lpar;&rpar; &NewLine; &period;objsLeftInBatch&lpar;&rpar; - returns count of docs left in current batch &lpar;when e &NewLine;xhausted&comma; a new getMore will be issued&rpar; &NewLine; &period;count&lpar;applySkipLimit&rpar; - runs command at server &NewLine; &period;itcount&lpar;&rpar; - iterates through documents and counts them &NewLine;&gt&semi; &NewLine;<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h4>References<&sol;h4>&NewLine;<ol>&NewLine;<li><a href&equals;"http&colon;&sol;&sol;www&period;mongodb&period;org&sol;display&sol;DOCS&sol;Tutorial" target&equals;"&lowbar;blank" rel&equals;"noopener">Official MongoDB tutorials<br &sol;>&NewLine;<&sol;a><&sol;li>&NewLine;<li><a href&equals;"http&colon;&sol;&sol;www&period;mongodb&period;org&sol;display&sol;DOCS&sol;Indexes" target&equals;"&lowbar;blank" rel&equals;"noopener">MongoDB Indexes<&sol;a><&sol;li>&NewLine;<&sol;ol>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi;">                        <b>     &lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;install-mongodb-on-windows&sol;">previous<&sol;a>&lt&semi;&lt&semi;             &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-mongodb&sol;">index  <&sol;a>&vert;&vert;         &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;create-database-or-collection-in-mongodb&sol;">next<&sol;a>&gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;install-mongodb-on-windows&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;create-database-or-collection-in-mongodb&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '595'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago