{"id":1749,"date":"2017-11-23T00:06:33","date_gmt":"2017-11-23T00:06:33","guid":{"rendered":"http:\/\/www.incredigeek.com\/home\/?p=1749"},"modified":"2022-02-17T07:20:07","modified_gmt":"2022-02-17T13:20:07","slug":"basic-mongodb-commands","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/basic-mongodb-commands\/","title":{"rendered":"Basic MongoDB commands"},"content":{"rendered":"<p>Connect to Mongo database<\/p>\n<pre>mongo<\/pre>\n<p>Connect to Mongo on a different port (May be needed.&nbsp; Port is for UniFi server)<\/p>\n<pre>mongo -port 27117<\/pre>\n<p>Show Databases<\/p>\n<pre>show dbs<\/pre>\n<p>Use database<\/p>\n<pre>use dbname<\/pre>\n<p>Show tables\/collections<\/p>\n<pre>show collections<\/pre>\n<p>or<\/p>\n<pre>show tables<\/pre>\n<p>List contents of table\/collection<\/p>\n<pre>db.collection.find()<\/pre>\n<p>Example: (This example prints everything in the &#8220;admin&#8221; collection)<\/p>\n<pre>db.admin.find()<\/pre>\n<p>Find info that line that whose name is admin<\/p>\n<pre> db.admin.find({name : \"admin\" })<\/pre>\n<p>Find everything in a table, but only print columns that are named &#8220;name, email, and x_shadow&#8221;<\/p>\n<pre>db.admin.find({ }, { name : \"\", email : \"\", \"x_shadow\" : \"\" })<\/pre>\n<p>List users<\/p>\n<pre>show users<\/pre>\n<p>Authenticate<\/p>\n<pre>db.auth(\"username\",\"password\")<\/pre>\n<p>Insert into table\/collection<\/p>\n<pre>d = {\"data\":\"data\"}\ndb.collection.insert(d)<\/pre>\n<p>Update a line, for example a users password.&nbsp; Swap out the ObjectId for the ID that mongo gives you when you list the admins or users.&nbsp; You will need to swap out the hash for the hash of the password you want.<\/p>\n<pre>db.admin.update({\"_id\" : ObjectId(\"a328bf90547ehc429a03ed85\")}, {$set: { \"x_shadow\" : \"$6$XB32GMXr$8dUt9huJzzL6O.gGZbs7QH1npldbzBzNDt\/uUO1bI3b7Ij3YipgubtVHwincUUZjnDLh.KDI36uh2gUCID9yb1\"}});<\/pre>\n<p>Example:<\/p>\n<pre>d = {\"name\":\"admin\",\"lang\":\"en_US\",\"x_password\":\"password\",\"time_created\":\"\",\"last_site_name\":\"default\"}\ndb.admin.insert(d)<\/pre>\n<p>Delete line from table\/collection<\/p>\n<pre>db.collection.remove()<\/pre>\n<p>Example: (This will look for all the &#8220;rows&#8221; where the &#8220;name&#8221; contains &#8220;admin&#8221; and remove it)<\/p>\n<pre>db.admin.remove({ name: \"admin\"})<\/pre>\n\n\n<p><strong>UniFI Specific <\/strong><\/p>\n\n\n\n<p><em>The following commands are specifically for a UniFi server.  <\/em><\/p>\n\n\n\n<p>Show UniFi Sites<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.site.find().forEach(printjson);<\/pre>\n\n\n\n<p>Or<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mongo --port 27117 ace --eval \"db.site.find().forEach(printjson);\"<\/pre>\n\n\n\n<p>Show UniFi admins<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.admin.find().forEach(printjson);<\/pre>\n\n\n\n<p>Or<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mongo --port 27117 ace --eval \"db.admin.find().forEach(printjson);\"<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Connect to Mongo database mongo Connect to Mongo on a different port (May be needed.&nbsp; Port is for UniFi server) mongo -port 27117 Show Databases show dbs Use database use dbname Show tables\/collections show collections or show tables List contents &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/basic-mongodb-commands\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[138,371,375],"class_list":["post-1749","post","type-post","status-publish","format-standard","hentry","category-linux","tag-database","tag-mongo","tag-mongodb"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/1749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=1749"}],"version-history":[{"count":8,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/1749\/revisions"}],"predecessor-version":[{"id":4365,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/1749\/revisions\/4365"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=1749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=1749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=1749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}