{"id":92437,"date":"2025-11-24T11:17:57","date_gmt":"2025-11-24T04:17:57","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=92437"},"modified":"2025-11-24T11:18:01","modified_gmt":"2025-11-24T04:18:01","slug":"truy-van-trong-mongodb","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/","title":{"rendered":"MongoDB Cheat sheet: T\u1ed5ng h\u1ee3p t\u1ea5t c\u1ea3 truy v\u1ea5n trong MongoDB"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">N\u1ed9i dung b\u00e0i vi\u1ebft<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/#Tong_quan_ve_MongoDB\" >T\u1ed5ng quan v\u1ec1 MongoDB<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/#Cac_loai_truy_van_trong_MongoDB\" >C\u00e1c lo\u1ea1i truy v\u1ea5n trong MongoDB<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/#Tong_hop_cac_lenh_truy_van_trong_MongoDB\" >T\u1ed5ng h\u1ee3p c\u00e1c l\u1ec7nh truy v\u1ea5n trong MongoDB<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/#Lam_the_nao_de_toi_uu_truy_van_trong_MongoDB\" >L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 t\u1ed1i \u01b0u truy v\u1ea5n trong MongoDB?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/#Cau_hoi_thuong_gap_ve_cac_truy_van_trong_MongoDB\" >C\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 c\u00e1c truy v\u1ea5n trong MongoDB<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/#Tong_ket\" >T\u1ed5ng k\u1ebft<\/a><\/li><\/ul><\/nav><\/div>\n\n<p><strong><em>Khi l\u00e0m vi\u1ec7c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u, kh\u1ea3 n\u0103ng truy v\u1ea5n d\u1eef li\u1ec7u l\u00e0 k\u1ef9 n\u0103ng c\u1ed1t l\u00f5i m\u00e0 b\u1ea5t k\u1ef3 l\u1eadp tr\u00ecnh vi\u00ean n\u00e0o c\u0169ng c\u1ea7n n\u1eafm v\u1eefng. V\u1edbi MongoDB &#8211; c\u01a1 s\u1edf d\u1eef li\u1ec7u NoSQL ph\u1ed5 bi\u1ebfn nh\u1ea5t hi\u1ec7n nay &#8211; c\u00fa ph\u00e1p truy v\u1ea5n kh\u00e1 linh ho\u1ea1t, m\u1ea1nh m\u1ebd v\u00e0 g\u1ea7n g\u0169i nh\u1edd \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf d\u1ef1a tr\u00ean JSON. B\u00e0i vi\u1ebft n\u00e0y s\u1ebd gi\u00fap b\u1ea1n c\u00f3 m\u1ed9t c\u00e1i nh\u00ecn r\u00f5 r\u00e0ng h\u01a1n v\u1ec1 c\u00e1ch truy v\u1ea5n trong MongoDB.<\/em><\/strong><\/p>\n\n\n\n<p>\u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u r\u00f5 h\u01a1n v\u1ec1:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C\u00e1c lo\u1ea1i truy v\u1ea5n trong MongoDB<\/li>\n\n\n\n<li>M\u1ed9t s\u1ed1 c\u00e1ch t\u1ed1i \u01b0u truy v\u1ea5n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-t\u1ed5ng-quan-v\u1ec1-mongodb\"><span class=\"ez-toc-section\" id=\"Tong_quan_ve_MongoDB\"><\/span><strong>T\u1ed5ng quan v\u1ec1 MongoDB<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>MongoDB l\u00e0 m\u1ed9t <a href=\"https:\/\/itviec.com\/blog\/cac-loai-co-so-du-lieu-nosql\/\" target=\"_blank\" rel=\"noreferrer noopener\">h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u NoSQL<\/a> d\u1ea1ng t\u00e0i li\u1ec7u (document-oriented database). N\u00f3 \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf \u0111\u1ec3 x\u1eed l\u00fd l\u01b0\u1ee3ng d\u1eef li\u1ec7u l\u1edbn m\u1ed9t c\u00e1ch nhanh ch\u00f3ng v\u00e0 linh ho\u1ea1t.\u00a0<\/p>\n\n\n\n<p>Thay v\u00ec l\u01b0u tr\u1eef d\u1eef li\u1ec7u trong c\u00e1c b\u1ea3ng (tables) v\u1edbi h\u00e0ng v\u00e0 c\u1ed9t c\u1ed1 \u0111\u1ecbnh nh\u01b0 c\u01a1 s\u1edf d\u1eef li\u1ec7u quan h\u1ec7 (RDBMS), MongoDB s\u1eed d\u1ee5ng document (t\u00e0i li\u1ec7u) l\u00e0m \u0111\u01a1n v\u1ecb l\u01b0u tr\u1eef ch\u00ednh. M\u1ed7i document \u0111\u01b0\u1ee3c bi\u1ec3u di\u1ec5n d\u01b0\u1edbi d\u1ea1ng BSON (Binary JSON) &#8211; m\u1ed9t \u0111\u1ecbnh d\u1ea1ng nh\u1ecb ph\u00e2n m\u1edf r\u1ed9ng c\u1ee7a JSON, h\u1ed7 tr\u1ee3 th\u00eam c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u nh\u01b0 Date, ObjectId, Binary data, c\u00f3 th\u1ec3 linh ho\u1ea1t thay \u0111\u1ed5i v\u1ec1 s\u1ed1 l\u01b0\u1ee3ng v\u00e0 ki\u1ec3u d\u1eef li\u1ec7u c\u1ee7a c\u00e1c tr\u01b0\u1eddng (fields).&nbsp;<\/p>\n\n\n\n<p>Ch\u00ednh s\u1ef1 linh ho\u1ea1t n\u00e0y gi\u00fap MongoDB \u0111\u1eb7c bi\u1ec7t ph\u00f9 h\u1ee3p v\u1edbi c\u00e1c \u1ee9ng d\u1ee5ng hi\u1ec7n \u0111\u1ea1i khi d\u1eef li\u1ec7u thay \u0111\u1ed5i li\u00ean t\u1ee5c v\u00e0 kh\u00f3 \u201c\u00e9p bu\u1ed9c\u201d v\u00e0o m\u1ed9t schema c\u1ee9ng nh\u1eafc.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>\u0110\u1ecdc chi ti\u1ebft: <strong><a href=\"https:\/\/itviec.com\/blog\/mongodb-la-gi\/\" target=\"_blank\" rel=\"noreferrer noopener\">MongoDB l\u00e0 g\u00ec? \u0110\u1ecbnh ngh\u0129a v\u00e0 Hi\u1ec3u r\u00f5 A-Z v\u1ec1 MongoDB<\/a><\/strong><\/em><\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-cac-khai-ni\u1ec7m-c\u01a1-b\u1ea3n-trong-mongodb\"><strong>C\u00e1c kh\u00e1i ni\u1ec7m c\u01a1 b\u1ea3n trong MongoDB<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Database (C\u01a1 s\u1edf d\u1eef li\u1ec7u):<\/strong> T\u1eadp h\u1ee3p c\u00e1c collections (b\u1ed9 s\u01b0u t\u1eadp). M\u1ed9t instance MongoDB c\u00f3 th\u1ec3 ch\u1ee9a nhi\u1ec1u databases \u0111\u1ed9c l\u1eadp v\u1edbi nhau.<\/li>\n\n\n\n<li><strong>Collection:<\/strong> C\u00f3 th\u1ec3 h\u00ecnh dung nh\u01b0 \u201cb\u1ea3ng\u201d trong SQL, nh\u01b0ng kh\u00f4ng b\u1ecb r\u00e0ng bu\u1ed9c b\u1edfi c\u1ea5u tr\u00fac c\u1ee9ng. M\u1ed7i collection c\u00f3 th\u1ec3 ch\u1ee9a nhi\u1ec1u document v\u1edbi c\u1ea5u tr\u00fac kh\u00e1c nhau.<\/li>\n\n\n\n<li><strong>Document (T\u00e0i li\u1ec7u):<\/strong> \u0110\u01a1n v\u1ecb d\u1eef li\u1ec7u c\u01a1 b\u1ea3n trong MongoDB, l\u01b0u d\u01b0\u1edbi d\u1ea1ng BSON (hi\u1ec3n th\u1ecb d\u01b0\u1edbi d\u1ea1ng JSON khi thao t\u00e1c). V\u00ed d\u1ee5:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>{ \"name\": \"Alice\", \"age\": 25, \"email\": \"alice@example.com\" }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Field (Tr\u01b0\u1eddng):<\/strong> C\u00e1c c\u1eb7p key-value trong document. V\u00ed d\u1ee5: <code>name: \"Alice\"<\/code> ch\u00ednh l\u00e0 m\u1ed9t field.<\/li>\n\n\n\n<li><strong>_id:<\/strong> M\u1ed7i document \u0111\u1ec1u c\u00f3 m\u1ed9t tr\u01b0\u1eddng _id duy nh\u1ea5t \u0111\u00f3ng vai tr\u00f2 l\u00e0 primary key, \u0111\u01b0\u1ee3c MongoDB t\u1ef1 \u0111\u1ed9ng t\u1ea1o n\u1ebfu kh\u00f4ng ch\u1ec9 \u0111\u1ecbnh.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-di\u1ec3m-n\u1ed5i-b\u1eadt-c\u1ee7a-mongodb-la-gi\"><strong>\u0110i\u1ec3m n\u1ed5i b\u1eadt c\u1ee7a MongoDB l\u00e0 g\u00ec?<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Linh ho\u1ea1t v\u1ec1 schema:<\/strong> Kh\u00f4ng c\u1ea7n \u0111\u1ecbnh ngh\u0129a tr\u01b0\u1edbc, c\u00f3 th\u1ec3 th\u00eam ho\u1eb7c b\u1edbt tr\u01b0\u1eddng khi d\u1eef li\u1ec7u thay \u0111\u1ed5i m\u00e0 kh\u00f4ng c\u1ea7n chuy\u1ec3n \u0111\u1ed5i (migration) ph\u1ee9c t\u1ea1p nh\u01b0 RDBMS.<\/li>\n\n\n\n<li><strong>M\u1edf r\u1ed9ng d\u1ec5 d\u00e0ng:<\/strong> Nh\u1edd c\u01a1 ch\u1ebf sharding (ph\u00e2n m\u1ea3nh ngang), d\u1eef li\u1ec7u c\u00f3 th\u1ec3 chia nh\u1ecf v\u00e0 ph\u00e2n ph\u1ed1i tr\u00ean nhi\u1ec1u m\u00e1y ch\u1ee7, \u0111\u1ea3m b\u1ea3o h\u1ec7 th\u1ed1ng v\u1eadn h\u00e0nh t\u1ed1t ngay c\u1ea3 khi l\u01b0\u1ee3ng d\u1eef li\u1ec7u kh\u1ed5ng l\u1ed3.<br><strong>T\u00ednh s\u1eb5n s\u00e0ng cao:<\/strong> V\u1edbi replication (c\u01a1 ch\u1ebf Replica Set), d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c sao ch\u00e9p sang nhi\u1ec1u m\u00e1y ch\u1ee7, gi\u00fap gi\u1ea3m thi\u1ec3u r\u1ee7i ro m\u1ea5t m\u00e1t khi c\u00f3 s\u1ef1 c\u1ed1.<br><strong>Truy v\u1ea5n phong ph\u00fa:<\/strong> H\u1ed7 tr\u1ee3 t\u1eeb c\u00e1c c\u00e2u l\u1ec7nh \u0111\u01a1n gi\u1ea3n \u0111\u1ebfn n\u00e2ng cao nh\u01b0 text search, truy v\u1ea5n \u0111\u1ecba l\u00fd (geospatial queries) hay pipeline t\u1ed5ng h\u1ee3p (aggregation pipeline) \u0111\u1ec3 x\u1eed l\u00fd d\u1eef li\u1ec7u ph\u1ee9c t\u1ea1p.<\/li>\n\n\n\n<li><strong>Th\u00e2n thi\u1ec7n v\u1edbi h\u1ec7 sinh th\u00e1i hi\u1ec7n \u0111\u1ea1i:<\/strong> \u0110\u01b0\u1ee3c s\u1eed d\u1ee5ng r\u1ed9ng r\u00e3i trong ph\u00e1t tri\u1ec3n web, \u1ee9ng d\u1ee5ng di \u0111\u1ed9ng (mobile), big data, ph\u00e2n t\u00edch d\u1eef li\u1ec7u th\u1eddi gian th\u1ef1c (real-time analytics) v\u00e0 c\u1ea3 IoT (Internet of Things).&nbsp;<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cac-lo\u1ea1i-truy-v\u1ea5n-trong-mongodb\"><span class=\"ez-toc-section\" id=\"Cac_loai_truy_van_trong_MongoDB\"><\/span><strong>C\u00e1c lo\u1ea1i truy v\u1ea5n trong MongoDB<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Tr\u01b0\u1edbc khi t\u00ecm hi\u1ec3u c\u00e1c lo\u1ea1i truy v\u1ea5n, ta c\u1ea7n chu\u1ea9n b\u1ecb s\u1eb5n m\u1ed9t collection m\u1eabu \u0111\u1ec3 d\u1ec5 th\u1ef1c h\u00e0nh. Gi\u1ea3 s\u1eed ch\u00fang ta \u0111ang c\u00f3 m\u1ed9t collection <code>users<\/code> trong database <code>test<\/code>, d\u1eef li\u1ec7u ban \u0111\u1ea7u nh\u01b0 sau:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.test.insertMany(&#91;\n  { name: \"Alice\", age: 25, email: \"alice@gmail.com\", gender: \"Female\", interests: &#91;\"Reading\", \"Traveling\"] },\n  { name: \"Bob\", age: 30, email: \"bob@yahoo.com\", gender: \"Male\", interests: &#91;\"Gaming\", \"Cooking\"] },\n  { name: \"Charlie\", age: 28, email: \"charlie@gmail.com\", gender: \"Male\", interests: &#91;\"Sports\", \"Traveling\"] },\n  { name: \"David\", age: 35, email: \"david@hotmail.com\", gender: \"Male\", interests: &#91;\"Cooking\", \"Reading\"] },\n  { name: \"Eva\", age: 22, email: \"eva@gmail.com\", gender: \"Female\", interests: &#91;\"Dancing\", \"Music\"] }\n])<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-truy-v\u1ea5n-d\u01a1n-gi\u1ea3n\"><strong>Truy v\u1ea5n \u0111\u01a1n gi\u1ea3n<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u0110\u1ec3 l\u1ea5y to\u00e0n b\u1ed9 d\u1eef li\u1ec7u trong collection ta d\u00f9ng c\u00fa ph\u00e1p:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find()<\/code><\/pre>\n\n\n\n<ul start=\"2\" class=\"wp-block-list\">\n<li>\u0110\u1ec3 l\u1ea5y b\u1ea3n ghi \u0111\u1ea7u ti\u00ean ta d\u00f9ng c\u00fa ph\u00e1p:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.findOne()<\/code><\/pre>\n\n\n\n<p>K\u1ebft qu\u1ea3: Tr\u1ea3 v\u1ec1 user \u0111\u1ea7u ti\u00ean MongoDB t\u00ecm th\u1ea5y l\u00e0 <code>Alice<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-truy-v\u1ea5n-v\u1edbi-di\u1ec1u-ki\u1ec7n\"><strong>Truy v\u1ea5n v\u1edbi \u0111i\u1ec1u ki\u1ec7n<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>T\u00ecm b\u1ea3n ghi v\u1edbi \u0111i\u1ec1u ki\u1ec7n c\u1ee5 th\u1ec3: V\u00ed d\u1ee5 \u0111\u1ec3 t\u00ecm user c\u00f3 t\u00ean l\u00e0 Alice:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({ name: \"Alice\" })<\/code><\/pre>\n\n\n\n<ul start=\"2\" class=\"wp-block-list\">\n<li>T\u00ecm user v\u1eeba c\u00f3 t\u00ean Alice v\u1eeba c\u00f3 tu\u1ed5i 25:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({ name: \"Alice\", age: 25 })<\/code><\/pre>\n\n\n\n<p>K\u1ebft qu\u1ea3: MongoDB ch\u1ec9 tr\u1ea3 v\u1ec1 document c\u1ee7a Alice n\u1ebfu \u0111\u1ed3ng th\u1eddi th\u1ecfa c\u1ea3 2 \u0111i\u1ec1u ki\u1ec7n (\u0111\u00e2y l\u00e0 to\u00e1n t\u1eed <code>AND<\/code> ng\u1ea7m \u0111\u1ecbnh).<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Truy v\u1ea5n v\u1edbi nhi\u1ec1u \u0111i\u1ec1u ki\u1ec7n (<\/strong><code>AND<\/code><strong>)<\/strong><\/li>\n<\/ul>\n\n\n\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 t\u00ecm user l\u00e0 nam v\u00e0 c\u00f3 tu\u1ed5i > 25 ta d\u00f9ng:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({ $and: &#91; { age: { $gt: 25 } }, { gender: \"Male\" } ] })<\/code><\/pre>\n\n\n\n<p>K\u1ebft qu\u1ea3 s\u1ebd tr\u1ea3 v\u1ec1 Bob (30), Charlie (28), David (35).<\/p>\n\n\n\n<p><strong>L\u01b0u \u00fd<\/strong>: Trong h\u1ea7u h\u1ebft tr\u01b0\u1eddng h\u1ee3p, b\u1ea1n kh\u00f4ng c\u1ea7n d\u00f9ng $and m\u1ed9t c\u00e1ch t\u01b0\u1eddng minh, v\u00ec MongoDB m\u1eb7c \u0111\u1ecbnh \u00e1p d\u1ee5ng <code>AND<\/code> khi c\u00f3 nhi\u1ec1u \u0111i\u1ec1u ki\u1ec7n. C\u00fa ph\u00e1p \u0111\u01a1n gi\u1ea3n h\u01a1n:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({ age: { $gt: 25 }, gender: \"Male\" })<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Truy v\u1ea5n v\u1edbi to\u00e1n t\u1eed OR<\/strong><\/li>\n<\/ul>\n\n\n\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 t\u00ecm user c\u00f3 tu\u1ed5i &lt; 25 ho\u1eb7c l\u00e0 n\u1eef ta c\u00f3 c\u00fa ph\u00e1p nh\u01b0 sau:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({ $or: &#91; { age: { $lt: 25 } }, { gender: \"Female\" } ] })<\/code><\/pre>\n\n\n\n<p>K\u1ebft qu\u1ea3 s\u1ebd tr\u1ea3 v\u1ec1 Alice (25, Female) v\u00e0 Eva (22, Female).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-truy-v\u1ea5n-v\u1edbi-toan-t\u1eed\"><strong>Truy v\u1ea5n v\u1edbi to\u00e1n t\u1eed<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>To\u00e1n t\u1eed<\/strong><\/td><td><strong>M\u00f4 t\u1ea3<\/strong><\/td><td><strong>V\u00ed d\u1ee5<\/strong><\/td><\/tr><tr><td><code>$gt<\/code><\/td><td>L\u1edbn h\u01a1n<\/td><td><code>db.users.find({ age: { $gt: 25 } })<\/code><\/td><\/tr><tr><td><code>$gte<\/code><\/td><td>L\u1edbn h\u01a1n ho\u1eb7c b\u1eb1ng<\/td><td><code>db.users.find({ age: { $gte: 30 } })<\/code><\/td><\/tr><tr><td><code>$lt<\/code><\/td><td>Nh\u1ecf h\u01a1n<\/td><td><code>db.users.find({ age: { $lt: 30 } })<\/code><\/td><\/tr><tr><td><code>$lte<\/code><\/td><td>Nh\u1ecf h\u01a1n ho\u1eb7c b\u1eb1ng<\/td><td><code>db.users.find({ age: { $lte: 25 } })<\/code><\/td><\/tr><tr><td><code>$ne<\/code><\/td><td>Kh\u00e1c v\u1edbi<\/td><td><code>db.users.find({ gender: { $ne: \"Male\" } })<\/code><\/td><\/tr><tr><td><code>$in<\/code><\/td><td>Trong danh s\u00e1ch<\/td><td><code>db.users.find({ name: { $in: [\"Alice\", \"Eva\"] } })<\/code><\/td><\/tr><tr><td><code>$nin<\/code><\/td><td>Kh\u00f4ng n\u1eb1m trong danh s\u00e1ch<\/td><td><code>db.users.find({ name: { $nin: [\"Alice\", \"Eva\"] } })<\/code><\/td><\/tr><tr><td><code>$regex<\/code><\/td><td>Kh\u1edbp chu\u1ed7i<\/td><td><code>db.users.find({ email: { $regex: \/gmail\/ } })<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>V\u00ed d\u1ee5 \u0111\u1ec3 t\u00ecm user c\u00f3 tu\u1ed5i l\u1edbn h\u01a1n 25:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({ age: { $gt: 25 } })<\/code><\/pre>\n\n\n\n<p>K\u1ebft qu\u1ea3: V\u1edbi c\u00e2u l\u1ec7nh tr\u00ean s\u1ebd tr\u1ea3 v\u1ec1 Bob (30), Charlie (28), David (35).<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>V\u00ed d\u1ee5 \u0111\u1ec3 t\u00ecm user c\u00f3 email ch\u1ee9a \u201cgmail\u201d:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({ email: { $regex: \/gmail\/ } })<\/code><\/pre>\n\n\n\n<p>K\u1ebft qu\u1ea3 s\u1ebd tr\u1ea3 v\u1ec1 Alice, Charlie, Eva.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>V\u00ed d\u1ee5 \u0111\u1ec3 t\u00ecm user c\u00f3 t\u00ean trong danh s\u00e1ch [&#8220;Alice&#8221;, &#8220;Eva&#8221;]:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({ name: { $in: &#91;\"Alice\", \"Eva\"] } })<\/code><\/pre>\n\n\n\n<p>K\u1ebft qu\u1ea3 s\u1ebd tr\u1ea3 v\u1ec1 Alice v\u00e0 Eva.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-m\u1ed9t-s\u1ed1-cau-truy-v\u1ea5n-trong-mongodb-khac\"><strong>M\u1ed9t s\u1ed1 c\u00e2u truy v\u1ea5n trong MongoDB kh\u00e1c<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-projection-field-filtering-ch\u1ec9-l\u1ea5y-tr\u01b0\u1eddng-c\u1ea7n-thi\u1ebft\"><strong>Projection \/ Field Filtering (ch\u1ec9 l\u1ea5y tr\u01b0\u1eddng c\u1ea7n thi\u1ebft)<\/strong><\/h4>\n\n\n\n<p>\u0110\u00f4i khi d\u1eef li\u1ec7u c\u00f3 r\u1ea5t nhi\u1ec1u field, nh\u01b0ng b\u1ea1n ch\u1ec9 mu\u1ed1n xem m\u1ed9t v\u00e0i field quan tr\u1ecdng. V\u00ed d\u1ee5 \u0111\u1ec3 ch\u1ec9 l\u1ea5y tr\u01b0\u1eddng name v\u00e0 email, b\u1ecf qua c\u00e1c field kh\u00e1c th\u00ec ta d\u00f9ng c\u00fa ph\u00e1p:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({}, { name: 1, email: 1, _id: 0 })<\/code><\/pre>\n\n\n\n<p>K\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1 ch\u1ec9 hi\u1ec3n th\u1ecb <code>name<\/code> v\u00e0 <code>email<\/code> m\u00e0 kh\u00f4ng c\u00f3 <code>_id<\/code>. Trong \u0111\u00f3:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1<\/code>: ngh\u0129a l\u00e0 bao g\u1ed3m (include)<\/li>\n\n\n\n<li><code>0<\/code>: ngh\u0129a l\u00e0 lo\u1ea1i tr\u1eeb (exclude)<\/li>\n<\/ul>\n\n\n\n<p><strong>L\u01b0u \u00fd<\/strong>: B\u1ea1n kh\u00f4ng th\u1ec3 k\u1ebft h\u1ee3p inclusion v\u00e0 exclusion trong c\u00f9ng m\u1ed9t truy v\u1ea5n, ngo\u1ea1i tr\u1eeb tr\u01b0\u1eddng h\u1ee3p \u0111\u1eb7c bi\u1ec7t v\u1edbi _id.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-sorting-s\u1eafp-x\u1ebfp-d\u1eef-li\u1ec7u\"><strong>Sorting (S\u1eafp x\u1ebfp d\u1eef li\u1ec7u)<\/strong><\/h4>\n\n\n\n<p>D\u00f9ng \u0111\u1ec3 s\u1eafp x\u1ebfp k\u1ebft qu\u1ea3 theo m\u1ed9t ho\u1eb7c nhi\u1ec1u tr\u01b0\u1eddng. V\u00ed d\u1ee5 \u0111\u1ec3 s\u1eafp x\u1ebfp danh s\u00e1ch user theo tu\u1ed5i gi\u1ea3m d\u1ea7n, ta d\u00f9ng c\u00fa ph\u00e1p:<\/p>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\"><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find().sort({ age: -1 })<\/code><\/pre>\n\n\n\n<p>K\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1 danh s\u00e1ch user t\u1eeb ng\u01b0\u1eddi l\u1edbn tu\u1ed5i nh\u1ea5t \u0111\u1ebfn tr\u1ebb nh\u1ea5t. Trong \u0111\u00f3:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>-1<\/code>: l\u00e0 gi\u1ea3m d\u1ea7n<\/li>\n\n\n\n<li><code>1<\/code>: l\u00e0 t\u0103ng d\u1ea7n<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-limit-gi\u1edbi-h\u1ea1n-s\u1ed1-k\u1ebft-qu\u1ea3\"><strong>Limit (Gi\u1edbi h\u1ea1n s\u1ed1 k\u1ebft qu\u1ea3)<\/strong><\/h4>\n\n\n\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 ch\u1ec9 l\u1ea5y 2 user \u0111\u1ea7u ti\u00ean trong t\u1eadp k\u1ebft qu\u1ea3, ta d\u00f9ng c\u00fa ph\u00e1p:<\/p>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\"><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find().limit(2)<\/code><\/pre>\n\n\n\n<p>K\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1 ch\u1ec9 g\u1ed3m 2 document \u0111\u1ea7u ti\u00ean c\u1ee7a collection.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-skip-b\u1ecf-qua-s\u1ed1-b\u1ea3n-ghi\"><strong>Skip (B\u1ecf qua s\u1ed1 b\u1ea3n ghi)<\/strong><\/h4>\n\n\n\n<p>Th\u01b0\u1eddng k\u1ebft h\u1ee3p v\u1edbi limit \u0111\u1ec3 ph\u00e2n trang. V\u00ed d\u1ee5 b\u1ecf qua 2 b\u1ea3n ghi \u0111\u1ea7u v\u00e0 l\u1ea5y 2 b\u1ea3n ghi ti\u1ebfp theo:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.countDocuments({ gender: \"Female\" })<\/code><\/pre>\n\n\n\n<p>H\u1eefu \u00edch cho ph\u00e2n trang (pagination). V\u00ed d\u1ee5: trang 1 s\u1ebd l\u1ea5y c\u00e1c b\u1ea3n ghi b\u1eb1ng <code>skip(0).limit(10)<\/code>, c\u00f2n trang 2 l\u00e0 <code>skip(10).limit(10)<\/code> &#8211; ngh\u0129a l\u00e0 b\u1ecf qua 10 b\u1ea3n ghi \u0111\u1ea7u ti\u00ean v\u00e0 l\u1ea5y ti\u1ebfp 10 b\u1ea3n ghi k\u1ebf ti\u1ebfp<\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"h-count-documents-d\u1ebfm-s\u1ed1-b\u1ea3n-ghi-th\u1ecfa-di\u1ec1u-ki\u1ec7n\"><strong>Count Documents (\u0110\u1ebfm s\u1ed1 b\u1ea3n ghi th\u1ecfa \u0111i\u1ec1u ki\u1ec7n)<\/strong><\/h5>\n\n\n\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 \u0111\u1ebfm s\u1ed1 l\u01b0\u1ee3ng user l\u00e0 n\u1eef, ta d\u00f9ng c\u00fa ph\u00e1p:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.countDocuments({ gender: \"Female\" })<\/code><\/pre>\n\n\n\n<p>K\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1 m\u1ed9t con s\u1ed1, v\u00ed d\u1ee5 nh\u01b0 <code>5<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-distinct-l\u1ea5y-gia-tr\u1ecb-duy-nh\u1ea5t-c\u1ee7a-m\u1ed9t-field\"><strong>Distinct (L\u1ea5y gi\u00e1 tr\u1ecb duy nh\u1ea5t c\u1ee7a m\u1ed9t field)<\/strong><\/h4>\n\n\n\n<p>Thay v\u00ec l\u1ea5y d\u1eef li\u1ec7u, ch\u1ec9 mu\u1ed1n bi\u1ebft c\u00f3 bao nhi\u00eau b\u1ea3n ghi th\u1ecfa \u0111i\u1ec1u ki\u1ec7n. V\u00ed d\u1ee5:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.distinct(\"gender\")<\/code><\/pre>\n\n\n\n<p>K\u1ebft qu\u1ea3 s\u1ebd tr\u1ea3 v\u1ec1 <code>[\"Male\", \"Female\"]<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-t\u1ed5ng-h\u1ee3p-cac-l\u1ec7nh-truy-v\u1ea5n-trong-mongodb\"><span class=\"ez-toc-section\" id=\"Tong_hop_cac_lenh_truy_van_trong_MongoDB\"><\/span><strong>T\u1ed5ng h\u1ee3p c\u00e1c l\u1ec7nh truy v\u1ea5n trong MongoDB<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>L\u1ec7nh<\/strong><\/td><td><strong>Ch\u1ee9c n\u0103ng<\/strong><\/td><td><strong>C\u00e1ch s\u1eed d\u1ee5ng<\/strong><\/td><td><strong>V\u00ed d\u1ee5 \u1ee9ng d\u1ee5ng<\/strong><\/td><\/tr><tr><td><code>db.test.find()<\/code><\/td><td>L\u1ea5y to\u00e0n b\u1ed9 document trong collection<\/td><td>Kh\u00f4ng c\u1ea7n tham s\u1ed1, tr\u1ea3 v\u1ec1 t\u1ea5t c\u1ea3 d\u1eef li\u1ec7u<\/td><td>Xem nhanh to\u00e0n b\u1ed9 d\u1eef li\u1ec7u trong collection \u0111\u1ec3 ki\u1ec3m tra nh\u1eadp li\u1ec7u<\/td><\/tr><tr><td><code>db.test.findOne({field: value})<\/code><\/td><td>L\u1ea5y 1 document \u0111\u1ea7u ti\u00ean th\u1ecfa \u0111i\u1ec1u ki\u1ec7n<\/td><td>Truy\u1ec1n v\u00e0o object \u0111i\u1ec1u ki\u1ec7n<\/td><td>T\u00ecm th\u00f4ng tin m\u1ed9t ng\u01b0\u1eddi d\u00f9ng c\u1ee5 th\u1ec3 theo t\u00ean<\/td><\/tr><tr><td><code>db.test.find({field1: value1, field2: value2})<\/code><\/td><td>Truy v\u1ea5n nhi\u1ec1u tr\u01b0\u1eddng c\u00f9ng l\u00fac<\/td><td>Ghi c\u00e1c c\u1eb7p key-value trong object<\/td><td>T\u00ecm user v\u1eeba c\u00f3 <code>name = Enayet v\u00e0 age = 36<\/code><\/td><\/tr><tr><td><code>db.test.find({field: {$exists: true}})<\/code><\/td><td>Ki\u1ec3m tra s\u1ef1 t\u1ed3n t\u1ea1i c\u1ee7a tr\u01b0\u1eddng<\/td><td>D\u00f9ng <code>$exists: true\/false<\/code><\/td><td>L\u1ea5y t\u1ea5t c\u1ea3 t\u00e0i li\u1ec7u c\u00f3 ch\u1ee9a tr\u01b0\u1eddng <code>age<\/code> (ho\u1eb7c kh\u00f4ng c\u00f3 tr\u01b0\u1eddng n\u00e0y)<\/td><\/tr><tr><td><code>db.test.find({field: {$eq: value}})<\/code><\/td><td>So s\u00e1nh b\u1eb1ng<\/td><td>D\u00f9ng <code>$eq<\/code> trong object \u0111i\u1ec1u ki\u1ec7n<\/td><td>T\u00ecm user c\u00f3 <code>age = 36<\/code><\/td><\/tr><tr><td><code>db.test.find({field: {$ne: value}})<\/code><\/td><td>So s\u00e1nh kh\u00e1c<\/td><td>D\u00f9ng <code>$ne<\/code> trong object \u0111i\u1ec1u ki\u1ec7n<\/td><td>T\u00ecm t\u1ea5t c\u1ea3 user kh\u00f4ng ph\u1ea3i <code>Male<\/code><\/td><\/tr><tr><td><code>db.test.find({field: {$gt: value}})<\/code><\/td><td>L\u1edbn h\u01a1n<\/td><td>D\u00f9ng <code>$gt<\/code> trong object \u0111i\u1ec1u ki\u1ec7n<\/td><td>T\u00ecm ng\u01b0\u1eddi l\u1edbn tu\u1ed5i h\u01a1n 20<\/td><\/tr><tr><td><code>db.test.find({field: {$gte: value}})<\/code><\/td><td>L\u1edbn h\u01a1n ho\u1eb7c b\u1eb1ng<\/td><td>D\u00f9ng <code>$gte<\/code> trong object \u0111i\u1ec1u ki\u1ec7n<\/td><td>L\u1ea5y ng\u01b0\u1eddi 20 tu\u1ed5i tr\u1edf l\u00ean<\/td><\/tr><tr><td><code>db.test.find({field: {$lt: value}})<\/code><\/td><td>Nh\u1ecf h\u01a1n<\/td><td>D\u00f9ng <code>$lt<\/code> trong object \u0111i\u1ec1u ki\u1ec7n<\/td><td>L\u1ea5y tr\u1ebb em d\u01b0\u1edbi 5 tu\u1ed5i<\/td><\/tr><tr><td><code>db.test.find({field: {$lte: value}})<\/code><\/td><td>Nh\u1ecf h\u01a1n ho\u1eb7c b\u1eb1ng<\/td><td>D\u00f9ng <code>$lte<\/code> trong object \u0111i\u1ec1u ki\u1ec7n<\/td><td>L\u1ea5y tr\u1ebb &lt;= 3 tu\u1ed5i<\/td><\/tr><tr><td><code>db.test.find({field: {$in: [v1, v2]}})<\/code><\/td><td>Thu\u1ed9c t\u1eadp gi\u00e1 tr\u1ecb<\/td><td>D\u00f9ng <code>$in<\/code> v\u1edbi array gi\u00e1 tr\u1ecb<\/td><td>T\u00ecm user t\u00ean l\u00e0 Enayet ho\u1eb7c Mariyam<\/td><\/tr><tr><td><code>db.test.find({field: {$nin: [v1, v2]}})<\/code><\/td><td>Kh\u00f4ng thu\u1ed9c t\u1eadp gi\u00e1 tr\u1ecb<\/td><td>D\u00f9ng <code>$nin<\/code> v\u1edbi array gi\u00e1 tr\u1ecb<\/td><td>L\u1ea5y ng\u01b0\u1eddi c\u00f3 tu\u1ed5i kh\u00e1c 1 v\u00e0 3<\/td><\/tr><tr><td><code>db.test.find({ $and: [cond1, cond2] })<\/code><\/td><td>K\u1ebft h\u1ee3p nhi\u1ec1u \u0111i\u1ec1u ki\u1ec7n (AND t\u01b0\u1eddng minh)<\/td><td>D\u00f9ng <code>$and<\/code> v\u1edbi m\u1ea3ng c\u00e1c object<\/td><td>T\u00ecm user <code>age &gt; 20<\/code> v\u00e0 <code>gender = Male<\/code><\/td><\/tr><tr><td><code>db.test.find({ $or: [cond1, cond2] })<\/code><\/td><td>Th\u1ecfa \u00edt nh\u1ea5t 1 \u0111i\u1ec1u ki\u1ec7n (OR)<\/td><td>D\u00f9ng <code>$or <\/code>v\u1edbi m\u1ea3ng c\u00e1c object<\/td><td>T\u00ecm ng\u01b0\u1eddi d\u01b0\u1edbi 5 tu\u1ed5i ho\u1eb7c gi\u1edbi t\u00ednh Female<\/td><\/tr><tr><td><code>db.test.find({arrayField: {$all: [v1, v2]}})<\/code><\/td><td>Array ch\u1ee9a t\u1ea5t c\u1ea3 gi\u00e1 tr\u1ecb<\/td><td>D\u00f9ng <code>$all<\/code> v\u1edbi array<\/td><td>T\u00ecm ng\u01b0\u1eddi c\u00f3 c\u1ea3 s\u1edf th\u00edch Travelling &amp; Cooking<\/td><\/tr><tr><td><code>db.test.find({arrayField: {$elemMatch: {...}}})<\/code><\/td><td>Array object th\u1ecfa t\u1ea5t c\u1ea3 \u0111i\u1ec1u ki\u1ec7n<\/td><td>D\u00f9ng <code>$elemMatch<\/code><\/td><td>T\u00ecm ng\u01b0\u1eddi c\u00f3 skill JavaScript \u1edf m\u1ee9c Intermediate<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-lam-th\u1ebf-nao-d\u1ec3-t\u1ed1i-\u01b0u-truy-v\u1ea5n-trong-mongodb\"><span class=\"ez-toc-section\" id=\"Lam_the_nao_de_toi_uu_truy_van_trong_MongoDB\"><\/span><strong>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 t\u1ed1i \u01b0u truy v\u1ea5n trong MongoDB?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-s\u1eed-d\u1ee5ng-index-ch\u1ec9-m\u1ee5c\"><strong>1. S\u1eed d\u1ee5ng Index (Ch\u1ec9 m\u1ee5c)<\/strong><\/h3>\n\n\n\n<p>Index gi\u00fap MongoDB t\u00ecm d\u1eef li\u1ec7u nhanh h\u01a1n, gi\u1ed1ng nh\u01b0 m\u1ee5c l\u1ee5c trong s\u00e1ch. Thay v\u00ec ph\u1ea3i l\u1eadt t\u1eebng trang, MongoDB ch\u1ec9 c\u1ea7n tra m\u1ee5c l\u1ee5c \u0111\u1ec3 \u0111\u1ebfn \u0111\u00fang v\u1ecb tr\u00ed. Index \u0111\u1eb7c bi\u1ec7t quan tr\u1ecdng khi collection c\u00f3 h\u00e0ng tri\u1ec7u documents.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 t\u00ecm user theo email nhanh h\u01a1n, ta t\u1ea1o index cho field <code>email<\/code> nh\u01b0 sau:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ T\u1ea1o index tr\u00ean field \"email\"\ndb.users.createIndex({ email: 1 })\n\n\/\/ Truy v\u1ea5n nhanh h\u01a1n khi c\u00f3 index<\/code><\/pre>\n\n\n\n<p>K\u1ebft qu\u1ea3: V\u1edbi index tr\u00ean, MongoDB s\u1ebd truy c\u1eadp th\u1eb3ng \u0111\u1ebfn document c\u00f3 <code>email = alice@example.com<\/code> thay v\u00ec duy\u1ec7t to\u00e0n b\u1ed9 d\u1eef li\u1ec7u.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C\u00e1c lo\u1ea1i index ph\u1ed5 bi\u1ebfn:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Single Field Index\ndb.users.createIndex({ age: 1 })\n\n\/\/ Compound Index (index nhi\u1ec1u tr\u01b0\u1eddng) - th\u1ee9 t\u1ef1 quan tr\u1ecdng!\ndb.users.createIndex({ gender: 1, age: -1 })\n\n\/\/ Unique Index (\u0111\u1ea3m b\u1ea3o gi\u00e1 tr\u1ecb duy nh\u1ea5t)\ndb.users.createIndex({ email: 1 }, { unique: true })\n\n\/\/ Text Index (cho full-text search)\ndb.users.createIndex({ name: \"text\", interests: \"text\" })\n\n\/\/ Multikey Index (t\u1ef1 \u0111\u1ed9ng cho array fields)\ndb.users.createIndex({ interests: 1 })<\/code><\/pre>\n\n\n\n<p><strong>L\u01b0u \u00fd<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Index t\u0103ng t\u1ed1c \u0111\u1ed9 \u0111\u1ecdc nh\u01b0ng l\u00e0m ch\u1eadm c\u00e1c thao t\u00e1c ghi nh\u01b0 <code>insert<\/code>, <code>update<\/code>, <code>delete<\/code> v\u00ec d\u1eef li\u1ec7u ph\u1ea3i \u0111\u01b0\u1ee3c c\u1eadp nh\u1eadt th\u00eam trong c\u1ea5u tr\u00fac ch\u1ec9 m\u1ee5c.<\/li>\n\n\n\n<li>M\u1ed7i index chi\u1ebfm th\u00eam dung l\u01b0\u1ee3ng l\u01b0u tr\u1eef, do \u0111\u00f3 vi\u1ec7c t\u1ea1o qu\u00e1 nhi\u1ec1u index kh\u00f4ng c\u1ea7n thi\u1ebft c\u00f3 th\u1ec3 l\u00e0m gi\u1ea3m hi\u1ec7u su\u1ea5t t\u1ed5ng th\u1ec3.<\/li>\n\n\n\n<li>C\u00e1ch ki\u1ec3m tra index hi\u1ec7n c\u00f3:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.getIndexes()<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C\u00e1ch x\u00f3a index kh\u00f4ng c\u00f2n s\u1eed d\u1ee5ng:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.dropIndex(\"index_name\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-projection-ch\u1ec9-l\u1ea5y-d\u1eef-li\u1ec7u-c\u1ea7n-thi\u1ebft\"><strong>2. Projection &#8211; Ch\u1ec9 l\u1ea5y d\u1eef li\u1ec7u c\u1ea7n thi\u1ebft<\/strong><\/h3>\n\n\n\n<p>Khi truy v\u1ea5n, MongoDB m\u1eb7c \u0111\u1ecbnh tr\u1ea3 v\u1ec1 to\u00e0n b\u1ed9 c\u00e1c field trong document. Nh\u01b0ng n\u1ebfu ch\u1ec9 c\u1ea7n v\u00e0i tr\u01b0\u1eddng th\u00f4ng tin, vi\u1ec7c l\u1ea5y t\u1ea5t c\u1ea3 s\u1ebd l\u00e3ng ph\u00ed t\u00e0i nguy\u00ean, l\u00e0m truy v\u1ea5n ch\u1eadm h\u01a1n v\u00e0 t\u1ed1n b\u0103ng th\u00f4ng. \u0110i\u1ec1u n\u00e0y c\u00e0ng \u0111\u00e1ng ch\u00fa \u00fd khi document ch\u1ee9a tr\u01b0\u1eddng l\u1edbn nh\u01b0 danh s\u00e1ch (array) ho\u1eb7c t\u00e0i li\u1ec7u l\u1ed3ng nhau (embedded document).<\/p>\n\n\n\n<p>V\u00ec v\u1eady ch\u1ec9 l\u1ea5y nh\u1eefng d\u1eef li\u1ec7u c\u1ea7n thi\u1ebft \u0111\u1ec3 tr\u00e1nh t\u1ed1n t\u00e0i nguy\u00ean. V\u00ed d\u1ee5 \u0111\u1ec3 ch\u1ec9 l\u1ea5y name v\u00e0 email (b\u1ecf qua _id), ta d\u00f9ng c\u00fa ph\u00e1p:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({}, { name: 1, email: 1, _id: 0 })<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-projection-nang-cao\"><strong>Projection n\u00e2ng cao<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Lo\u1ea1i tr\u1eeb m\u1ed9t tr\u01b0\u1eddng c\u1ee5 th\u1ec3 (exclusion)\ndb.users.find({}, { interests: 0 })\n\n\/\/ L\u1ea5y ph\u1ea7n t\u1eed \u0111\u1ea7u ti\u00ean c\u1ee7a array\ndb.users.find({}, { interests: { $slice: 1 } })\n\n\/\/ L\u1ea5y 2 ph\u1ea7n t\u1eed cu\u1ed1i c\u1ee7a array\ndb.users.find({}, { interests: { $slice: -2 } })<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-s\u1eed-d\u1ee5ng-explain-d\u1ec3-phan-tich-truy-v\u1ea5n\"><strong>3. S\u1eed d\u1ee5ng Explain \u0111\u1ec3 ph\u00e2n t\u00edch truy v\u1ea5n<\/strong><\/h3>\n\n\n\n<p>MongoDB h\u1ed7 tr\u1ee3 l\u1ec7nh <code>explain()<\/code> \u0111\u1ec3 ph\u00e2n t\u00edch truy v\u1ea5n \u0111ang ch\u1ea1y nhanh hay ch\u1eadm, c\u00f3 d\u00f9ng index hay kh\u00f4ng. V\u00ed d\u1ee5 \u0111\u1ec3 ki\u1ec3m tra truy v\u1ea5n t\u00ecm user c\u00f3 <code>age &gt; 25<\/code>, ta d\u00f9ng c\u00fa ph\u00e1p:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({ age: { $gt: 25 } }).explain(\"executionStats\")<\/code><\/pre>\n\n\n\n<p>K\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1 cho bi\u1ebft MongoDB \u0111\u00e3 qu\u00e9t bao nhi\u00eau document, c\u00f3 d\u00f9ng index hay kh\u00f4ng r\u1ed3i t\u1eeb \u0111\u00f3 tinh ch\u1ec9nh th\u00eam.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-4-gi\u1edbi-h\u1ea1n-limit-va-phan-trang\"><strong>4. Gi\u1edbi h\u1ea1n (Limit) v\u00e0 ph\u00e2n trang<\/strong><\/h3>\n\n\n\n<p>N\u1ebfu b\u1ea1n ch\u1ec9 c\u1ea7n m\u1ed9t ph\u1ea7n d\u1eef li\u1ec7u (v\u00ed d\u1ee5: top 10 b\u1ea3n ghi m\u1edbi nh\u1ea5t), h\u00e3y d\u00f9ng <code>limit()<\/code> ho\u1eb7c k\u1ebft h\u1ee3p v\u1edbi <code>skip()<\/code> \u0111\u1ec3 ph\u00e2n trang. Tuy nhi\u00ean<code> skip()<\/code> v\u1edbi s\u1ed1 l\u1edbn r\u1ea5t ch\u1eadm, n\u00ean d\u00f9ng ph\u01b0\u01a1ng ph\u00e1p ph\u00e2n trang d\u1ef1a tr\u00ean range queries. V\u00ed d\u1ee5:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ L\u1ea5y 10 user \u0111\u1ea7u ti\u00ean, s\u1eafp x\u1ebfp theo tu\u1ed5i gi\u1ea3m d\u1ea7n\ndb.users.find().sort({ age: -1 }).limit(10)\n\n\/\/ L\u1ea5y trang th\u1ee9 2 (b\u1ecf qua 10 b\u1ea3n ghi \u0111\u1ea7u)\ndb.users.find().sort({ age: -1 }).skip(10).limit(10)<\/code><\/pre>\n\n\n\n<p>Ph\u00e2n trang t\u1ed1i \u01b0u h\u01a1n (Range-based pagination):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Trang \u0111\u1ea7u ti\u00ean\ndb.users.find().sort({ _id: -1 }).limit(10)\n\n\/\/ Trang ti\u1ebfp theo (d\u00f9ng _id c\u1ee7a b\u1ea3n ghi cu\u1ed1i trang tr\u01b0\u1edbc)\ndb.users.find({ _id: { $lt: lastSeenId } }).sort({ _id: -1 }).limit(10)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-5-s\u1eed-d\u1ee5ng-covered-query\"><strong>5. S\u1eed d\u1ee5ng Covered Query<\/strong><\/h3>\n\n\n\n<p>Covered Query l\u00e0 lo\u1ea1i truy v\u1ea5n m\u00e0 to\u00e0n b\u1ed9 c\u00e1c tr\u01b0\u1eddng \u0111\u01b0\u1ee3c truy v\u1ea5n v\u00e0 tr\u1ea3 v\u1ec1 \u0111\u1ec1u n\u1eb1m trong index. Nh\u1edd \u0111\u00f3, MongoDB kh\u00f4ng c\u1ea7n \u0111\u1ecdc document g\u1ed1c trong collection, gi\u00fap t\u0103ng t\u1ed1c \u0111\u1ed9 truy v\u1ea5n.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ T\u1ea1o compound index\ndb.users.createIndex({ gender: 1, age: 1, name: 1 })\n\n\/\/ Covered query - c\u1ef1c nhanh!\ndb.users.find(\n  { gender: \"Male\", age: { $gt: 25 } },\n  { gender: 1, age: 1, name: 1, _id: 0 }\n)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Trong \u0111\u00f3: C\u1ea3 \u0111i\u1ec1u ki\u1ec7n l\u1ecdc (<code>gender<\/code>, <code>age<\/code>) v\u00e0 c\u00e1c tr\u01b0\u1eddng tr\u1ea3 v\u1ec1 (<code>gender<\/code>, <code>age<\/code>, <code>name<\/code>) \u0111\u1ec1u n\u1eb1m trong index <code>{ gender: 1, age: 1, name: 1 }<\/code>. V\u00ec v\u1eady, MongoDB c\u00f3 th\u1ec3 tr\u1ea3 k\u1ebft qu\u1ea3 tr\u1ef1c ti\u1ebfp t\u1eeb index m\u00e0 kh\u00f4ng c\u1ea7n truy c\u1eadp document g\u1ed1c.<\/li>\n<\/ul>\n\n\n\n<p>Khi ki\u1ec3m tra b\u1eb1ng l\u1ec7nh <code>explain()<\/code>, ta s\u1ebd th\u1ea5y:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"totalDocsExamined\" : 0<\/code><\/pre>\n\n\n\n<p>Ngh\u0129a l\u00e0 kh\u00f4ng c\u00f3 document n\u00e0o \u0111\u01b0\u1ee3c \u0111\u1ecdc t\u1eeb \u0111\u0129a, t\u1ea5t c\u1ea3 d\u1eef li\u1ec7u \u0111\u1ec1u \u0111\u01b0\u1ee3c l\u1ea5y t\u1eeb index<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cau-h\u1ecfi-th\u01b0\u1eddng-g\u1eb7p-v\u1ec1-cac-truy-v\u1ea5n-trong-mongodb\"><span class=\"ez-toc-section\" id=\"Cau_hoi_thuong_gap_ve_cac_truy_van_trong_MongoDB\"><\/span><strong>C\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 c\u00e1c truy v\u1ea5n trong MongoDB<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-truy-v\u1ea5n-trong-mongodb-co-khac-gi-so-v\u1edbi-sql-khong\"><strong>Truy v\u1ea5n trong MongoDB c\u00f3 kh\u00e1c g\u00ec so v\u1edbi SQL kh\u00f4ng?<\/strong><\/h3>\n\n\n\n<p>C\u00f3. MongoDB d\u00f9ng c\u00fa ph\u00e1p d\u1ef1a tr\u00ean JSON thay v\u00ec SQL.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5, thay v\u00ec <code>SELECT * FROM users WHERE age > 25,<\/code> ta s\u1ebd d\u00f9ng:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({ age: { $gt: 25 } })<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-mongodb-co-h\u1ed7-tr\u1ee3-truy-v\u1ea5n-ph\u1ee9c-t\u1ea1p-khong\"><strong>MongoDB c\u00f3 h\u1ed7 tr\u1ee3 truy v\u1ea5n ph\u1ee9c t\u1ea1p kh\u00f4ng?<\/strong><\/h3>\n\n\n\n<p>C\u00f3. MongoDB cung c\u1ea5p nhi\u1ec1u to\u00e1n t\u1eed (operators) nh\u01b0 <code>$and<\/code>, <code>$or<\/code>, <code>$in<\/code>, <code>$regex<\/code> v\u00e0 h\u1ec7 th\u1ed1ng Aggregation Pipeline \u0111\u1ec3 x\u1eed l\u00fd d\u1eef li\u1ec7u ph\u1ee9c t\u1ea1p nh\u01b0 l\u1ecdc, nh\u00f3m, t\u00ednh to\u00e1n.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-truy-v\u1ea5n-trong-mongodb-co-phan-bi\u1ec7t-hoa-th\u01b0\u1eddng-khong\"><strong>Truy v\u1ea5n trong MongoDB c\u00f3 ph\u00e2n bi\u1ec7t hoa th\u01b0\u1eddng kh\u00f4ng?<\/strong><\/h3>\n\n\n\n<p>C\u00f3. M\u1eb7c \u0111\u1ecbnh MongoDB ph\u00e2n bi\u1ec7t ch\u1eef hoa v\u00e0 th\u01b0\u1eddng. Tuy nhi\u00ean, ta c\u00f3 th\u1ec3 d\u00f9ng <code>$regex<\/code> k\u00e8m <code>flag i<\/code> \u0111\u1ec3 b\u1ecf qua ph\u00e2n bi\u1ec7t.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({ name: { $regex: \/^alice$\/i } })<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-t\u1ed5ng-k\u1ebft\"><span class=\"ez-toc-section\" id=\"Tong_ket\"><\/span><strong>T\u1ed5ng k\u1ebft<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Truy v\u1ea5n l\u00e0 b\u01b0\u1edbc n\u1ec1n t\u1ea3ng \u0111\u1ec3 b\u1ea1n khai th\u00e1c t\u1ed1i \u0111a s\u1ee9c m\u1ea1nh c\u1ee7a MongoDB. Ch\u1ec9 c\u1ea7n n\u1eafm v\u1eefng c\u00fa ph\u00e1p c\u01a1 b\u1ea3n, hi\u1ec3u r\u00f5 c\u00e1c to\u00e1n t\u1eed v\u00e0 bi\u1ebft c\u00e1ch t\u1ed1i \u01b0u nh\u01b0 ITviec \u0111\u00e3 chia s\u1ebb \u1edf tr\u00ean, b\u1ea1n \u0111\u00e3 c\u00f3 th\u1ec3 t\u1ef1 tin x\u00e2y d\u1ef1ng nh\u1eefng h\u1ec7 th\u1ed1ng x\u1eed l\u00fd d\u1eef li\u1ec7u linh ho\u1ea1t, nhanh ch\u00f3ng v\u00e0 hi\u1ec7u qu\u1ea3, t\u1eeb \u0111\u00f3 d\u1ec5 d\u00e0ng ti\u1ebfn t\u1edbi c\u00e1c k\u1ef9 n\u0103ng n\u00e2ng cao nh\u01b0 Aggregation Pipeline, Indexing hay Data Modeling.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Khi l\u00e0m vi\u1ec7c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u, kh\u1ea3 n\u0103ng truy v\u1ea5n d\u1eef li\u1ec7u l\u00e0 k\u1ef9 n\u0103ng c\u1ed1t l\u00f5i m\u00e0 b\u1ea5t k\u1ef3 l\u1eadp tr\u00ecnh vi\u00ean n\u00e0o c\u0169ng c\u1ea7n n\u1eafm v\u1eefng. V\u1edbi MongoDB &#8211; c\u01a1 s\u1edf d\u1eef li\u1ec7u NoSQL ph\u1ed5 bi\u1ebfn nh\u1ea5t hi\u1ec7n nay &#8211; c\u00fa ph\u00e1p truy v\u1ea5n kh\u00e1 linh ho\u1ea1t, m\u1ea1nh m\u1ebd v\u00e0 g\u1ea7n [&hellip;]<\/p>\n","protected":false},"author":209,"featured_media":93093,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","footnotes":""},"categories":[109,7226],"tags":[],"class_list":["post-92437","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chuyen-mon-it","category-database"],"blocksy_meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>MongoDB Cheat sheet: T\u1ed5ng h\u1ee3p t\u1ea5t c\u1ea3 truy v\u1ea5n trong MongoDB - ITviec Blog<\/title>\n<meta name=\"description\" content=\"T\u1ed5ng h\u1ee3p truy v\u1ea5n trong MongoDB k\u00e8m v\u00ed d\u1ee5 c\u1ee5 th\u1ec3, c\u00e1c tr\u01b0\u1eddng h\u1ee3p s\u1eed d\u1ee5ng v\u00e0 c\u00e1ch t\u1ed1i \u01b0u truy v\u1ea5n MongoDB sao cho hi\u1ec7u qu\u1ea3.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MongoDB Cheat sheet: T\u1ed5ng h\u1ee3p t\u1ea5t c\u1ea3 truy v\u1ea5n trong MongoDB\" \/>\n<meta property=\"og:description\" content=\"Khi l\u00e0m vi\u1ec7c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u, kh\u1ea3 n\u0103ng truy v\u1ea5n d\u1eef li\u1ec7u l\u00e0 k\u1ef9 n\u0103ng c\u1ed1t l\u00f5i m\u00e0 b\u1ea5t k\u1ef3 l\u1eadp tr\u00ecnh vi\u00ean n\u00e0o c\u0169ng c\u1ea7n n\u1eafm v\u1eefng. V\u1edbi MongoDB - c\u01a1 s\u1edf d\u1eef li\u1ec7u\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/\" \/>\n<meta property=\"og:site_name\" content=\"ITviec Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ITviec\" \/>\n<meta property=\"article:published_time\" content=\"2025-11-24T04:17:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-11-24T04:18:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/10\/truy-van-trong-mongodb-scaled.png\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"421\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"M\u1ef9 Duy\u00ean\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ITviec\" \/>\n<meta name=\"twitter:site\" content=\"@ITviec\" \/>\n<meta name=\"twitter:label1\" content=\"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi\" \/>\n\t<meta name=\"twitter:data1\" content=\"M\u1ef9 Duy\u00ean\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"MongoDB Cheat sheet: T\u1ed5ng h\u1ee3p t\u1ea5t c\u1ea3 truy v\u1ea5n trong MongoDB - ITviec Blog","description":"T\u1ed5ng h\u1ee3p truy v\u1ea5n trong MongoDB k\u00e8m v\u00ed d\u1ee5 c\u1ee5 th\u1ec3, c\u00e1c tr\u01b0\u1eddng h\u1ee3p s\u1eed d\u1ee5ng v\u00e0 c\u00e1ch t\u1ed1i \u01b0u truy v\u1ea5n MongoDB sao cho hi\u1ec7u qu\u1ea3.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/","og_locale":"vi_VN","og_type":"article","og_title":"MongoDB Cheat sheet: T\u1ed5ng h\u1ee3p t\u1ea5t c\u1ea3 truy v\u1ea5n trong MongoDB","og_description":"Khi l\u00e0m vi\u1ec7c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u, kh\u1ea3 n\u0103ng truy v\u1ea5n d\u1eef li\u1ec7u l\u00e0 k\u1ef9 n\u0103ng c\u1ed1t l\u00f5i m\u00e0 b\u1ea5t k\u1ef3 l\u1eadp tr\u00ecnh vi\u00ean n\u00e0o c\u0169ng c\u1ea7n n\u1eafm v\u1eefng. V\u1edbi MongoDB - c\u01a1 s\u1edf d\u1eef li\u1ec7u","og_url":"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2025-11-24T04:17:57+00:00","article_modified_time":"2025-11-24T04:18:01+00:00","og_image":[{"width":800,"height":421,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/10\/truy-van-trong-mongodb-scaled.png","type":"image\/png"}],"author":"M\u1ef9 Duy\u00ean","twitter_card":"summary_large_image","twitter_creator":"@ITviec","twitter_site":"@ITviec","twitter_misc":{"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi":"M\u1ef9 Duy\u00ean","\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc":"13 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/"},"author":{"name":"M\u1ef9 Duy\u00ean","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/73733c0725c7e39e696a896bd1abe2d7"},"headline":"MongoDB Cheat sheet: T\u1ed5ng h\u1ee3p t\u1ea5t c\u1ea3 truy v\u1ea5n trong MongoDB","datePublished":"2025-11-24T04:17:57+00:00","dateModified":"2025-11-24T04:18:01+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/"},"wordCount":3151,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/10\/truy-van-trong-mongodb-scaled.png","articleSection":["Chuy\u00ean m\u00f4n IT","Database"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/","url":"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/","name":"MongoDB Cheat sheet: T\u1ed5ng h\u1ee3p t\u1ea5t c\u1ea3 truy v\u1ea5n trong MongoDB - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/10\/truy-van-trong-mongodb-scaled.png","datePublished":"2025-11-24T04:17:57+00:00","dateModified":"2025-11-24T04:18:01+00:00","description":"T\u1ed5ng h\u1ee3p truy v\u1ea5n trong MongoDB k\u00e8m v\u00ed d\u1ee5 c\u1ee5 th\u1ec3, c\u00e1c tr\u01b0\u1eddng h\u1ee3p s\u1eed d\u1ee5ng v\u00e0 c\u00e1ch t\u1ed1i \u01b0u truy v\u1ea5n MongoDB sao cho hi\u1ec7u qu\u1ea3.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/10\/truy-van-trong-mongodb-scaled.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/10\/truy-van-trong-mongodb-scaled.png","width":800,"height":421,"caption":"truy v\u1ea5n trong mongodb - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/truy-van-trong-mongodb\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Chuy\u00ean m\u00f4n IT","item":"https:\/\/itviec.com\/blog\/chuyen-mon-it\/"},{"@type":"ListItem","position":2,"name":"MongoDB Cheat sheet: T\u1ed5ng h\u1ee3p t\u1ea5t c\u1ea3 truy v\u1ea5n trong MongoDB"}]},{"@type":"WebSite","@id":"https:\/\/itviec.com\/blog\/#website","url":"https:\/\/itviec.com\/blog\/","name":"ITviec Blog","description":"IT Jobs &amp; People in Vietnam","publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/itviec.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"vi"},{"@type":"Organization","@id":"https:\/\/itviec.com\/blog\/#organization","name":"ITviec","url":"https:\/\/itviec.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","width":1800,"height":1800,"caption":"ITviec"},"image":{"@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ITviec","https:\/\/x.com\/ITviec","https:\/\/www.linkedin.com\/company\/itviec","https:\/\/www.youtube.com\/channel\/UCYthAQ3bcGr57M_ag5gHDvQ"]},{"@type":"Person","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/73733c0725c7e39e696a896bd1abe2d7","name":"M\u1ef9 Duy\u00ean","image":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","caption":"M\u1ef9 Duy\u00ean"},"url":"https:\/\/itviec.com\/blog\/author\/my-duyen\/"}]}},"_links":{"self":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/92437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/users\/209"}],"replies":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/comments?post=92437"}],"version-history":[{"count":2,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/92437\/revisions"}],"predecessor-version":[{"id":93094,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/92437\/revisions\/93094"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/93093"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=92437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=92437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=92437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}