{"id":2308,"date":"2012-10-17T08:03:47","date_gmt":"2012-10-17T08:03:47","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=2308"},"modified":"2012-10-17T08:05:29","modified_gmt":"2012-10-17T08:05:29","slug":"mongodb%e6%9d%83%e5%a8%81%e6%8c%87%e5%8d%97%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2012\/10\/17\/mongodb%e6%9d%83%e5%a8%81%e6%8c%87%e5%8d%97%e7%ac%94%e8%ae%b0\/","title":{"rendered":"mongodb\u6743\u5a01\u6307\u5357\u7b14\u8bb0"},"content":{"rendered":"<pre class=\"brush:js;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;\">\r\n1.\t\u5b89\u88c5\r\n\u2460\t\u6307\u5b9a\u6570\u636e\u5e93\u4f4d\u7f6e\r\n?\tmongod.exe --dbpath d:\\test\\mongodb\\data\r\n?\t\u82e5\u4e0d\u6307\u5b9a\uff0c\u9ed8\u8ba4\u4e3ac:\\data\\db\\\r\n\u2461\t\u8fd0\u884c\uff1a\r\n?\tmongod.exe\r\n?\t\u4ee5windows\u670d\u52a1\u8fd0\u884c\r\n\u5b89\u88c5\uff1a&gt;mongod.exe --logpath &quot;mongolog.txt&quot; --install\r\n\u8fd0\u884c\uff1a net start mongodb\r\n?\t\u9ed8\u8ba4\u7aef\u53e3\uff1a27017\r\n\u2462\tcsharp\u5f00\u53d1\u6587\u6863 (http:\/\/www.mongodb.org\/display\/DOCS\/CSharp+Driver+Tutorial#CSharpDriverTutorial-Downloading) \r\n\u2463\t\u9762\u5411\u6587\u6863\u7684\u975e\u5173\u7cfb\u6570\u636e\u5e93\u4e3b\u8981\u89e3\u51b3\u7684\u95ee\u9898\u4e0d\u662f\u9ad8\u6027\u80fd\u7684\u5e76\u53d1\u8bfb\u5199\uff0c\u800c\u662f\u4fdd\u8bc1\u6d77\u91cf\u6570\u636e\u5b58\u50a8\u7684\u540c\u65f6\uff0c\u5177\u6709\u826f\u597d\u7684\u67e5\u8be2\u6027\u80fd\u3002\r\n\r\n\r\n2.\t\u7b2c2\u7ae0\r\n\u2460\t\u6587\u6863\r\n?\t\u57fa\u672c\u5355\u5143\u662f\u6587\u6863\uff0c\u7c7b\u4f3c\u4e8e\u6570\u636e\u5e93\u4e2d\u7684\u884c\r\n?\t\u6bcf\u4e2a\u6587\u6863\u90fd\u6709\u7279\u6b8a\u7684\u952e_id,\u5b83\u5728\u6587\u6863\u6240\u5904\u7684\u96c6\u5408\u4e2d\u662f\u552f\u4e00\u7684\r\n?\t\u7c7b\u4f3c\u4e8ejson,\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u5b58\u5728\uff0c\u533a\u5206\u7c7b\u578b\u4e0e\u5927\u5c0f\u5199\r\n?\t\u4f8b\uff1a&gt; db.test.save( { &quot;username&quot;:&quot;asong&quot;,age:15} )\r\n\u2461\t\u96c6\u5408\r\n?\t\u76f8\u5f53\u4e8e\u6570\u636e\u5e93\u4e2d\u7684\u8868\uff0c\u5b83\u91cc\u9762\u5b58\u50a8\u7684\u662f&ldquo;\u884c&rdquo;\u6587\u6863\r\n?\t\u591a\u4e2a\u96c6\u5408\u7ec4\u6210\u6570\u636e\u5e93\r\n?\t\u5305\u542b\u7684\u6587\u6863\u6570\u91cf&gt; db.test.count()\r\n\u2462\t\u547d\u4ee4\u64cd\u4f5c\r\n?\t\u63d2\u5165\r\n&gt; db.test.save( { &quot;username&quot;:&quot;asong&quot;,age:15} )\r\n&gt; db.test.insert({&quot;a&quot;:&quot;b&quot;})\r\n?\tdb:\u5f53\u524d\u6570\u636e\u5e93\r\n?\ttest:\u5f53\u524d\u96c6\u5408\r\n?\t\u663e\u793a\u5f53\u524d\u6570\u636e\u5e93\u7684\u96c6\u5408\uff1ashow collections\r\n?\t\u8fdb\u884c\u63d2\u5165\u65f6\uff0c\u662f\u5426\u6210\u529f\u5982\u91cd\u590d\uff0c\u9ed8\u8ba4\u662f\u6ca1\u6709\u63d0\u793a\u7684\r\n?\t\u67e5\u8be2\r\n&gt; db.text.findOne()\r\n&gt; db.test.find()\r\ndb.test.findOne({&quot;username&quot;:&quot;asong&quot;})\r\n?\t\u66f4\u65b0\r\ndb.test.update({&quot;username&quot;:&quot;asong&quot;},{&quot;username&quot;:&quot;fangfang&quot;})\r\n?\t\u5220\u9664\r\n&gt; db.test.remove({&quot;username&quot;:&quot;asong&quot;})\r\n?\t&gt;help\r\n?\tshow dbs\r\n?\t\u8f93\u5165\u65b9\u6cd5\u4e0d\u52a0\u62ec\u53f7\u53ef\u770b\u6e90\u7801\u5982\uff1a&gt; db.test.update\r\n\r\n\r\n3.\t\u7b2c3\u7ae0\u521b\u5efa\u66f4\u65b0\u5220\u9664\u6587\u6863\r\n\u2460\tupdate\u53ef\u4ee5\u4e00\u6b21\u6539\u5168\u90e8\u6570\u636e\uff0c \u4e5f\u53ef\u4fee\u6539\u6587\u6863\u90e8\u5206\u6570\u636e\r\n?\t$inc\uff1a\u81ea\u589e \u4e0d\u5b58\u5728\u5219\u521b\u5efa\r\ndb.it.update({&quot;name&quot;:&quot;pdf&quot;},{&quot;$inc&quot;:{&quot;age&quot;:3}})\r\n?\t$set\uff1a\u8bbe\u7f6e \u4e0d\u5b58\u5728\u5219\u521b\u5efa\r\ndb.it.update({&quot;name&quot;:&quot;pdf&quot;},{&quot;$set&quot;:{&quot;age&quot;:3}})\r\n?\tc\r\n\u82e5\u6ca1\u6709\uff0c\u5219\u589e\u52a0\u6b64\u5c5e\u6027\uff0c\u6545\u53ef\u7528set \u5b8c\u6210\u6587\u6863\u5c5e\u6027\u7684\u589e\u52a0\r\n?\t$unset:\u5220\u9664\u5c5e\u6027\r\ndb.it.update({&quot;name&quot;:&quot;pdf&quot;},{&quot;$unset&quot;:{&quot;age&quot;:3}})\r\n\u2461\t\u6570\u7ec4\u64cd\u4f5c\r\n?\t\u503c\u662f\u4e00\u6587\u6863\u7c7b\u578b\uff0c\u5982db.it.insert({&quot;person&quot;:{&quot;name&quot;:&quot;zxs&quot;,age:28}})\r\n?\t$push:\u589e\u52a0\u4fee\u6539\r\ndb.it.update({&quot;name&quot;:&quot;pdf&quot;},{&quot;$push&quot;:{&quot;www&quot;:{&quot;a&quot;:3}}})\r\n?\t$pop:\u5220\u9664\r\n\u2462\tupsert:\u66f4\u65b0\uff0c\u6ca1\u6709\u5219insert,\u6216\u8005\u7528save\r\n\r\n\r\n4.\t\u7b2c4\u7ae0 \u67e5\u8be2\r\n\u2460\tdb.it.find({&quot;name&quot;:&quot;pdf&quot;})\r\n\u2461\tdb.it.findOne({&quot;_id&quot;:ObjectId(&quot;506e3f62e4cfb782ba3d4233&quot;)})\r\n\u2462\t\u8fd4\u56de\u6307\u5b9a\u5c5e\u6027\u5b57\u6bb5\r\n?\tdb.it.find({&quot;name&quot;:&quot;pdf&quot;},{&quot;age&quot;:1,&quot;skill&quot;:1})\r\n\u2463\t\u6bd4\u8f83\u8fd0\u7b97\r\n?\tfind({\u5c5e\u6027\uff1a{\u6761\u4ef6}})  db.test.find({&quot;age&quot;:{&quot;$gte&quot;:28,&quot;$lte&quot;:30}})\r\n?\t\u8fd0\u7b97\u7b26\r\n$lt:&lt;\r\n$gt:&gt;\r\n$lte,$gte:&lt;=,&gt;=\r\n\u2464\t\u903b\u8f91\u8fd0\u7b97\r\n?\tOR\u5173\u7cfb\r\ndb.test.find({&quot;$or&quot;:[{&quot;age&quot;:26},{&quot;age&quot;:28}]})\r\n?\tIN\r\ndb.test.find({&quot;age&quot;:{&quot;$in&quot;:[26,28]}})\r\n\u2465\t\u6b63\u5219\u8868\u8fbe\u5f0f\r\n?\t\/\u6b63\u5219\/ \uff1adb.test.find({&quot;name&quot;:\/aso*|fang*\/})\r\n\u2466\t\u96c6\u5408\u904d\u5386\r\n?\t&gt; var mycur = db.test.find() &gt; if(mycur.hasNext()) {mycur.next()}\r\n\u2467\tlimit,skip,sort\r\n?\tsort:\u6392\u5e8f 1 \u5347\u5e8f\uff0c-1 \u964d\u5e8f\r\n?\tlimit:\u8fd4\u56de\u51e0\u6761\u7ed3\u679c\r\n?\tskip:\u7565\u8fc7\u51e0\u6761\uff0c\u8fd4\u56de\u5269\u4e0b\u7684\r\n?\t&gt; db.test.find().limit(2).skip(1).sort({&quot;name&quot;:1})\r\n\r\n\r\n5.\t\u7b2c5\u7ae0 \u7d22\u5f15\r\n\u2460\t\u521b\u5efa\uff1a1\u5347\u5e8f -1\u964d\u5e8f \uff1a&gt; db.test.ensureIndex({&quot;name&quot;:-1})\r\n\u2461\t\u552f\u4e00\u7d22\u5f15\uff1a&gt; db.test.ensureIndex({&quot;name&quot;:-1},{&quot;unique&quot;:true})\r\n\u2462\t\u6267\u884c\u8ba1\u5212\u67e5\u770b\uff1aexplain()\r\n?\t&gt; db.test.find({&quot;name&quot;:&quot;asong&quot;}).explain() {         &quot;cursor&quot; : &quot;BtreeCursor name_1&quot;,         &quot;nscanned&quot; : 3,         &quot;nscannedObjects&quot; : 3,         &quot;n&quot; : 3,         &quot;millis&quot; : 0,         &quot;nYields&quot; : 0,         &quot;nChunkSkips&quot; : 0,         &quot;isMultiKey&quot; : false,         &quot;indexOnly&quot; : false,         &quot;indexBounds&quot; : {                 &quot;name&quot; : [                         [                                 &quot;asong&quot;,                                 &quot;asong&quot;                         ]                 ]         } } &gt;\r\n?\t\u7ed3\u679c\r\ncursor:\u4f7f\u7528\u7684\u7d22\u5f15\uff0c\u5f53\u4e3aBasicCursor\u65f6\u4e3a\u5168\u8868\u626b\u63cf\r\nnscanned:\u67e5\u627e\u7684\u6587\u6863\u6570\r\nn:\u8fd4\u56de\u7684\u6587\u6863\u6570\r\nmillis:\u67e5\u8be2\u6240\u7528\u65f6\u95f4\r\n\u2463\t\u67e5\u770b\u7d22\u5f15\uff1a&gt; db.system.indexes.find()\r\n\u2464\t\u5220\u9664\u7d22\u5f15\uff1a&gt; db.test.dropIndex({&quot;name&quot;:1})\r\n\r\n\r\n6.\t\u7b2c6\u7ae0 \u805a\u5408\r\n\u2460\t&gt; db.test.count()\r\n\u2461\t&gt; db.test.find().distinct({&quot;name&quot;,&quot;key&quot;:&quot;name&quot;})\r\n\u2462\tMapReduce:\u6620\u5c04\u5316\u7b80\uff0c\u5c06\u4efb\u52a1\u62c6\u5206\u5230\u591a\u53f0\u670d\u52a1\u5668\u6267\u884c\r\n\r\n\r\n7.\t\u7b2c7\u7ae0 \u8fdb\u9636\u6307\u5357\r\n\u2460\tdb.listCommands():\u67e5\u770b\u547d\u4ee4\r\n\u2461\t\u67e5\u770b\u6570\u636e\u5e93\u7248\u672c\u53ca\u64cd\u4f5c\u7cfb\u7edf\r\n?\t&gt; db.runCommand({&quot;buildInfo&quot;:1})\r\n\r\n\r\n8.\t\u7b2c8\u7ae0 \u7ba1\u7406\r\n\u2460\t\u5728mongodb\u542f\u52a8\u65f6\uff0c\u4f1a\u542f\u52a8\u4e00HTTP\u670d\u52a1\u5668 \u53ef\u901a\u8fc7\u7f51\u9875\u67e5\u770b\u6570\u636e\u5e93\u6027\u80fd\r\n?\thttp:\/\/localhost:28017\/\r\n\u2461\t\u67e5\u770b\u670d\u52a1\u5668\u7edf\u8ba1\u4fe1\u606f\r\n?\t&gt; db.runCommand({&quot;serverStatus&quot;:1})\r\n?\t\u6216\u8005cmd\u4e0b\uff1amongostat\r\n\u2462\t\u5907\u4efd\uff1amongodump\r\n\r\n\r\n9.\t\u7b2c9\u7ae0 \u590d\u5236\r\n\u2460\t\u4e3b\u4ece\u590d\u5236\r\n?\t\u4e00\u53f0\u4e3b\u673a\u53ef\u8dd1\u591a\u4e2amongodb,\u6d4b\u8bd5\u4e3b\u4ece\r\n?\t\u542f\u52a8\u65f6\u6307\u5b9a\u662f\u4e3b\u8fd8\u662f\u4ece --master\r\n\u2461\t\u526f\u672c\u96c6\r\n?\t\u5e26\u6709\u6545\u969c\u81ea\u52a8\u8f6c\u79fb\u7684\u4e3b\u4ece\u590d\u5236\r\n\r\n\r\n10.\t\u7b2c10\u7ae0 \u5206\u7247\r\n\u2460\t\u5c06\u6570\u636e\u62c6\u5206\uff0c\u5206\u6563\u5230\u5404\u670d\u52a1\u5668\u4e0a\r\n\u2461\t\u590d\u5236\u5206\u53d1\u914d\u7f6e\u90fd\u5f88\u7b80\u5355\uff0c\u5927\u90e8\u5206\u81ea\u52a8\u5316\uff0c\u6838\u5fc3\u662f\u952e\u503c\u5bf9\uff0c\u975e\u7ed3\u6784\u5316\r\n\r\n\r\n11.\t\u7b2c11\u7ae0 \u5e94\u7528\u4e3e\u4f8b\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. \u5b89\u88c5 \u2460 \u6307\u5b9a\u6570\u636e\u5e93\u4f4d\u7f6e ? mongod.exe &#8211;dbpath d:\\test\\mongodb\\d [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[],"class_list":["post-2308","post","type-post","status-publish","format-standard","hentry","category-mongodb"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/2308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/comments?post=2308"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/2308\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=2308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=2308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=2308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}