{"id":81,"date":"2015-04-15T14:47:30","date_gmt":"2015-04-15T06:47:30","guid":{"rendered":"https:\/\/yanjingang.com\/blog\/?p=81"},"modified":"2016-01-07T14:48:06","modified_gmt":"2016-01-07T06:48:06","slug":"mongodb%e7%b4%a2%e5%bc%95%e5%b0%8f%e7%bb%93","status":"publish","type":"post","link":"https:\/\/yanjingang.com\/blog\/?p=81","title":{"rendered":"MongoDB\u7d22\u5f15\u5c0f\u7ed3"},"content":{"rendered":"<div>\u5b98\u65b9\u6587\u6863\u7d22\u5f15\u90e8\u5206\u7684\u5730\u5740\uff1a<\/div>\n<p><a href=\"http:\/\/docs.mongodb.org\/manual\/indexes\/\" target=\"_blank\">http:\/\/docs.mongodb.org\/manual\/indexes\/<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>1.\u521b\u5efa\u7d22\u5f15<\/p>\n<p>1\uff09\u521b\u5efa\u5355\u4e2a\u7d22\u5f15<\/p>\n<p>db.collection.ensureIndex({a:1})<\/p>\n<p>\u5728a\u5b57\u6bb5\u4e0a\u521b\u5efa\u4e00\u4e2a\u5347\u5e8f\u7684\u7d22\u5f15(\u5bf9\u4e8e\u5355\u4e2a\u5b57\u6bb5\u7684\u7d22\u5f15\uff0c\u5347\u5e8f\u6216\u662f\u964d\u5e8f\u90fd\u4e00\u6837)\u3002<\/p>\n<p>2)\u521b\u5efa\u590d\u5408\u7d22\u5f15<\/p>\n<p>db.collection.ensureIndex({a:1,b:-1})<\/p>\n<p>3\uff09\u521b\u5efa\u7a00\u758f\u7d22\u5f15<\/p>\n<p>db.collection.ensureIndex({a:1},{sparse:true})<\/p>\n<p>\u7d22\u5f15\u4e2d\u5c06\u4e0d\u5305\u542b\u6ca1\u6709a\u5b57\u6bb5\u7684\u6587\u6863\u3002<\/p>\n<p>4)\u521b\u5efa\u552f\u4e00\u7d22\u5f15<\/p>\n<p>db.collection.ensureIndex({a:1},{unique:true})<\/p>\n<p>\u4e3aa\u5b57\u6bb5\u5efa\u7acb\u552f\u4e00\u7d22\u5f15\u3002<\/p>\n<p>\u5f53mongo\u8981\u7d22\u5f15\u4e00\u4e2a\u5b57\u6bb5\u65f6\uff0c\u5982\u679c\u4e00\u7bc7\u6587\u6863\u4e2d\u6ca1\u6709\u8fd9\u4e2a\u5b57\u6bb5\uff0c\u8fd9\u7bc7\u6587\u6863\u5c31\u4f1a\u88ab\u7d22\u5f15\u4e3anull\uff0c<\/p>\n<p>\u56e0\u4e3a\u552f\u4e00\u7d22\u5f15\u4e0d\u80fd\u6709\u91cd\u590d\u503c\uff0c\u6240\u4ee5\u5fc5\u987b\u548c\u7a00\u758f\u7d22\u5f15\u914d\u5408\u4f7f\u7528\uff0c\u5982\uff1a<\/p>\n<p>db.collection.ensureIndex({a:1},{unique:true,sparse:true})<\/p>\n<p>\u590d\u5408\u7d22\u5f15\u4e5f\u53ef\u4ee5\u52a0\u552f\u4e00\u9650\u5236\uff0c\u5982\uff1a<\/p>\n<p>db.collection.ensureIndex({a:1,b:1},{unique:true})<\/p>\n<p>5)\u5728\u540e\u53f0\u521b\u5efa\u7d22\u5f15<\/p>\n<p>db.collection.ensureIndex({a:1},{background:true})<\/p>\n<p>6)\u4e22\u5f03\u91cd\u590d\u6570\u636e<\/p>\n<p>\u8981\u5f3a\u5236\u5728\u4e00\u4e2a\u6709\u91cd\u590d\u6570\u636e\u7684\u5b57\u6bb5\u4e0a\u521b\u5efa\u552f\u4e00\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528dropDups\u9009\u9879\uff0c\u8fd9\u4f1a\u5f3a\u5236mongo<\/p>\n<p>\u5728\u521b\u5efa\u552f\u4e00\u7d22\u5f15\u65f6\u5220\u9664\u91cd\u590d\u6570\u636e\uff08\u5371\u9669\u64cd\u4f5c\uff09\uff0c\u5982\uff1a<\/p>\n<p>db.collection.ensureIndex({a:1},{dropDups:true})<\/p>\n<p>&nbsp;<\/p>\n<p>2.\u67e5\u770b\u7d22\u5f15<\/p>\n<p>1)\u67e5\u770b\u67d0\u4e2a\u5e93\u4e0a\u7684\u6240\u6709\u7d22\u5f15<\/p>\n<p>db.system.index.find()<\/p>\n<p>2)\u67e5\u770b\u67d0\u4e2a\u8868\u4e0a\u7684\u6240\u6709\u7d22\u5f15<\/p>\n<p>db.collection.getIndexes()<\/p>\n<p>&nbsp;<\/p>\n<p>3.\u5220\u9664\u7d22\u5f15<\/p>\n<p>1)\u5220\u9664\u8868\u4e0a\u7684\u67d0\u4e2a\u7d22\u5f15<\/p>\n<p>db.collection.dropIndex({a:1})<\/p>\n<p>2)\u5220\u9664\u8868\u4e0a\u7684\u6240\u6709\u7d22\u5f15<\/p>\n<p>db.collection.dropIndexes()<\/p>\n<p>&nbsp;<\/p>\n<p>4.\u91cd\u5efa\u7d22\u5f15<\/p>\n<p>db.collection.reIndex()<\/p>\n<p>\u4ee5\u4e0a\u64cd\u4f5c\u4f1a\u5220\u9664\u8868\u4e0a\u7684\u6240\u6709\u7d22\u5f15(\u5305\u542b_id)\u5e76\u91cd\u5efa\u6240\u6709\u7d22\u5f15\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>5.\u5728\u526f\u672c\u96c6\u4e0a\u521b\u5efa\u7d22\u5f15<\/p>\n<p>\u540e\u53f0\u521b\u5efa\u7d22\u5f15\u64cd\u4f5c\u5728\u4e00\u4e2a\u526f\u672c\u96c6\u7684\u4ece\u8282\u70b9(secondary)\u4e0a\u4f1a\u53d8\u6210\u524d\u53f0\u64cd\u4f5c\uff0c\u8fd9\u65f6\u521b\u5efa\u7d22\u5f15\u7684\u64cd\u4f5c\u4f1a<\/p>\n<p>\u963b\u585e\u4ece\u8282\u70b9\u4e0a\u7684\u6240\u6709\u7684\u590d\u5236\u548c\u8bfb\u64cd\u4f5c\u3002<\/p>\n<p>\u4ece\u8282\u70b9\u4f1a\u5728\u4e3b\u8282\u70b9\u5b8c\u6210\u521b\u5efa\u7d22\u5f15\u7684\u64cd\u4f5c\u540e\u5f00\u59cb\u521b\u5efa\u7d22\u5f15\uff0c\u5728\u5206\u7247\u96c6\u7fa4\u73af\u5883(sharded cluster)\u4e2d\uff0c<\/p>\n<p>mongos\u4f1a\u5411\u6bcf\u4e2a\u5206\u7247\u4e0a\u7684\u526f\u672c\u96c6\u73af\u5883\u7684\u4e3b\u8282\u70b9\u53d1\u9001ensureIndex()\uff0c\u7136\u540e\u5728\u4e3b\u8282\u70b9\u5b8c\u6210\u521b\u5efa\u7d22\u5f15\u540e<\/p>\n<p>\u590d\u5236\u5230\u4ece\u8282\u70b9\u4e0a\u3002<\/p>\n<p>\u4e3a\u4e86\u5728\u526f\u672c\u96c6\u73af\u5883\u4e2d\u5c06\u521b\u5efa\u7d22\u5f15\u7684\u5f71\u54cd\u964d\u4f4e\u5230\u6700\u5c0f\uff0c\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5728\u4ece\u8282\u70b9\u4e0a\u521b\u5efa\u7d22\u5f15\uff1a<\/p>\n<p>1)\u5728\u4e00\u4e2a\u4ece\u8282\u70b9\u4e0a\u505c\u6b62mongod\u8fdb\u7a0b\uff0c\u4f7f\u7528\u53e6\u4e00\u4e2a\u7aef\u53e3\u53f7\u4e0d\u91c7\u7528&#8211;replSet\u9009\u9879\u91cd\u542fmongod\u8fdb\u7a0b\uff0c\u8fd9<\/p>\n<p>\u4e2amongod\u5b9e\u4f8b\u73b0\u5728\u5df2&#8221;\u72ec\u7acb&#8221;(standalone)\u6a21\u5f0f\u8fd0\u884c;<\/p>\n<p>2)\u5728\u8fd9\u4e2amongod\u5b9e\u4f8b\u4e0a\u521b\u5efa\u65b0\u7d22\u5f15\u6216\u8005\u91cd\u5efa\u7d22\u5f15;<\/p>\n<p>3)\u91c7\u7528&#8211;replSet\u9009\u9879\u91cd\u542fmongod\u5b9e\u4f8b\u3002\u5141\u8bb8\u590d\u5236\u64cd\u4f5ccatch up\u8fd9\u4e2a\u6210\u5458;<\/p>\n<p>4)\u5728\u5176\u5b83\u7684\u4ece\u8282\u70b9\u4e0a\u91cd\u590d\u8fd9\u4e2a\u64cd\u4f5c;<\/p>\n<p>5)\u5728\u8fd9\u4e2a\u96c6(set)\u4e0a\u7684\u4e3b\u8282\u70b9\u4e0a\u8fd0\u884crs.stepDown()\uff0c\u7136\u540e\u5728\u4e4b\u524d\u7684\u4e3b\u8282\u70b9\u4e0a\u91cd\u590d\u8fd9\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>6.\u7d22\u5f15\u4e2d\u7684\u5347\u5e8f\u952e\u548c\u964d\u5e8f\u952e<\/p>\n<p>\u5bf9\u4e8e\u5355\u4e2a\u952e\u7684\u7d22\u5f15\uff0c\u952e\u7684\u987a\u5e8f\u6ca1\u6709\u5173\u7cfb\u3002\u4f46\u662f\u5bf9\u4e8e\u7b26\u5408\u7d22\u5f15\uff0c\u952e\u7684\u5347\u3001\u964d\u987a\u5e8f\u6709\u5f88\u5927\u5f71\u54cd\u3002<\/p>\n<p>\u6bd4\u5982\u8bf4\u4e00\u4e2a\u8868\u5305\u542b\u7528\u6237\u540d(username)\u548c\u8be5\u7528\u6237\u53d1\u8868\u6587\u7ae0\u7684\u65f6\u95f4(time)\uff0c\u5982\u679c\u4f60\u9700\u8981\u7ecf\u5e38\u67e5\u8be2\u67d0\u7528\u6237\u6700<\/p>\n<p>\u65b0\u53d1\u8868\u7684\u6587\u7ae0\uff0c\u4f60\u5e94\u8be5\u521b\u5efa\u8fd9\u6837\u7684\u7d22\u5f15\uff1a<\/p>\n<p>db.collection.ensureIndex({&#8220;username&#8221;:1,&#8221;time&#8221;:-1})<\/p>\n<p>&nbsp;<\/p>\n<p>7.\u7d22\u5f15\u7684\u9650\u5236<\/p>\n<p>1)\u4e00\u4e2a\u8868\u4e0a\u7684\u7d22\u5f15\u4e0d\u80fd\u8d85\u8fc764\u4e2a;<\/p>\n<p>2)\u7d22\u5f15\u7684\u952e\u4e0d\u80fd\u8d85\u8fc71024bytes\uff08\uff09;<\/p>\n<p>3)\u7d22\u5f15\u7684\u540d\u5b57\uff08\u5305\u542b\u547d\u540d\u7a7a\u95f4\uff09\u5fc5\u987b\u5c11\u4e8e128\u4e2a\u5b57\u7b26;<\/p>\n<p>&nbsp;<\/p>\n<p>8.\u7d22\u5f15\u7b56\u7565<\/p>\n<p>MongoDB\u5bf9\u4e8e\u6bcf\u4e2a\u64cd\u4f5c\u53ea\u80fd\u4f7f\u7528\u4e00\u4e2a\u7d22\u5f15\uff0c$or\u7684\u6bcf\u4e00\u4e2a\u5b50\u53e5\u53ef\u4ee5\u4f7f\u7528\u81ea\u5df1\u7684\u7d22\u5f15\u3002<\/p>\n<p>\u5bf9\u4e8e\u4e0b\u8fb9\u7684\u7d22\u5f15\uff1a<\/p>\n<p>db.collection.ensureIndex({&#8220;category&#8221;:1,&#8221;item&#8221;:1})<\/p>\n<p>\u65e0\u8bba\u5355\u72ec\u67e5\u8be2category\u3001item\uff0c\u8fd8\u662f\u4e24\u8005\u4e00\u8d77\u67e5\u8be2\uff0c\u8be5\u7d22\u5f15\u90fd\u6709\u6548\u3002\u4f46\u662f\u6709\u4e2a\u4f8b\u5916\uff0c\u4f7f\u7528$or\u64cd\u4f5c\u7b26\u65f6\uff0c<\/p>\n<p>\u4e00\u4e2a\u67e5\u8be2\u4e0d\u80fd\u4f7f\u7528\u590d\u5408\u7d22\u5f15\uff0c\u53ea\u80fd\u4f7f\u7528\u5355\u4e2a\u7d22\u5f15\u3002<\/p>\n<p>\u4e00\u4e2a\u591a\u5b57\u6bb5\u7684\u590d\u5408\u7d22\u5f15\u53ef\u4ee5\u652f\u6301\u6240\u6709\u5728\u8fd9\u4e9b\u5b57\u6bb5\u4e0a\u5c31\u524d\u7f00\u5b50\u96c6\u8fdb\u884c\u67e5\u8be2\u7684\u9700\u6c42\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>db.collection.ensureIndex({x:1,y:1,z:1})<\/p>\n<p>\u4ee5\u4e0a\u7d22\u5f15\u53ef\u4ee5\u652f\u6301\u5982\u4e0b\u67e5\u8be2\u9700\u6c42\uff1a<\/p>\n<p>db.collection.find({x:232})<\/p>\n<p>db.collection.find({x:232,y:3})<\/p>\n<p>db.collection.find({x:232,y:3,z:34})<\/p>\n<p>&nbsp;<\/p>\n<p>\u7d22\u5f15{x:1,y:1,z:1}\u53ef\u4ee5\u652f\u6301\u4e00\u4e9b\u7d22\u5f15{x:1,z:1}\u652f\u6301\u7684\u67e5\u8be2?<\/p>\n<p>\u5bf9\u4e8e\u67e5\u8be2db.collection.find({x:5}).sort({z:1})\uff0c\u7d22\u5f15{x:1,z:1}\u65e2\u652f\u6301\u67e5\u8be2\u9875\u652f\u6301\u6392\u5e8f\uff0c\u800c\u7d22\u5f15<\/p>\n<p>{x:1,y:1,z:1}\u53ea\u652f\u6301\u67e5\u8be2\uff1f<\/p>\n<p>&nbsp;<\/p>\n<p>\u4f7f\u7528\u7d22\u5f15\u4e3a\u67e5\u8be2\u7684\u7ed3\u679c\u6392\u5e8f\uff1a<\/p>\n<p>\u6bd4\u5982\u6709\u8fd9\u6837\u4e00\u4e2a\u7d22\u5f15<\/p>\n<p>db.collection.ensureIndex({a:1,b:1,c:1,d:1})<\/p>\n<p>\u4ee5\u4e0b\u7684\u67e5\u8be2\u548c\u6392\u5e8f\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\uff1a<\/p>\n<p>db.collection.find().sort({a:1})<\/p>\n<p>db.collection.find().sort({a:1,b:1})<\/p>\n<p>db.collection.find({a:4}).sort({a:1,b:1})<\/p>\n<p>db.collection.find({b:5}).sort({a:1,b:1})<\/p>\n<p>db.collection.find({a:5}).sort({b:1,c:1})<\/p>\n<p>db.collection.find({a:5,c:4,b:3}).sort({d:1})<\/p>\n<p>db.collection.find({a:{$gt:4}}).sort({a:1,b:1})<\/p>\n<p>db.collection.find({a:{$gt:5}}).sort({a:1,b:1})<\/p>\n<p>db.collection.find({a:5, b:3,d:{$gt:4}}).sort({c:1})<\/p>\n<p>db.collection.find({a:5, b:3,c:{$lt:2},d:{$gt:4}}).sort({c:1})<\/p>\n<p>\u4ee5\u4e0b\u7684\u67e5\u8be2\u548c\u6392\u5e8f\u4e0d\u80fd\u4f7f\u7528\u7d22\u5f15\uff1a<\/p>\n<p>db.collection.find().sort( { b:1 } )<\/p>\n<p>db.collection.find( { b:5 } ).sort( { b:1 } )<\/p>\n<p>&nbsp;<\/p>\n<p>\u5bf9\u4e8e\u4e0d\u4f7f\u7528\u7d22\u5f15\u7684sort()\u64cd\u4f5c\uff0c\u5f53\u4f7f\u7528\u8d85\u8fc732Mb\u5185\u5b58\u65f6\uff0csort()\u64cd\u4f5c\u5c06\u9000\u51fa\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u5728\u4e00\u4e2a&#8221;\u5199\u4efb\u52a1&#8221;\u5f88\u91cd\u7684\u7cfb\u7edf\u4e2d\uff0c\u6dfb\u52a0\u6bcf\u4e00\u4e2a\u7d22\u5f15\u65f6\u8981\u7279\u522b\u8c28\u614e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b98\u65b9\u6587\u6863\u7d22\u5f15\u90e8\u5206\u7684\u5730\u5740\uff1a http:\/\/docs.mongodb.org\/manual\/indexes\/ &#038;n [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21],"tags":[63,22,62],"_links":{"self":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/81"}],"collection":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=81"}],"version-history":[{"count":0,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/81\/revisions"}],"wp:attachment":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}