{"id":1091,"date":"2023-03-12T11:27:29","date_gmt":"2023-03-12T03:27:29","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1091"},"modified":"2023-04-29T11:28:17","modified_gmt":"2023-04-29T03:28:17","slug":"mongodb-type-operator","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/mongodb-type-operator\/","title":{"rendered":"MongoDB $type\u64cd\u4f5c\u7b26"},"content":{"rendered":"<p><code>$type<\/code>\u64cd\u4f5c\u7b26\u662f\u57fa\u4e8eBSON\u7c7b\u578b\u6765\u68c0\u7d22\u96c6\u5408\u4e2d\u5339\u914d\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<p>MongoDB \u4e2d\u53ef\u4ee5\u4f7f\u7528\u7684\u7c7b\u578b\u5982\u4e0b\u8868\u6240\u793a\uff1a<\/p>\n<p><!-- more --><\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u7c7b\u578b<\/th>\n<th style=\"text-align: center;\">\u6570\u5b57<\/th>\n<th style=\"text-align: center;\">\u5907\u6ce8<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">Double<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">String<\/td>\n<td style=\"text-align: center;\">2<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Object<\/td>\n<td style=\"text-align: center;\">3<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Array<\/td>\n<td style=\"text-align: center;\">4<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Binary data<\/td>\n<td style=\"text-align: center;\">5<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Undefined<\/td>\n<td style=\"text-align: center;\">6<\/td>\n<td style=\"text-align: center;\">\u5df2\u5e9f\u5f03<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Object id<\/td>\n<td style=\"text-align: center;\">7<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Boolean<\/td>\n<td style=\"text-align: center;\">8<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Date<\/td>\n<td style=\"text-align: center;\">9<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Null<\/td>\n<td style=\"text-align: center;\">10<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Regular Expression<\/td>\n<td style=\"text-align: center;\">11<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">JavaScript<\/td>\n<td style=\"text-align: center;\">13<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Symbol<\/td>\n<td style=\"text-align: center;\">14<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">JavaScript (with scope)<\/td>\n<td style=\"text-align: center;\">15<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">32-bit integer<\/td>\n<td style=\"text-align: center;\">16<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Timestamp<\/td>\n<td style=\"text-align: center;\">17<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">64-bit integer<\/td>\n<td style=\"text-align: center;\">18<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Min key<\/td>\n<td style=\"text-align: center;\">255<\/td>\n<td style=\"text-align: center;\">Query with -1<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Max key<\/td>\n<td style=\"text-align: center;\">127<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6211\u4eec\u4f7f\u7528\u7684\u6570\u636e\u5e93\u540d\u79f0\u4e3a &quot;appblog&quot;\uff0c\u96c6\u5408\u540d\u79f0\u4e3a &quot;col&quot;<\/p>\n<p>\u63d2\u5165\u4ee5\u4e0b\u6570\u636e<\/p>\n<pre><code class=\"language-javascript\">> db.col.insert({\n    title: &#039;PHP&#039;, \n    description: &#039;PHP \u662f\u4e00\u79cd\u521b\u5efa\u52a8\u6001\u4ea4\u4e92\u6027\u7ad9\u70b9\u7684\u5f3a\u6709\u529b\u7684\u670d\u52a1\u5668\u7aef\u811a\u672c\u8bed\u8a00\u3002&#039;,\n    by: &#039;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&#039;,\n    url: &#039;http:\/\/www.appblog.cn&#039;,\n    tags: [&#039;php&#039;],\n    likes: 200\n})\n> db.col.insert({title: &#039;Java&#039;, \n    description: &#039;Java \u662f\u7531Sun\u516c\u53f8\u4e8e1995\u5e745\u6708\u63a8\u51fa\u7684\u9ad8\u7ea7\u7a0b\u5e8f\u8bbe\u8ba1\u8bed\u8a00\u3002&#039;,\n    by: &#039;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&#039;,\n    url: &#039;http:\/\/www.appblog.cn&#039;,\n    tags: [&#039;java&#039;],\n    likes: 150\n})\n> db.col.insert({title: &#039;MongoDB&#039;, \n    description: &#039;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&#039;,\n    by: &#039;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&#039;,\n    url: &#039;http:\/\/www.appblog.cn&#039;,\n    tags: [&#039;mongodb&#039;],\n    likes: 100\n})<\/code><\/pre>\n<p>\u4f7f\u7528find()\u547d\u4ee4\u67e5\u770b\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.col.find()\n{ &quot;_id&quot; : ObjectId(&quot;5aefe63c2d2577acadf1e0d2&quot;), &quot;title&quot; : &quot;PHP&quot;, &quot;description&quot; : &quot;PHP \u662f\u4e00\u79cd\u521b\u5efa\u52a8\u6001\u4ea4\u4e92\u6027\u7ad9\u70b9\u7684\u5f3a\u6709\u529b\u7684\u670d\u52a1\u5668\u7aef\u811a\u672c\u8bed\u8a00\u3002&quot;, &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;, &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;, &quot;tags&quot; : [ &quot;php&quot; ], &quot;likes&quot; : 200 }\n{ &quot;_id&quot; : ObjectId(&quot;5aefe6462d2577acadf1e0d3&quot;), &quot;title&quot; : &quot;Java&quot;, &quot;description&quot; : &quot;Java \u662f\u7531Sun\u516c\u53f8\u4e8e1995\u5e745\u6708\u63a8\u51fa\u7684\u9ad8\u7ea7\u7a0b\u5e8f\u8bbe\u8ba1\u8bed\u8a00\u3002&quot;, &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;, &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;, &quot;tags&quot; : [ &quot;java&quot; ], &quot;likes&quot; : 150 }\n{ &quot;_id&quot; : ObjectId(&quot;5aefe64f2d2577acadf1e0d4&quot;), &quot;title&quot; : &quot;MongoDB&quot;, &quot;description&quot; : &quot;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&quot;, &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;, &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;, &quot;tags&quot; : [ &quot;mongodb&quot; ], &quot;likes&quot; : 100 }<\/code><\/pre>\n<h2>MongoDB \u64cd\u4f5c\u7b26 &#8211; $type \u5b9e\u4f8b<\/h2>\n<p>\u5982\u679c\u60f3\u83b7\u53d6 &quot;col&quot; \u96c6\u5408\u4e2d title \u4e3a String \u7684\u6570\u636e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code class=\"language-javascript\">db.col.find({&quot;title&quot; : {$type : 2}})<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.col.find({&quot;title&quot; : {$type : 2}})\n{ &quot;_id&quot; : ObjectId(&quot;5aefe63c2d2577acadf1e0d2&quot;), &quot;title&quot; : &quot;PHP&quot;, &quot;description&quot; : &quot;PHP \u662f\u4e00\u79cd\u521b\u5efa\u52a8\u6001\u4ea4\u4e92\u6027\u7ad9\u70b9\u7684\u5f3a\u6709\u529b\u7684\u670d\u52a1\u5668\u7aef\u811a\u672c\u8bed\u8a00\u3002&quot;, &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;, &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;, &quot;tags&quot; : [ &quot;php&quot; ], &quot;likes&quot; : 200 }\n{ &quot;_id&quot; : ObjectId(&quot;5aefe6462d2577acadf1e0d3&quot;), &quot;title&quot; : &quot;Java&quot;, &quot;description&quot; : &quot;Java \u662f\u7531Sun\u516c\u53f8\u4e8e1995\u5e745\u6708\u63a8\u51fa\u7684\u9ad8\u7ea7\u7a0b\u5e8f\u8bbe\u8ba1\u8bed\u8a00\u3002&quot;, &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;, &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;, &quot;tags&quot; : [ &quot;java&quot; ], &quot;likes&quot; : 150 }\n{ &quot;_id&quot; : ObjectId(&quot;5aefe64f2d2577acadf1e0d4&quot;), &quot;title&quot; : &quot;MongoDB&quot;, &quot;description&quot; : &quot;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&quot;, &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;, &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;, &quot;tags&quot; : [ &quot;mongodb&quot; ], &quot;likes&quot; : 100 }\n> <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>$type\u64cd\u4f5c\u7b26\u662f\u57fa\u4e8eBSON\u7c7b\u578b\u6765\u68c0\u7d22\u96c6\u5408\u4e2d\u5339\u914d\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002 MongoDB \u4e2d\u53ef\u4ee5\u4f7f\u7528\u7684\u7c7b\u578b\u5982 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[300],"tags":[],"class_list":["post-1091","post","type-post","status-publish","format-standard","hentry","category-mongodb"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1091"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1091\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}