{"id":6505,"date":"2014-07-28T14:54:40","date_gmt":"2014-07-28T06:54:40","guid":{"rendered":"http:\/\/jpuyy.com\/?p=6505"},"modified":"2014-07-28T15:01:43","modified_gmt":"2014-07-28T07:01:43","slug":"mongodb-command-line","status":"publish","type":"post","link":"https:\/\/jpuyy.com\/?p=6505","title":{"rendered":"mongodb\u64cd\u4f5c"},"content":{"rendered":"<p>mongodb\u7aef\u53e3\u4e3a27017<\/p>\n<pre>http:\/\/127.0.0.1:27017<\/pre>\n<p>It looks like you are trying to access MongoDB over HTTP on the native driver port.<\/p>\n<p>mongodb\u7684\u7ed3\u6784\u4e3adatabase, collection,<\/p>\n<p>\u521b\u5efadatabase,<\/p>\n<pre>&gt; use blog\r\n switched to db blog<\/pre>\n<p>\u67e5\u770b\u5df2\u7ecf\u5b58\u5728\u7684db \u53ca\u5176\u5360\u7528\u7a7a\u95f4<\/p>\n<pre>&gt; show dbs\r\n admin (empty)\r\n blog 0.078GB\r\n example 0.078GB\r\n local 0.078GB<\/pre>\n<p>\u63a5\u4e0b\u6765\u5728collection\u91cc\u6dfb\u52a0json\u683c\u5f0f\u7684\u5185\u5bb9<\/p>\n<pre>&gt; db.posts.insert({'title':'learn mongo','tag':'db'})\r\n WriteResult({ \"nInserted\" : 1 })\r\n &gt; db.posts.insert({'title':'learn mysql','tag':'db'})\r\n WriteResult({ \"nInserted\" : 1 })<\/pre>\n<p>\u67e5\u770b\u540d\u4e3aposts\u7684collection\u7684\u6240\u6709\u5185\u5bb9<\/p>\n<pre>&gt; db.posts.find()\r\n { \"_id\" : ObjectId(\"53d1b11aecbaca2dd97bc8d8\"), \"title\" : \"learn mongo\", \"tag\" : \"db\" }\r\n { \"_id\" : ObjectId(\"53d1b124ecbaca2dd97bc8d9\"), \"title\" : \"learn mysql\", \"tag\" : \"db\" }<\/pre>\n<p>\u67e5\u770b\u5df2\u7ecf\u6709\u7684collections<\/p>\n<pre>&gt; show collections\r\n posts\r\n system.indexes<\/pre>\n<p>\u6e05\u7a7acollection<\/p>\n<pre>&gt; db.posts.remove()\r\n 2014-07-25T09:30:33.139+0800 remove needs a query at src\/mongo\/shell\/collection.js:299<\/pre>\n<p>\u5220\u9664collection<\/p>\n<pre>&gt; db.posts.drop()\r\n true<\/pre>\n<p>\u5220\u9664\u5f53\u524d\u6570\u636e\u5e93<\/p>\n<pre>&gt; db.dropDatabase()<\/pre>\n<p>\u4e94\uff0c\u66f4\u591a\u547d\u4ee4<br \/>\ndb.AddUser(username,password) \u6dfb\u52a0\u7528\u6237<\/p>\n<p>db.auth(usrename,password) \u8bbe\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u9a8c\u8bc1<\/p>\n<p>db.cloneDataBase(fromhost) \u4ece\u76ee\u6807\u670d\u52a1\u5668\u514b\u9686\u4e00\u4e2a\u6570\u636e\u5e93<br \/>\ndb.commandHelp(name) returns the help for the command<br \/>\ndb.copyDatabase(fromdb,todb,fromhost) \u590d\u5236\u6570\u636e\u5e93fromdb&#8212;\u6e90\u6570\u636e\u5e93\u540d\u79f0\uff0ctodb&#8212;\u76ee\u6807\u6570\u636e\u5e93\u540d\u79f0\uff0cfromhost&#8212;\u6e90\u6570\u636e\u5e93\u670d\u52a1\u5668\u5730\u5740<br \/>\ndb.createCollection(name,{size:3333,capped:333,max:88888}) \u521b\u5efa\u4e00\u4e2a\u6570\u636e\u96c6\uff0c\u76f8\u5f53\u4e8e\u4e00\u4e2a\u8868<br \/>\ndb.currentOp() \u53d6\u6d88\u5f53\u524d\u5e93\u7684\u5f53\u524d\u64cd\u4f5c<br \/>\ndb.dropDataBase() \u5220\u9664\u5f53\u524d\u6570\u636e\u5e93<br \/>\ndb.eval(func,args) run code server-side<br \/>\ndb.getCollection(cname) \u53d6\u5f97\u4e00\u4e2a\u6570\u636e\u96c6\u5408\uff0c\u540c\u7528\u6cd5\uff1adb[&#8216;cname&#8217;] or<br \/>\ndb.getCollenctionNames() \u53d6\u5f97\u6240\u6709\u6570\u636e\u96c6\u5408\u7684\u540d\u79f0\u5217\u8868<br \/>\ndb.getLastError() \u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u9519\u8bef\u7684\u63d0\u793a\u6d88\u606f<br \/>\ndb.getLastErrorObj() \u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u9519\u8bef\u7684\u5bf9\u8c61<br \/>\ndb.getMongo() \u53d6\u5f97\u5f53\u524d\u670d\u52a1\u5668\u7684\u8fde\u63a5\u5bf9\u8c61get the server<br \/>\ndb.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair<br \/>\ndb.getName() \u8fd4\u56de\u5f53\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u540d\u79f0<br \/>\ndb.getPrevError() \u8fd4\u56de\u4e0a\u4e00\u4e2a\u9519\u8bef\u5bf9\u8c61<br \/>\ndb.getProfilingLevel()<br \/>\ndb.getReplicationInfo() \u83b7\u5f97\u91cd\u590d\u7684\u6570\u636e<br \/>\ndb.getSisterDB(name) get the db at the same server as this onew<br \/>\ndb.killOp() \u505c\u6b62\uff08\u6740\u6b7b\uff09\u5728\u5f53\u524d\u5e93\u7684\u5f53\u524d\u64cd\u4f5c<br \/>\ndb.printCollectionStats() \u8fd4\u56de\u5f53\u524d\u5e93\u7684\u6570\u636e\u96c6\u72b6\u6001<br \/>\ndb.printReplicationInfo()<br \/>\ndb.printSlaveReplicationInfo()<br \/>\ndb.printShardingStatus() \u8fd4\u56de\u5f53\u524d\u6570\u636e\u5e93\u662f\u5426\u4e3a\u5171\u4eab\u6570\u636e\u5e93<br \/>\ndb.removeUser(username) \u5220\u9664\u7528\u6237<br \/>\ndb.repairDatabase() \u4fee\u590d\u5f53\u524d\u6570\u636e\u5e93<br \/>\ndb.resetError()<br \/>\ndb.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj:1}<br \/>\ndb.setProfilingLevel(level) 0=off,1=slow,2=all<br \/>\ndb.shutdownServer() \u5173\u95ed\u5f53\u524d\u670d\u52a1\u7a0b\u5e8f<\/p>\n<p>db.version() \u8fd4\u56de\u5f53\u524dMongoDB\u7248\u672c\u4fe1\u606f<\/p>\n<pre>&gt; db.version()\r\n2.6.0<\/pre>\n<p>db.test.find({id:10}) \u8fd4\u56detest\u6570\u636e\u96c6ID=10\u7684\u6570\u636e\u96c6<br \/>\ndb.test.find({id:10}).count() \u8fd4\u56detest\u6570\u636e\u96c6ID=10\u7684\u6570\u636e\u603b\u6570<br \/>\ndb.test.find({id:10}).limit(2) \u8fd4\u56detest\u6570\u636e\u96c6ID=10\u7684\u6570\u636e\u96c6\u4ece\u7b2c\u4e8c\u6761\u5f00\u59cb\u7684\u6570\u636e\u96c6<br \/>\ndb.test.find({id:10}).skip(8) \u8fd4\u56detest\u6570\u636e\u96c6ID=10\u7684\u6570\u636e\u96c6\u4ece0\u5230\u7b2c\u516b\u6761\u7684\u6570\u636e\u96c6<br \/>\ndb.test.find({id:10}).limit(2).skip(8) \u8fd4\u56detest\u6570\u636e\u96c6ID=1=\u7684\u6570\u636e\u96c6\u4ece\u7b2c\u4e8c\u6761\u5230\u7b2c\u516b\u6761\u7684\u6570\u636e<br \/>\ndb.test.find({id:10}).sort() \u8fd4\u56detest\u6570\u636e\u96c6ID=10\u7684\u6392\u5e8f\u6570\u636e\u96c6<br \/>\ndb.test.findOne([query]) \u8fd4\u56de\u7b26\u5408\u6761\u4ef6\u7684\u4e00\u6761\u6570\u636e<br \/>\ndb.test.getDB() \u8fd4\u56de\u6b64\u6570\u636e\u96c6\u6240\u5c5e\u7684\u6570\u636e\u5e93\u540d\u79f0<br \/>\ndb.test.getIndexes() \u8fd4\u56de\u4e9b\u6570\u636e\u96c6\u7684\u7d22\u5f15\u4fe1\u606f<br \/>\ndb.test.group({key:&#8230;,initial:&#8230;,reduce:&#8230;[,cond:&#8230;]})<br \/>\ndb.test.mapReduce(mayFunction,reduceFunction,)<br \/>\ndb.test.remove(query) \u5728\u6570\u636e\u96c6\u4e2d\u5220\u9664\u4e00\u6761\u6570\u636e<br \/>\ndb.test.renameCollection(newName) \u91cd\u547d\u540d\u4e9b\u6570\u636e\u96c6\u540d\u79f0<br \/>\ndb.test.save(obj) \u5f80\u6570\u636e\u96c6\u4e2d\u63d2\u5165\u4e00\u6761\u6570\u636e<br \/>\ndb.test.stats() \u8fd4\u56de\u6b64\u6570\u636e\u96c6\u7684\u72b6\u6001<br \/>\ndb.test.storageSize() \u8fd4\u56de\u6b64\u6570\u636e\u96c6\u7684\u5b58\u50a8\u5927\u5c0f<br \/>\ndb.test.totalIndexSize() \u8fd4\u56de\u6b64\u6570\u636e\u96c6\u7684\u7d22\u5f15\u6587\u4ef6\u5927\u5c0f<br \/>\ndb.test.totalSize() \u8fd4\u56de\u4e9b\u6570\u636e\u96c6\u7684\u603b\u5927\u5c0f<br \/>\ndb.test.update(query,object[,upsert_bool]) \u5728\u6b64\u6570\u636e\u96c6\u4e2d\u66f4\u65b0\u4e00\u6761\u6570\u636e<br \/>\ndb.test.validate() \u9a8c\u8bc1\u6b64\u6570\u636e\u96c6<br \/>\ndb.test.getShardVersion() \u8fd4\u56de\u6570\u636e\u96c6\u5171\u4eab\u7248\u672c\u53f7<\/p>\n<p>\u516d\uff0cMongoDB\u8bed\u6cd5\u4e0e\u73b0\u6709\u5173\u7cfb\u578b\u6570\u636e\u5e93SQL\u8bed\u6cd5\u6bd4\u8f83<br \/>\nMongoDB\u8bed\u6cd5 MySql\u8bed\u6cd5<br \/>\ndb.test.find({&#8216;name&#8217;:&#8217;foobar&#8217;}) &lt;==&gt; select * from test where name=&#8217;foobar&#8217;<br \/>\ndb.test.find() &lt;==&gt; select * from test<br \/>\ndb.test.find({&#8216;ID&#8217;:10}).count() &lt;==&gt; select count(*) from test where ID=10<br \/>\ndb.test.find().skip(10).limit(20) &lt;==&gt; select * from test limit 10,20<br \/>\ndb.test.find({&#8216;ID&#8217;:{$in:[25,35,45]}}) &lt;==&gt; select * from test where ID in (25,35,45)<br \/>\ndb.test.find().sort({&#8216;ID&#8217;:-1}) &lt;==&gt; select * from test order by ID desc<br \/>\ndb.test.distinct(&#8216;name&#8217;,{&#8216;ID&#8217;:{$lt:20}}) &lt;==&gt; select distinct(name) from test where ID&lt;20<br \/>\ndb.test.group({key:{&#8216;name&#8217;:true},cond:{&#8216;name&#8217;:&#8217;foo&#8217;},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}}) &lt;==&gt; select name,sum(marks) from test group by name<br \/>\ndb.test.find(&#8216;this.ID&lt;20&#8217;,{name:1}) &lt;==&gt; select name from test where ID&lt;20<br \/>\ndb.test.insert({&#8216;name&#8217;:&#8217;foobar&#8217;,&#8217;age&#8217;:25})&lt;==&gt;insert into test (&#8216;name&#8217;,&#8217;age&#8217;) values(&#8216;foobar&#8217;,25)<br \/>\ndb.test.remove({}) &lt;==&gt; delete * from test<br \/>\ndb.test.remove({&#8216;age&#8217;:20}) &lt;==&gt; delete test where age=20<br \/>\ndb.test.remove({&#8216;age&#8217;:{$lt:20}}) &lt;==&gt; elete test where age&lt;20<br \/>\ndb.test.remove({&#8216;age&#8217;:{$lte:20}}) &lt;==&gt; delete test where age&lt;=20<br \/>\ndb.test.remove({&#8216;age&#8217;:{$gt:20}}) &lt;==&gt; delete test where age&gt;20<br \/>\ndb.test.remove({&#8216;age&#8217;:{$gte:20}}) &lt;==&gt; delete test where age&gt;=20<br \/>\ndb.test.remove({&#8216;age&#8217;:{$ne:20}}) &lt;==&gt; delete test where age!=20<br \/>\ndb.test.update({&#8216;name&#8217;:&#8217;foobar&#8217;},{$set:{&#8216;age&#8217;:36}}) &lt;==&gt; update test set age=36 where name=&#8217;foobar&#8217;<br \/>\ndb.test.update({&#8216;name&#8217;:&#8217;foobar&#8217;},{$inc:{&#8216;age&#8217;:3}}) &lt;==&gt; update test set age=age+3 where name=&#8217;foobar&#8217;<\/p>\n<p>\u6ce8\u610f\u4ee5\u4e0a\u547d\u4ee4\u5927\u5c0f\u5199\u654f\u611f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mongodb\u7aef\u53e3\u4e3a27017 http:\/\/127.0.0.1:27017 It looks like you are trying to access MongoDB over HTTP on the native driver port. mongodb\u7684\u7ed3\u6784\u4e3adatabase, collection, \u521b\u5efadatabase, &gt; use blog switched to db blog \u67e5\u770b\u5df2\u7ecf\u5b58\u5728\u7684db \u53ca\u5176\u5360\u7528\u7a7a\u95f4 &gt; show dbs admin (empty) blog 0.078GB example 0.078GB local 0.078GB \u63a5\u4e0b\u6765\u5728collection\u91cc\u6dfb\u52a0json\u683c\u5f0f\u7684\u5185\u5bb9 &gt; db.posts.insert({&#8216;title&#8217;:&#8217;learn mongo&#8217;,&#8217;tag&#8217;:&#8217;db&#8217;}) WriteResult({ &#8220;nInserted&#8221; : 1 }) &gt; db.posts.insert({&#8216;title&#8217;:&#8217;learn mysql&#8217;,&#8217;tag&#8217;:&#8217;db&#8217;}) WriteResult({ &#8220;nInserted&#8221; [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-6505","post","type-post","status-publish","format-standard","hentry","category-life"],"_links":{"self":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6505"}],"version-history":[{"count":12,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6505\/revisions"}],"predecessor-version":[{"id":6522,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6505\/revisions\/6522"}],"wp:attachment":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}