{"id":535,"date":"2017-01-29T11:22:18","date_gmt":"2017-01-29T09:22:18","guid":{"rendered":"http:\/\/www.extradrm.com\/?p=535"},"modified":"2018-12-04T13:10:27","modified_gmt":"2018-12-04T11:10:27","slug":"mongodb-mini-user-guide","status":"publish","type":"post","link":"https:\/\/www.extradrm.com\/?p=535","title":{"rendered":"MongoDB Mini User Guide"},"content":{"rendered":"<p><span style=\"text-decoration: underline;\"><strong>Shell Mongo :<br \/>\n<\/strong><\/span>&gt; mongo<span style=\"text-decoration: underline;\"><strong><br \/>\n<\/strong><\/span><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Once you are in terminal\/command line, access the database\/collection you want to use as follows:<\/strong><\/span><\/p>\n<pre><code>show dbs\r\nuse &lt;db name&gt;\r\nshow collections<\/code><\/pre>\n<p>choose your collection and type the following to <strong><span style=\"text-decoration: underline;\">see all contents of that collection<\/span><\/strong>:<\/p>\n<pre><code>db.collectionName.find()\r\n<\/code><\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>for a query below some samples :<\/strong><\/span><br \/>\n<code><\/code><\/p>\n<pre>db.products.find({ \"_id\": \"apples\", \"qty\": 5 }<\/pre>\n<pre>db.products.find( { qty: { $gt: 25 } } )<\/pre>\n<pre>db.students.find( { score: { $gt: 0, $lt: 2 } } )<\/pre>\n<pre>db.bios.find( { _id: 5 } )<\/pre>\n<pre>db.bios.find( { contribs: \"UNIX\" } ) &gt;&gt;&gt;&gt;&gt;&gt; Array elements<\/pre>\n<p>Check this link for more : <a href=\"https:\/\/docs.mongodb.com\/manual\/reference\/method\/db.collection.find\/\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.mongodb.com\/manual\/reference\/method\/db.collection.find\/<\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong> Import \/ Export command in MongoDB (Unicode files)(json)<\/strong><\/span><\/p>\n<pre>mongoimport -d comedy -c cartoons -file collections.json\r\nmongoexport -d comedy -c cartoons -o collections.json<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Change collection name in mongodb<\/strong><\/span><\/p>\n<pre>db.oldname.renameCollection(\"newname\")<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Change type fields :<\/strong><\/span><\/p>\n<pre>\/\/ String to Integer for notice_id\r\ndb.mycollection.find({notice_id : {$exists : true}}).forEach( function(obj) { obj.notice_id = new NumberInt(obj.notice_id); db.mycollection.save(obj); } );<\/pre>\n<pre>\/\/ Integer to String for field-name\r\ndb.db-name.find({field-name : {$exists : true}}).forEach( function(obj) { obj.field-name = \"\"+obj.field-name; db.db-name.save(obj); } );<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Rename a field in mongoDB :<br \/>\n<\/strong><\/span><\/p>\n<pre>db.mycollection.update ( {}, { $rename : { \"notice_id\" : \"isn\" }}, false, true );<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Delete a collection<\/strong><\/span><\/p>\n<pre>db.compta.drop();<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Empty a collection<\/strong><\/span><\/p>\n<pre>db.compta.remove();<\/pre>\n<p>Under the Mongodb shell please import the follwing json file (test.json) listed below with :<\/p>\n<pre><strong>mongoimport -d test -c compta -file d:test.json<\/strong><\/pre>\n<p>{ &#8220;montant&#8221; : 3.95, &#8220;travel&#8221; : &#8220;Prague&#8221;, &#8220;sens&#8221; : &#8220;credit&#8221; }<br \/>\n{ &#8220;montant&#8221; : 6.03, &#8220;travel&#8221; : &#8220;Prague&#8221;, &#8220;sens&#8221; : &#8220;credit&#8221; }<br \/>\n{ &#8220;montant&#8221; : 5.53, &#8220;travel&#8221; : &#8220;New York&#8221;, &#8220;sens&#8221; : &#8220;credit&#8221; }<br \/>\n{ &#8220;montant&#8221; : -1.96, &#8220;travel&#8221; : &#8220;New York&#8221;, &#8220;sens&#8221; : &#8220;debit&#8221; }<br \/>\n{ &#8220;montant&#8221; : 3.95, &#8220;tickets&#8221; : &#8220;eurodisney&#8221;, &#8220;sens&#8221; : &#8220;credit&#8221; }<br \/>\n{ &#8220;montant&#8221; : 6.03, &#8220;tickets&#8221; : &#8220;asterix&#8221;, &#8220;sens&#8221; : &#8220;credit&#8221; }<\/p>\n<p>Mongo Request Sum or Total aggregate :<\/p>\n<pre>db.compta.aggregate({$group:{\r\n_id:\"\",\r\nmontant: {$sum: \"$montant\"}}\r\n}, {$project:{\r\n_id:0,\r\nmontant: \"$montant\"}});\r\n\r\ndb.compta.aggregate({$group:{\r\n_id:\"$travel\",\r\nmontant: {$sum: \"$montant\"}}\r\n}, {$project:{\r\n_id:1,\r\nmontant: \"$montant\"}});\r\n\r\n&lt;br?&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Shell Mongo : &gt; mongo Once you are in terminal\/command line, access the database\/collection you want to use as follows: show dbs use &lt;db name&gt; show collections choose your collection and type the following&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":2845,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[351,352,110,143],"youtube_video":null,"_links":{"self":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/535"}],"collection":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=535"}],"version-history":[{"count":0,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/535\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/media\/2845"}],"wp:attachment":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}