{"id":321,"date":"2013-08-14T13:15:47","date_gmt":"2013-08-14T05:15:47","guid":{"rendered":"https:\/\/yanjingang.com\/blog\/?p=321"},"modified":"2018-12-17T20:36:40","modified_gmt":"2018-12-17T12:36:40","slug":"mongo-db-%e4%b8%8emysql-%e8%af%ad%e6%b3%95%e6%af%94%e8%be%83","status":"publish","type":"post","link":"https:\/\/yanjingang.com\/blog\/?p=321","title":{"rendered":"Mongo db \u4e0emysql \u8bed\u6cd5\u6bd4\u8f83"},"content":{"rendered":"<div>mongodb\u4e0emysql\u547d\u4ee4\u5bf9\u6bd4<\/div>\n<p>\u4f20\u7edf\u7684\u5173\u7cfb\u6570\u636e\u5e93\u4e00\u822c\u7531\u6570\u636e\u5e93\uff08database\uff09\u3001\u8868\uff08table\uff09\u3001\u8bb0\u5f55\uff08record\uff09\u4e09\u4e2a\u5c42\u6b21\u6982\u5ff5\u7ec4\u6210\uff0cMongoDB\u662f\u7531<strong>\u6570\u636e\u5e93\uff08database\uff09\u3001\u96c6\u5408\uff08collection\uff09\u3001\u6587\u6863\u5bf9\u8c61\uff08document\uff09<\/strong>\u4e09\u4e2a\u5c42\u6b21\u7ec4\u6210\u3002MongoDB\u5bf9\u4e8e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u91cc\u7684\u8868\uff0c\u4f46\u662f\u96c6\u5408\u4e2d\u6ca1\u6709\u5217\u3001\u884c\u548c\u5173\u7cfb\u6982\u5ff5\uff0c\u8fd9\u4f53\u73b0\u4e86\u6a21\u5f0f\u81ea\u7531\u7684\u7279\u70b9\u3002<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p align=\"center\"><strong>MySQL<\/strong><\/p>\n<\/td>\n<td valign=\"top\">\n<p align=\"center\"><strong>MongoDB<\/strong><\/p>\n<\/td>\n<td valign=\"top\">\n<p align=\"center\"><strong>\u8bf4\u660e<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">mysqld<\/td>\n<td valign=\"top\">mongod<\/td>\n<td valign=\"top\">\u670d\u52a1\u5668\u5b88\u62a4\u8fdb\u7a0b<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">mysql<\/td>\n<td valign=\"top\">mongo<\/td>\n<td valign=\"top\">\u5ba2\u6237\u7aef\u5de5\u5177<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">mysqldump<\/td>\n<td valign=\"top\">mongodump<\/td>\n<td valign=\"top\">\u903b\u8f91\u5907\u4efd\u5de5\u5177<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">mysql<\/td>\n<td valign=\"top\">mongorestore<\/td>\n<td valign=\"top\">\u903b\u8f91\u6062\u590d\u5de5\u5177<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<td valign=\"top\">db.repairDatabase()<\/td>\n<td valign=\"top\">\u4fee\u590d\u6570\u636e\u5e93<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">mysqldump<\/td>\n<td valign=\"top\">mongoexport<\/td>\n<td valign=\"top\">\u6570\u636e\u5bfc\u51fa\u5de5\u5177<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">source<\/td>\n<td valign=\"top\">mongoimport<\/td>\n<td valign=\"top\">\u6570\u636e\u5bfc\u5165\u5de5\u5177<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">grant * privileges on *.* to \u2026<\/td>\n<td valign=\"top\">Db.addUser()<\/p>\n<p>Db.auth()<\/td>\n<td valign=\"top\">\u65b0\u5efa\u7528\u6237\u5e76\u6743\u9650<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">show databases<\/td>\n<td valign=\"top\">show dbs<\/td>\n<td valign=\"top\">\u663e\u793a\u5e93\u5217\u8868<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Show tables<\/td>\n<td valign=\"top\">Show collections<\/td>\n<td valign=\"top\">\u663e\u793a\u8868\u5217\u8868<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Show slave status<\/td>\n<td valign=\"top\">Rs.status<\/td>\n<td valign=\"top\">\u67e5\u8be2\u4e3b\u4ece\u72b6\u6001<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Create table users(a int, b int)<\/td>\n<td valign=\"top\">db.createCollection(&#8220;mycoll&#8221;, {capped:true,<\/p>\n<p>size:100000}) \u53e6\uff1a\u53ef\u9690\u5f0f\u521b\u5efa\u8868\u3002<\/td>\n<td valign=\"top\">\u521b\u5efa\u8868<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Create INDEX idxname ON users(name)<\/td>\n<td valign=\"top\">db.users.ensureIndex({name:1})<\/td>\n<td valign=\"top\">\u521b\u5efa\u7d22\u5f15<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Create INDEX idxname ON users(name,ts DESC)<\/td>\n<td valign=\"top\">db.users.ensureIndex({name:1,ts:-1})<\/td>\n<td valign=\"top\">\u521b\u5efa\u7d22\u5f15<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Insert into users values(1, 1)<\/td>\n<td valign=\"top\">db.users.insert({a:1, b:1})<\/td>\n<td valign=\"top\">\u63d2\u5165\u8bb0\u5f55<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Select a, b from users<\/td>\n<td valign=\"top\">db.users.find({},{a:1, b:1})<\/td>\n<td valign=\"top\">\u67e5\u8be2\u8868<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Select * from users<\/td>\n<td valign=\"top\">db.users.find()<\/td>\n<td valign=\"top\">\u67e5\u8be2\u8868<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Select * from users where age=33<\/td>\n<td valign=\"top\">db.users.find({age:33})<\/td>\n<td valign=\"top\">\u6761\u4ef6\u67e5\u8be2<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Select a, b from users where age=33<\/td>\n<td valign=\"top\">db.users.find({age:33},{a:1, b:1})<\/td>\n<td valign=\"top\">\u6761\u4ef6\u67e5\u8be2<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">select * from users where age&lt;33<\/td>\n<td valign=\"top\">db.users.find({&#8216;age&#8217;:{$lt:33}})<\/td>\n<td valign=\"top\">\u6761\u4ef6\u67e5\u8be2<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">select * from users where age&gt;33 and age&lt;=40<\/td>\n<td valign=\"top\">db.users.find({&#8216;age&#8217;:{$gt:33,$lte:40}})<\/td>\n<td valign=\"top\">\u6761\u4ef6\u67e5\u8be2<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">select * from users where a=1 and b=&#8217;q&#8217;<\/td>\n<td valign=\"top\">db.users.find({a:1,b:&#8217;q&#8217;})<\/td>\n<td valign=\"top\">\u6761\u4ef6\u67e5\u8be2<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">select * from users where a=1 or b=2<\/td>\n<td valign=\"top\">db.users.find( { $or : [ { a : 1 } , { b : 2 } ] } )<\/td>\n<td valign=\"top\">\u6761\u4ef6\u67e5\u8be2<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">select * from users limit 1<\/td>\n<td valign=\"top\">db.users.findOne()<\/td>\n<td valign=\"top\">\u6761\u4ef6\u67e5\u8be2<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">select * from users where name like &#8220;%Joe%&#8221;<\/td>\n<td valign=\"top\">db.users.find({name:\/Joe\/})<\/td>\n<td valign=\"top\">\u6a21\u7cca\u67e5\u8be2<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">select * from users where name like &#8220;Joe%&#8221;<\/td>\n<td valign=\"top\">db.users.find({name:\/^Joe\/})<\/td>\n<td valign=\"top\">\u6a21\u7cca\u67e5\u8be2<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">select count(1) from users<\/td>\n<td valign=\"top\">Db.users.count()<\/td>\n<td valign=\"top\">\u83b7\u53d6\u8868\u8bb0\u5f55\u6570<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">select count(1) from users where age&gt;30<\/td>\n<td valign=\"top\">db.users.find({age: {&#8216;$gt&#8217;: 30}}).count()<\/td>\n<td valign=\"top\">\u83b7\u53d6\u8868\u8bb0\u5f55\u6570<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">select DISTINCT last_name from users<\/td>\n<td valign=\"top\">db.users.distinct(&#8216;last_name&#8217;)<\/td>\n<td valign=\"top\">\u53bb\u6389\u91cd\u590d\u503c<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">select * from users ORDER BY name<\/td>\n<td valign=\"top\">db.users.find().sort({name:-1})<\/td>\n<td valign=\"top\">\u6392\u5e8f<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">select * from users ORDER BY name DESC<\/td>\n<td valign=\"top\">db.users.find().sort({name:-1})<\/td>\n<td valign=\"top\">\u6392\u5e8f<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">EXPLAIN select * from users where z=3<\/td>\n<td valign=\"top\">db.users.find({z:3}).explain()<\/td>\n<td valign=\"top\">\u83b7\u53d6\u5b58\u50a8\u8def\u5f84<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">update users set a=1 where b=&#8217;q&#8217;<\/td>\n<td valign=\"top\">db.users.update({b:&#8217;q&#8217;}, {$set:{a:1}}, false, true)<\/td>\n<td valign=\"top\">\u66f4\u65b0\u8bb0\u5f55<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">update users set a=a+2 where b=&#8217;q&#8217;<\/td>\n<td valign=\"top\">db.users.update({b:&#8217;q&#8217;}, {$inc:{a:2}}, false, true)<\/td>\n<td valign=\"top\">\u66f4\u65b0\u8bb0\u5f55<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">delete from users where z=&#8221;abc&#8221;<\/td>\n<td valign=\"top\">db.users.remove({z:&#8217;abc&#8217;})<\/td>\n<td valign=\"top\">\u5220\u9664\u8bb0\u5f55<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<td valign=\"top\">db. users.remove()<\/td>\n<td valign=\"top\">\u5220\u9664\u6240\u6709\u7684\u8bb0\u5f55<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">drop database IF EXISTS test;<\/td>\n<td valign=\"top\">use test<\/p>\n<p>db.dropDatabase()<\/td>\n<td valign=\"top\">\u5220\u9664\u6570\u636e\u5e93<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">drop table IF EXISTS test;<\/td>\n<td valign=\"top\">db.mytable.drop()<\/td>\n<td valign=\"top\">\u5220\u9664\u8868\/collection<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<td valign=\"top\">db.addUser(\u2018test\u2019, \u2019test\u2019)<\/td>\n<td valign=\"top\">\u6dfb\u52a0\u7528\u6237<\/p>\n<p>readOnly&#8211;&gt;false<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<td valign=\"top\">db.addUser(\u2018test\u2019, \u2019test\u2019, true)<\/td>\n<td valign=\"top\">\u6dfb\u52a0\u7528\u6237<\/p>\n<p>readOnly&#8211;&gt;true<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<td valign=\"top\">db.addUser(&#8220;test&#8221;,&#8221;test222&#8243;)<\/td>\n<td valign=\"top\">\u66f4\u6539\u5bc6\u7801<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<td valign=\"top\">db.system.users.remove({user:&#8221;test&#8221;})<\/p>\n<p>\u6216\u8005db.removeUser(&#8216;test&#8217;)<\/td>\n<td valign=\"top\">\u5220\u9664\u7528\u6237<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<td valign=\"top\">use admin<\/td>\n<td valign=\"top\">\u8d85\u7ea7\u7528\u6237<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<td valign=\"top\">db.auth(\u2018test\u2019, \u2018test\u2019)<\/td>\n<td valign=\"top\">\u7528\u6237\u6388\u6743<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<td valign=\"top\">db.system.users.find()<\/td>\n<td valign=\"top\">\u67e5\u770b\u7528\u6237\u5217\u8868<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<td valign=\"top\">show users<\/td>\n<td valign=\"top\">\u67e5\u770b\u6240\u6709\u7528\u6237<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<td valign=\"top\">db.printCollectionStats()<\/td>\n<td valign=\"top\">\u67e5\u770b\u5404collection\u7684\u72b6\u6001<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<td valign=\"top\">db.printReplicationInfo()<\/td>\n<td valign=\"top\">\u67e5\u770b\u4e3b\u4ece\u590d\u5236\u72b6\u6001<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<td valign=\"top\">show profile<\/td>\n<td valign=\"top\">\u67e5\u770bprofiling<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<td valign=\"top\">db.copyDatabase(&#8216;mail_addr&#8217;,&#8217;mail_addr_tmp&#8217;)<\/td>\n<td valign=\"top\">\u62f7\u8d1d\u6570\u636e\u5e93<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<td valign=\"top\">db.users.dataSize()<\/td>\n<td valign=\"top\">\u67e5\u770bcollection\u6570\u636e\u7684\u5927\u5c0f<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<td valign=\"top\">db. users.totalIndexSize()<\/td>\n<td valign=\"top\">\u67e5\u8be2\u7d22\u5f15\u7684\u5927\u5c0f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p align=\"left\"><strong>mongodb\u8bed\u6cd5<\/strong><\/p>\n<p>MongoDB\u7684\u597d\u5904\u633a\u591a\u7684\uff0c\u6bd4\u5982\u591a\u5217\u7d22\u5f15\uff0c\u67e5\u8be2\u65f6\u53ef\u4ee5\u7528\u4e00\u4e9b\u7edf\u8ba1\u51fd\u6570\uff0c\u652f\u6301\u591a\u6761\u4ef6\u67e5\u8be2\uff0c\u4f46\u662f\u76ee\u524d\u591a\u8868\u67e5\u8be2\u662f\u4e0d\u652f\u6301\u7684\uff0c\u53ef\u4ee5\u60f3\u529e\u6cd5\u901a\u8fc7\u6570\u636e\u5197\u4f59\u6765\u89e3\u51b3\u591a\u8868\u67e5\u8be2\u7684\u95ee\u9898\u3002<\/p>\n<p>MongoDB\u5bf9\u6570\u636e\u7684\u64cd\u4f5c\u5f88\u4e30\u5bcc\uff0c\u4e0b\u9762\u505a\u4e00\u4e9b\u4e3e\u4f8b\u8bf4\u660e\uff0c\u5185\u5bb9\u5927\u90e8\u5206\u6765\u81ea\u5b98\u65b9\u6587\u6863\uff0c\u53e6\u5916\u6709\u90e8\u5206\u4e3a\u81ea\u5df1\u7406\u89e3\u3002<\/p>\n<p>\u67e5\u8be2colls\u6240\u6709\u6570\u636e<\/p>\n<p>db.colls.find() \/\/select * from colls<\/p>\n<p>\u901a\u8fc7\u6307\u5b9a\u6761\u4ef6\u67e5\u8be2<\/p>\n<p>db.colls.find({\u2018last_name\u2019: \u2018Smith\u2019});\/\/select * from colls where last_name=\u2019Smith\u2019<\/p>\n<p>\u6307\u5b9a\u591a\u6761\u4ef6\u67e5\u8be2<\/p>\n<p>db.colls.find( { x : 3, y : \u201cfoo\u201d } );\/\/select * from colls where x=3 and y=\u2019foo\u2019<\/p>\n<p>\u6307\u5b9a\u6761\u4ef6\u8303\u56f4\u67e5\u8be2<\/p>\n<p>db.colls.find({j: {$ne: 3}, k: {$gt: 10} });\/\/select * from colls where j!=3 and k&gt;10<\/p>\n<p>\u67e5\u8be2\u4e0d\u5305\u62ec\u67d0\u5185\u5bb9<\/p>\n<p>db.colls.find({}, {a:0});\/\/\u67e5\u8be2\u9664a\u4e3a0\u5916\u7684\u6240\u6709\u6570\u636e<\/p>\n<p>\u652f\u6301&lt;, &lt;=, &gt;, &gt;=\u67e5\u8be2\uff0c\u9700\u7528\u7b26\u53f7\u66ff\u4ee3\u5206\u522b\u4e3a$lt\uff0c$lte\uff0c$gt\uff0c$gte<\/p>\n<p>db.colls.find({ \u201cfield\u201d : { $gt: value } } );<\/p>\n<p>db.colls.find({ \u201cfield\u201d : { $lt: value } } );<\/p>\n<p>db.colls.find({ \u201cfield\u201d : { $gte: value } } );<\/p>\n<p>db.colls.find({ \u201cfield\u201d : { $lte: value } } );<\/p>\n<p>\u4e5f\u53ef\u5bf9\u67d0\u4e00\u5b57\u6bb5\u505a\u8303\u56f4\u67e5\u8be2<\/p>\n<p>db.colls.find({ \u201cfield\u201d : { $gt: value1, $lt: value2 } } );<\/p>\n<p>\u4e0d\u7b49\u4e8e\u67e5\u8be2\u7528\u5b57\u7b26$ne<\/p>\n<p>db.colls.find( { x : { $ne : 3 } } );<\/p>\n<p>in\u67e5\u8be2\u7528\u5b57\u7b26$in<\/p>\n<p>db.colls.find( { \u201cfield\u201d : { $in : array } } );<\/p>\n<p>db.colls.find({j:{$in: [2,4,6]}});<\/p>\n<p>not in\u67e5\u8be2\u7528\u5b57\u7b26$nin<\/p>\n<p>db.colls.find({j:{$nin: [2,4,6]}});<\/p>\n<p>\u53d6\u6a21\u67e5\u8be2\u7528\u5b57\u7b26$mod<\/p>\n<p>db.colls.find( { a : { $mod : [ 10 , 1 ] } } )\/\/ where a % 10 == 1<\/p>\n<p>$all\u67e5\u8be2<\/p>\n<p>db.colls.find( { a: { $all: [ 2, 3 ] } } );\/\/\u6307\u5b9aa\u6ee1\u8db3\u6570\u7ec4\u4e2d\u4efb\u610f\u503c\u65f6<\/p>\n<p>$size\u67e5\u8be2<\/p>\n<p>db.colls.find( { a : { $size: 1 } } );\/\/\u5bf9\u5bf9\u8c61\u7684\u6570\u91cf\u67e5\u8be2\uff0c\u6b64\u67e5\u8be2\u67e5\u8be2a\u7684\u5b50\u5bf9\u8c61\u6570\u76ee\u4e3a1\u7684\u8bb0\u5f55<\/p>\n<p>$exists\u67e5\u8be2<\/p>\n<p>db.colls.find( { a : { $exists : true } } ); \/\/ \u5b58\u5728a\u5bf9\u8c61\u7684\u6570\u636e<\/p>\n<p>db.colls.find( { a : { $exists : false } } ); \/\/ \u4e0d\u5b58\u5728a\u5bf9\u8c61\u7684\u6570\u636e<\/p>\n<p>$type\u67e5\u8be2$type\u503c\u4e3abson<a href=\"http:\/\/bsonspec.org\/\" target=\"_blank\" rel=\"noopener\">http:\/\/bsonspec.org\/<\/a>\u6570 \u636e\u7684\u7c7b\u578b\u503c<\/p>\n<p>db.colls.find( { a : { $type : 2 } } ); \/\/ \u5339\u914da\u4e3astring\u7c7b\u578b\u6570\u636e<\/p>\n<p>db.colls.find( { a : { $type : 16 } } ); \/\/ \u5339\u914da\u4e3aint\u7c7b\u578b\u6570\u636e<\/p>\n<p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d<\/p>\n<p>db.colls.find( { name : \/acme.*corp\/i } );\/\/\u7c7b\u4f3c\u4e8eSQL\u4e2dlike<\/p>\n<p>\u5185\u5d4c\u5bf9\u8c61\u67e5\u8be2<\/p>\n<p>db.colls.find( { \u201cauthor.name\u201d : \u201cjoe\u201d } );<\/p>\n<p>1.3.3\u7248\u672c\u53ca\u66f4\u9ad8\u7248\u672c\u5305\u542b$not\u67e5\u8be2<\/p>\n<p>db.colls.find( { name : { $not : \/acme.*corp\/i } } );<\/p>\n<p>db.colls.find( { a : { $not : { $mod : [ 10 , 1 ] } } } );<\/p>\n<p>sort()\u6392\u5e8f<\/p>\n<p>db.colls.find().sort( { ts : -1 } );\/\/1\u4e3a\u5347\u5e8f2\u4e3a\u964d\u5e8f<\/p>\n<p>limit()\u5bf9\u9650\u5236\u67e5\u8be2\u6570\u636e\u8fd4\u56de\u4e2a\u6570<\/p>\n<p>db.colls.find().limit(10)<\/p>\n<p>skip()\u8df3\u8fc7\u67d0\u4e9b\u6570\u636e<\/p>\n<p>db.colls.find().skip(10)<\/p>\n<p>snapshot()\u5feb\u7167\u4fdd\u8bc1\u6ca1\u6709\u91cd\u590d\u6570\u636e\u8fd4\u56de\u6216\u5bf9\u8c61\u4e22\u5931<\/p>\n<p>count()\u7edf\u8ba1\u67e5\u8be2\u5bf9\u8c61\u4e2a\u6570<\/p>\n<p>db.students.find({\u2018address.state\u2019 : \u2018CA\u2019}).count();\/\/\u6548\u7387\u8f83\u9ad8<\/p>\n<p>db.students.find({\u2018address.state\u2019 : \u2018CA\u2019}).toArray().length;\/\/\u6548\u7387\u5f88\u4f4e<\/p>\n<p>group()\u5bf9\u67e5\u8be2\u7ed3\u679c\u5206\u7ec4\u548cSQL\u4e2dgroup by\u51fd\u6570\u7c7b\u4f3c<\/p>\n<p>distinct()\u8fd4\u56de\u4e0d\u91cd\u590d\u503c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mongodb\u4e0emysql\u547d\u4ee4\u5bf9\u6bd4<br \/>\n\u4f20\u7edf\u7684\u5173\u7cfb\u6570\u636e\u5e93\u4e00\u822c\u7531\u6570\u636e\u5e93\uff08database\uff09\u3001\u8868\uff08table\uff09\u3001\u8bb0\u5f55\uff08record\uff09\u4e09\u4e2a\u5c42\u6b21\u6982\u5ff5\u7ec4\u6210\uff0cMongoDB\u662f\u7531\u6570\u636e\u5e93\uff08database\uff09\u3001\u96c6\u5408\uff08collection\uff09\u3001\u6587\u6863\u5bf9\u8c61\uff08document\uff09\u4e09\u4e2a\u5c42\u6b21\u7ec4\u6210\u3002MongoDB\u5bf9\u4e8e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u91cc\u7684\u8868\uff0c\u4f46\u662f\u96c6\u5408\u4e2d\u6ca1\u6709\u5217\u3001\u884c\u548c\u5173\u7cfb\u6982\u5ff5\uff0c\u8fd9\u4f53\u73b0\u4e86\u6a21\u5f0f\u81ea\u7531\u7684\u7279\u70b9\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,7],"tags":[22,8,225],"_links":{"self":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/321"}],"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=321"}],"version-history":[{"count":0,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/321\/revisions"}],"wp:attachment":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}