{"id":93878,"date":"2025-12-30T22:36:29","date_gmt":"2025-12-30T15:36:29","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=93878"},"modified":"2025-12-30T22:36:56","modified_gmt":"2025-12-30T15:36:56","slug":"cau-hoi-phong-van-mongodb","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-mongodb\/","title":{"rendered":"Top 20+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB th\u01b0\u1eddng g\u1eb7p"},"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\/cau-hoi-phong-van-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\/cau-hoi-phong-van-mongodb\/#Cau_hoi_phong_van_MongoDB_cap_do_co_ban\" >C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB c\u1ea5p \u0111\u1ed9 c\u01a1 b\u1ea3n<\/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\/cau-hoi-phong-van-mongodb\/#Cau_hoi_phong_van_MongoDB_cap_do_nang_cao\" >C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB c\u1ea5p \u0111\u1ed9 n\u00e2ng cao<\/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\/cau-hoi-phong-van-mongodb\/#MongoDB_Query-Based_Interview_Questions_%E2%80%93_Bai_tap_thuc_hanh\" >MongoDB Query-Based Interview Questions &#8211; B\u00e0i t\u1eadp th\u1ef1c h\u00e0nh<\/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\/cau-hoi-phong-van-mongodb\/#Tong_ket\" >T\u1ed5ng k\u1ebft<\/a><\/li><\/ul><\/nav><\/div>\n\n<p><strong><em>MongoDB \u0111\u00e3 tr\u1edf th\u00e0nh m\u1ed9t trong nh\u1eefng NoSQL Database \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng r\u1ed9ng r\u00e3i nh\u1ea5t hi\u1ec7n nay nh\u1edd kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng linh ho\u1ea1t, l\u01b0u tr\u1eef d\u1eef li\u1ec7u d\u1ea1ng t\u00e0i li\u1ec7u v\u00e0 hi\u1ec7u n\u0103ng cao. N\u1ebfu b\u1ea1n \u0111ang chu\u1ea9n b\u1ecb cho bu\u1ed5i ph\u1ecfng v\u1ea5n v\u1ecb tr\u00ed Backend Developer, Data Engineer, Database Administrator hay b\u1ea5t k\u1ef3 c\u00f4ng vi\u1ec7c n\u00e0o li\u00ean quan \u0111\u1ebfn d\u1eef li\u1ec7u, vi\u1ec7c n\u1eafm ch\u1eafc c\u00e1c c\u00e2u h\u1ecfi xoay quanh MongoDB l\u00e0 b\u01b0\u1edbc chu\u1ea9n b\u1ecb v\u00f4 c\u00f9ng quan tr\u1ecdng.\u00a0<\/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 c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB c\u01a1 b\u1ea3n<\/li>\n\n\n\n<li>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB n\u00e2ng cao<\/li>\n\n\n\n<li>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB d\u1ea1ng b\u00e0i t\u1eadp th\u1ef1c h\u00e0nh<\/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 h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u NoSQL ph\u1ed5 bi\u1ebfn, \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf \u0111\u1ec3 l\u01b0u tr\u1eef d\u1eef li\u1ec7u d\u1ea1ng t\u00e0i li\u1ec7u (document-oriented) d\u01b0\u1edbi \u0111\u1ecbnh d\u1ea1ng BSON (Binary JSON). Kh\u00f4ng gi\u1ed1ng c\u00e1c h\u1ec7 c\u01a1 s\u1edf d\u1eef li\u1ec7u quan h\u1ec7 (RDBMS) nh\u01b0 MySQL hay PostgreSQL, MongoDB kh\u00f4ng y\u00eau c\u1ea7u schema c\u1ed1 \u0111\u1ecbnh &#8211; \u0111i\u1ec1u n\u00e0y gi\u00fap l\u1eadp tr\u00ecnh vi\u00ean d\u1ec5 d\u00e0ng m\u1edf r\u1ed9ng, thay \u0111\u1ed5i c\u1ea5u tr\u00fac d\u1eef li\u1ec7u m\u00e0 kh\u00f4ng c\u1ea7n ch\u1ec9nh s\u1eeda to\u00e0n b\u1ed9 b\u1ea3ng.<\/p>\n\n\n\n<p>M\u1ed9t s\u1ed1 \u0111\u1eb7c \u0111i\u1ec3m n\u1ed5i b\u1eadt c\u1ee7a MongoDB g\u1ed3m:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Linh ho\u1ea1t v\u1ec1 c\u1ea5u tr\u00fac d\u1eef li\u1ec7u: M\u1ed7i document c\u00f3 th\u1ec3 ch\u1ee9a c\u00e1c tr\u01b0\u1eddng kh\u00e1c nhau, ph\u00f9 h\u1ee3p v\u1edbi \u1ee9ng d\u1ee5ng ph\u00e1t tri\u1ec3n nhanh ho\u1eb7c thay \u0111\u1ed5i th\u01b0\u1eddng xuy\u00ean.<\/li>\n\n\n\n<li>Hi\u1ec7u n\u0103ng cao: D\u1eef li\u1ec7u \u0111\u01b0\u1ee3c truy xu\u1ea5t nhanh nh\u1edd c\u01a1 ch\u1ebf indexing m\u1ea1nh m\u1ebd v\u00e0 l\u01b0u tr\u1eef d\u1ea1ng BSON.<\/li>\n\n\n\n<li>Kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng (Scalability): H\u1ed7 tr\u1ee3 horizontal scaling th\u00f4ng qua sharding \u0111\u1ec3 ph\u00e2n t\u00e1n d\u1eef li\u1ec7u tr\u00ean nhi\u1ec1u m\u00e1y ch\u1ee7.<\/li>\n\n\n\n<li>D\u1ec5 t\u00edch h\u1ee3p: MongoDB ho\u1ea1t \u0111\u1ed9ng t\u1ed1t v\u1edbi c\u00e1c ng\u00f4n ng\u1eef nh\u01b0 Node.js, Python, Java, .NET\u2026<\/li>\n\n\n\n<li>H\u1ed7 tr\u1ee3 m\u1ea1nh cho Big Data &amp; Analytics: Th\u00f4ng qua Aggregation Framework, Atlas v\u00e0 t\u00edch h\u1ee3p t\u1ed1t v\u1edbi c\u00e1c c\u00f4ng c\u1ee5 nh\u01b0 Spark.<\/li>\n<\/ul>\n\n\n\n<p>MongoDB th\u01b0\u1eddng \u0111\u01b0\u1ee3c d\u00f9ng trong:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C\u00e1c \u1ee9ng d\u1ee5ng web v\u00e0 mobile c\u00f3 d\u1eef li\u1ec7u linh ho\u1ea1t.<\/li>\n\n\n\n<li>H\u1ec7 th\u1ed1ng IoT, real-time analytics, AI\/ML pipelines.<\/li>\n\n\n\n<li>C\u00e1c s\u1ea3n ph\u1ea9m c\u1ea7n scalability cao v\u00e0 thay \u0111\u1ed5i c\u1ea5u tr\u00fac nhanh nh\u01b0 e-commerce, m\u1ea1ng x\u00e3 h\u1ed9i, content management systems (CMS), hay qu\u1ea3n l\u00fd n\u1ed9i dung.<\/li>\n\n\n\n<li>Product catalogs v\u1edbi c\u00e1c thu\u1ed9c t\u00ednh \u0111a d\u1ea1ng<\/li>\n\n\n\n<li>Session storage v\u00e0 caching<\/li>\n\n\n\n<li>Logging v\u00e0 event tracking<\/li>\n\n\n\n<li>L\u01b0u h\u1ed3 s\u01a1 ng\u01b0\u1eddi d\u00f9ng, s\u1edf th\u00edch, l\u1ecbch s\u1eed t\u01b0\u01a1ng t\u00e1c \u0111\u1ec3 c\u00e1 nh\u00e2n ho\u00e1 tr\u1ea3i nghi\u1ec7m<\/li>\n<\/ul>\n\n\n\n<p>Ngo\u00e0i ra, b\u1ea1n c\u00f3 th\u1ec3 t\u00ecm hi\u1ec3u k\u1ef9 h\u01a1n v\u1ec1 <strong><a href=\"https:\/\/itviec.com\/blog\/mongodb-la-gi\/\" target=\"_blank\" rel=\"noreferrer noopener\">MongoDB l\u00e0 g\u00ec<\/a><\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-mongodb-c\u1ea5p-d\u1ed9-c\u01a1-b\u1ea3n\"><span class=\"ez-toc-section\" id=\"Cau_hoi_phong_van_MongoDB_cap_do_co_ban\"><\/span><strong>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB c\u1ea5p \u0111\u1ed9 c\u01a1 b\u1ea3n<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-hay-trinh-bay-cac-thanh-ph\u1ea7n-c\u1ee7a-mongodb\"><strong>H\u00e3y tr\u00ecnh b\u00e0y c\u00e1c th\u00e0nh ph\u1ea7n c\u1ee7a MongoDB<\/strong><\/h3>\n\n\n\n<p>MongoDB g\u1ed3m c\u00e1c th\u00e0nh ph\u1ea7n c\u01a1 b\u1ea3n sau:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Document<\/strong>: l\u00e0 \u0111\u01a1n v\u1ecb d\u1eef li\u1ec7u c\u01a1 b\u1ea3n trong MongoDB, l\u01b0u \u1edf d\u1ea1ng BSON (Binary JSON). M\u1ed7i document l\u00e0 m\u1ed9t \u201cb\u1ea3n ghi\u201d t\u1ef1 m\u00f4 t\u1ea3, c\u00f3 th\u1ec3 l\u1ed3ng (embedded) c\u00e1c document\/array kh\u00e1c, c\u00f3 k\u00edch th\u01b0\u1edbc t\u1ed1i \u0111a 16MB.<\/li>\n<\/ul>\n\n\n\n<p><strong>Kh\u00f3a \u0111\u1eb7c bi\u1ec7t:<\/strong> <code>_id<\/code> (duy nh\u1ea5t trong collection \u0111\u01b0\u1ee3c t\u1ef1 \u0111\u1ed9ng index).<\/p>\n\n\n\n<p>V\u00ed d\u1ee5:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"_id\": \"usr_1001\",\n  \"name\": { \"first\": \"An\", \"last\": \"Nguyen\" },\n  \"email\": \"an@example.com\",\n  \"roles\": &#91;\"admin\", \"editor\"],\n  \"profile\": {\n    \"dob\": \"1999-10-21\",\n    \"phones\": &#91;{ \"type\": \"mobile\", \"number\": \"+84-912-xxx-xxx\" }]\n  },\n  \"active\": true,\n  \"createdAt\": ISODate(\"2025-12-07T05:00:00Z\")\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Field<\/strong>: M\u1ed7i tr\u01b0\u1eddng (key) trong document, \u0111i k\u00e8m gi\u00e1 tr\u1ecb (value). Document trong c\u00f9ng m\u1ed9t collection kh\u00f4ng b\u1eaft bu\u1ed9c c\u00f3 c\u00f9ng t\u1eadp field (schema linh ho\u1ea1t), t\u00ean field ph\u00e2n bi\u1ec7t hoa th\u01b0\u1eddng (case-sensitive).<\/li>\n<\/ul>\n\n\n\n<p><strong>Ki\u1ec3u d\u1eef li\u1ec7u ph\u1ed5 bi\u1ebfn<\/strong>: string, number, boolean, date, array, object, ObjectId, Decimal128, Binary, Timestamp, Null, Regular Expression, JavaScript Code \u2026<\/p>\n\n\n\n<p>V\u00ed d\u1ee5: name, email, roles (array), profile (object), active (boolean).<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Collection: <\/strong>T\u1eadp h\u1ee3p nhi\u1ec1u document c\u00f3 li\u00ean quan (t\u01b0\u01a1ng t\u1ef1 \u201cb\u1ea3ng\u201d trong SQL nh\u01b0ng kh\u00f4ng y\u00eau c\u1ea7u schema). M\u1ed9t database c\u00f3 nhi\u1ec1u collection; collection c\u00f3 th\u1ec3 t\u1ea1o index, validator, collation, \u2026<\/li>\n<\/ul>\n\n\n\n<p>V\u00ed d\u1ee5: users, orders, products.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Database: <\/strong>Ch\u1ee9a nhi\u1ec1u collection, m\u1ed7i MongoDB instance c\u00f3 th\u1ec3 c\u00f3 nhi\u1ec1u database.<\/li>\n<\/ul>\n\n\n\n<p>Database m\u1eb7c \u0111\u1ecbnh: admin (qu\u1ea3n tr\u1ecb), local (replica set), config (sharding metadata).<\/p>\n\n\n\n<p>V\u00ed d\u1ee5: ecommerce_db, user_management, analytics_db.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-mongodb-khac-gi-v\u1edbi-cac-rdbms-khac\"><strong>MongoDB kh\u00e1c g\u00ec v\u1edbi c\u00e1c RDBMS kh\u00e1c?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Ti\u00eau ch\u00ed<\/strong><\/td><td><strong>MongoDB (NoSQL)<\/strong><\/td><td><strong>RDBMS (MySQL, PostgreSQL, &#8230;)<\/strong><\/td><\/tr><tr><td>M\u00f4 h\u00ecnh d\u1eef li\u1ec7u<\/td><td>D\u1ea1ng document (BSON) linh ho\u1ea1t, kh\u00f4ng c\u1ea7n schema c\u1ed1 \u0111\u1ecbnh<\/td><td>D\u1ea1ng b\u1ea3ng (table) v\u1edbi schema c\u1ee9ng (ph\u1ea3i \u0111\u1ecbnh ngh\u0129a c\u1ed9t, ki\u1ec3u d\u1eef li\u1ec7u)<\/td><\/tr><tr><td>C\u1ea5u tr\u00fac d\u1eef li\u1ec7u<\/td><td>Document g\u1ed3m c\u00e1c field-value, c\u00f3 th\u1ec3 l\u1ed3ng nhau (nested, array, object)<\/td><td>B\u1ea3ng g\u1ed3m h\u00e0ng v\u00e0 c\u1ed9t; c\u00e1c b\u1ea3ng li\u00ean k\u1ebft b\u1eb1ng kh\u00f3a ngo\u1ea1i (foreign key)<\/td><\/tr><tr><td>Ng\u00f4n ng\u1eef truy v\u1ea5n<\/td><td>MongoDB Query Language (MQL) \u2013 c\u00fa ph\u00e1p JSON<\/td><td>SQL c\u00f3 c\u00fa ph\u00e1p chu\u1ea9n, d\u00f9ng SELECT, JOIN, GROUP BY\u2026<\/td><\/tr><tr><td>T\u00ednh linh ho\u1ea1t (Schema)<\/td><td>Schema linh ho\u1ea1t &#8211; m\u1ed7i document c\u00f3 th\u1ec3 kh\u00e1c nhau<\/td><td>Schema c\u1ed1 \u0111\u1ecbnh, m\u1ecdi b\u1ea3n ghi ph\u1ea3i tu\u00e2n th\u1ee7 c\u00f9ng c\u1ea5u tr\u00fac<\/td><\/tr><tr><td>M\u1ed1i quan h\u1ec7 d\u1eef li\u1ec7u<\/td><td>Kh\u00f4ng h\u1ed7 tr\u1ee3 join ph\u1ee9c t\u1ea1p; c\u00f3 th\u1ec3 d\u00f9ng<code> $lookup<\/code> ho\u1eb7c embedded documents<\/td><td>H\u1ed7 tr\u1ee3 JOIN nhi\u1ec1u b\u1ea3ng \u0111\u1ec3 k\u1ebft n\u1ed1i d\u1eef li\u1ec7u quan h\u1ec7<\/td><\/tr><tr><td>Kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng (Scalability)<\/td><td>M\u1edf r\u1ed9ng theo chi\u1ec1u ngang (horizontal scaling) th\u00f4ng qua sharding, ngh\u0129a l\u00e0 chia d\u1eef li\u1ec7u sang nhi\u1ec1u server d\u1ec5 d\u00e0ng<\/td><td>M\u1edf r\u1ed9ng theo chi\u1ec1u d\u1ecdc (vertical scaling), ngh\u0129a l\u00e0 t\u0103ng t\u00e0i nguy\u00ean ph\u1ea7n c\u1ee9ng cho 1 server l\u00e0 ch\u00ednh<\/td><\/tr><tr><td>Hi\u1ec7u n\u0103ng<\/td><td>Truy xu\u1ea5t nhanh v\u1edbi d\u1eef li\u1ec7u l\u1edbn, kh\u00f4ng c\u1ea7n join, ph\u00f9 h\u1ee3p v\u1edbi h\u1ec7 th\u1ed1ng thi\u00ean v\u1ec1 \u0111\u1ecdc (read-heavy workload)<\/td><td>C\u00f3 th\u1ec3 ch\u1eadm h\u01a1n n\u1ebfu join ho\u1eb7c subquery nhi\u1ec1u, nh\u01b0ng t\u1ed1i \u01b0u t\u1ed1t cho c\u00e1c truy v\u1ea5n ph\u1ee9c t\u1ea1p<\/td><\/tr><tr><td>Giao d\u1ecbch (Transaction)<\/td><td>H\u1ed7 tr\u1ee3 transactions tr\u00ean nhi\u1ec1u document t\u1eeb MongoDB 4.0, \u0111\u1ea3m b\u1ea3o ACID compliance&nbsp;<\/td><td>H\u1ed7 tr\u1ee3 transaction m\u1ea1nh m\u1ebd v\u00e0 nh\u1ea5t qu\u00e1n ACID t\u1eeb l\u00e2u<\/td><\/tr><tr><td>T\u00ednh nh\u1ea5t qu\u00e1n (Consistency)<\/td><td>Theo m\u00f4 h\u00ecnh BASE (Basically Available, Soft state, Eventual consistency), nh\u01b0ng c\u00f3 th\u1ec3 c\u1ea5u h\u00ecnh \u0111\u1ed9 nh\u1ea5t qu\u00e1n m\u1ea1nh (strong consistency) th\u00f4ng qua <em>read concern<\/em> v\u00e0 <em>write concern<\/em>. v\u1edbi read\/write concern<\/td><td>Theo m\u00f4 h\u00ecnh ACID (Atomicity, Consistency, Isolation, Durability)<\/td><\/tr><tr><td>\u1ee8ng d\u1ee5ng<\/td><td>H\u1ec7 th\u1ed1ng big data, real-time analytics, IoT, \u1ee9ng d\u1ee5ng linh ho\u1ea1t, qu\u1ea3n l\u00fd n\u1ed9i dung, product catalogs<\/td><td>H\u1ec7 th\u1ed1ng ng\u00e2n h\u00e0ng, t\u00e0i ch\u00ednh, k\u1ebf to\u00e1n, y\u00eau c\u1ea7u t\u00ednh to\u00e0n v\u1eb9n cao<\/td><\/tr><tr><td>C\u1ea5u tr\u00fac l\u01b0u tr\u1eef<\/td><td>L\u01b0u tr\u1eef d\u01b0\u1edbi d\u1ea1ng collection ch\u1ee9a c\u00e1c document BSON<\/td><td>L\u01b0u tr\u1eef d\u01b0\u1edbi d\u1ea1ng b\u1ea3ng, h\u00e0ng, c\u1ed9t<\/td><\/tr><tr><td>C\u00f4ng c\u1ee5 h\u1ed7 tr\u1ee3<\/td><td>C\u00f3 Aggregation Framework, Change Streams, Atlas, Spark Connector \u0111\u1ec3 x\u1eed l\u00fd v\u00e0 ph\u00e2n t\u00edch d\u1eef li\u1ec7u l\u1edbn<\/td><td>C\u00f3 Stored Procedures, Triggers, Views, Joins v\u00e0 c\u00e1c c\u00f4ng c\u1ee5 t\u1ed1i \u01b0u truy v\u1ea5n truy\u1ec1n th\u1ed1ng.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-bson-la-gi-va-t\u1ea1i-sao-mongodb-dung-bson-thay-vi-json-thu\u1ea7n\"><strong>BSON l\u00e0 g\u00ec v\u00e0 t\u1ea1i sao MongoDB d\u00f9ng BSON thay v\u00ec JSON thu\u1ea7n?<\/strong><\/h3>\n\n\n\n<p>BSON (Binary JSON) l\u00e0 \u0111\u1ecbnh d\u1ea1ng nh\u1ecb ph\u00e2n \u0111\u01b0\u1ee3c MongoDB d\u00f9ng \u0111\u1ec3 l\u01b0u tr\u1eef v\u00e0 truy\u1ec1n d\u1eef li\u1ec7u.<\/p>\n\n\n\n<p>N\u00f3i \u0111\u01a1n gi\u1ea3n, BSON l\u00e0 phi\u00ean b\u1ea3n \u201cn\u00e2ng c\u1ea5p\u201d c\u1ee7a JSON, \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf \u0111\u1ec3 m\u00e1y t\u00ednh \u0111\u1ecdc nhanh h\u01a1n, ghi nhanh h\u01a1n v\u00e0 linh ho\u1ea1t h\u01a1n trong vi\u1ec7c x\u1eed l\u00fd d\u1eef li\u1ec7u. V\u00ed d\u1ee5 m\u1ed9t document trong MongoDB m\u00e0 b\u1ea1n th\u1ea5y nh\u01b0 th\u1ebf n\u00e0y (d\u1ea1ng JSON):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"name\": \"An Nguyen\",\n  \"age\": 25,\n  \"skills\": &#91;\"Node.js\", \"MongoDB\", \"Python\"],\n  \"joinedAt\": \"2024-10-05T10:00:00Z\"\n}<\/code><\/pre>\n\n\n\n<p>Khi \u0111\u01b0\u1ee3c l\u01b0u trong c\u01a1 s\u1edf d\u1eef li\u1ec7u, MongoDB chuy\u1ec3n \u0111\u1ed5i n\u00f3 th\u00e0nh d\u1ea1ng BSON, \u0111\u1ec3 t\u1ed1i \u01b0u t\u1ed1c \u0111\u1ed9 \u0111\u1ecdc-ghi v\u00e0 ti\u1ebft ki\u1ec7m kh\u00f4ng gian l\u01b0u tr\u1eef.<\/p>\n\n\n\n<p>T\u1ea1i sao MongoDB d\u00f9ng BSON thay v\u00ec JSON thu\u1ea7n?<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>L\u00fd do<\/strong><\/td><td><strong>Gi\u1ea3i th\u00edch<\/strong><\/td><\/tr><tr><td><strong>1. Hi\u1ec7u n\u0103ng cao h\u01a1n<\/strong><\/td><td>BSON \u0111\u01b0\u1ee3c l\u01b0u \u1edf d\u1ea1ng nh\u1ecb ph\u00e2n, n\u00ean m\u00e1y t\u00ednh \u0111\u1ecdc nhanh h\u01a1n nhi\u1ec1u so v\u1edbi v\u0103n b\u1ea3n JSON (v\u00ec kh\u00f4ng c\u1ea7n parse t\u1eebng k\u00fd t\u1ef1), v\u00e0 c\u00f3 th\u1ec3 traverse document m\u00e0 kh\u00f4ng c\u1ea7n parse to\u00e0n b\u1ed9.<\/td><\/tr><tr><td><strong>2. H\u1ed7 tr\u1ee3 nhi\u1ec1u ki\u1ec3u d\u1eef li\u1ec7u h\u01a1n<\/strong><\/td><td>JSON ch\u1ec9 h\u1ed7 tr\u1ee3 string, number, boolean, array, object. C\u00f2n BSON h\u1ed7 tr\u1ee3 th\u00eam c\u00e1c ki\u1ec3u nh\u01b0 Date, Binary, ObjectId, Decimal128, Timestamp, &#8230;<\/td><\/tr><tr><td><strong>3. T\u00ednh to\u00e0n v\u1eb9n v\u00e0 \u0111\u1ecbnh v\u1ecb nhanh<\/strong><\/td><td>BSON l\u01b0u th\u00eam th\u00f4ng tin \u0111\u1ed9 d\u00e0i v\u00e0 ki\u1ec3u d\u1eef li\u1ec7u, gi\u00fap MongoDB truy c\u1eadp t\u1eebng tr\u01b0\u1eddng nhanh m\u00e0 kh\u00f4ng ph\u1ea3i \u0111\u1ecdc h\u1ebft document, cho ph\u00e9p skip v\u00e0 seek hi\u1ec7u qu\u1ea3<\/td><\/tr><tr><td><strong>4. T\u1ed1i \u01b0u cho l\u01b0u tr\u1eef v\u00e0 network<\/strong><\/td><td>V\u00ec l\u00e0 d\u1ea1ng nh\u1ecb ph\u00e2n n\u00e9n, BSON g\u1ecdn nh\u1eb9 h\u01a1n, gi\u00fap gi\u1ea3m dung l\u01b0\u1ee3ng v\u00e0 t\u0103ng t\u1ed1c \u0111\u1ed9 truy\u1ec1n t\u1ea3i qua m\u1ea1ng. L\u01b0u \u00fd: BSON kh\u00f4ng ph\u1ea3i lu\u00f4n nh\u1ecf h\u01a1n JSON, \u0111\u00f4i khi c\u00f3 th\u1ec3 l\u1edbn h\u01a1n do metadata.<\/td><\/tr><tr><td><strong>5. D\u1ec5 m\u1edf r\u1ed9ng trong t\u01b0\u01a1ng lai<\/strong><\/td><td>BSON \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf c\u00f3 th\u1ec3 th\u00eam ki\u1ec3u d\u1eef li\u1ec7u m\u1edbi m\u00e0 kh\u00f4ng l\u00e0m m\u1ea5t t\u01b0\u01a1ng th\u00edch v\u1edbi \u0111\u1ecbnh d\u1ea1ng c\u0169.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-find-va-findone-khac-nhau-nh\u01b0-th\u1ebf-nao\"><strong>find()<\/strong><strong> v\u00e0 <\/strong><strong>findOne()<\/strong><strong> kh\u00e1c nhau nh\u01b0 th\u1ebf n\u00e0o?<\/strong><\/h3>\n\n\n\n<p>Trong MongoDB, c\u1ea3 find() v\u00e0 findOne() \u0111\u1ec1u d\u00f9ng \u0111\u1ec3 truy v\u1ea5n d\u1eef li\u1ec7u, nh\u01b0ng kh\u00e1c nhau \u1edf s\u1ed1 l\u01b0\u1ee3ng k\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1 v\u00e0 c\u00e1ch s\u1eed d\u1ee5ng nh\u01b0 sau:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>H\u00e0m<\/strong><\/td><td><strong>M\u1ee5c \u0111\u00edch<\/strong><\/td><td><strong>K\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1<\/strong><\/td><td><strong>Khi n\u00e0o n\u00ean d\u00f9ng?<\/strong><\/td><\/tr><tr><td><code>find()<\/code><\/td><td>D\u00f9ng \u0111\u1ec3 l\u1ea5y nhi\u1ec1u document ph\u00f9 h\u1ee3p v\u1edbi \u0111i\u1ec1u ki\u1ec7n.<\/td><td>Tr\u1ea3 v\u1ec1 con tr\u1ecf (cursor) v\u00e0 c\u00f3 th\u1ec3 duy\u1ec7t qua nhi\u1ec1u k\u1ebft qu\u1ea3, h\u1ed7 tr\u1ee3 pagination v\u1edbi limit(), skip(), sort()<\/td><td>Khi b\u1ea1n c\u1ea7n danh s\u00e1ch d\u1eef li\u1ec7u (v\u00ed d\u1ee5: t\u1ea5t c\u1ea3 ng\u01b0\u1eddi d\u00f9ng tr\u00ean 18 tu\u1ed5i).<\/td><\/tr><tr><td><code>findOne()<\/code><\/td><td>D\u00f9ng \u0111\u1ec3 l\u1ea5y m\u1ed9t document duy nh\u1ea5t ph\u00f9 h\u1ee3p v\u1edbi \u0111i\u1ec1u ki\u1ec7n.<\/td><td>Tr\u1ea3 v\u1ec1 document \u0111\u1ea7u ti\u00ean t\u00ecm th\u1ea5y (ho\u1eb7c null n\u1ebfu kh\u00f4ng c\u00f3), kh\u00f4ng tr\u1ea3 v\u1ec1 cursor.<\/td><td>Khi b\u1ea1n ch\u1ec9 c\u1ea7n m\u1ed9t b\u1ea3n ghi c\u1ee5 th\u1ec3 (v\u00ed d\u1ee5: t\u00ecm ng\u01b0\u1eddi d\u00f9ng theo email), hi\u1ec7u n\u0103ng cao h\u01a1n khi ch\u1ec9 c\u1ea7n 1 k\u1ebft qu\u1ea3.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Gi\u1ea3 s\u1eed ta c\u00f3 collection users nh\u01b0 sau:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\n  { \"name\": \"An\", \"age\": 25, \"city\": \"Hanoi\" },\n  { \"name\": \"Binh\", \"age\": 30, \"city\": \"Hanoi\" },\n  { \"name\": \"Cuong\", \"age\": 22, \"city\": \"Da Nang\" }\n]<\/code><\/pre>\n\n\n\n<p>S\u1eed d\u1ee5ng <code>find()<\/code> \u0111\u1ec3 t\u00ecm nh\u1eefng ng\u01b0\u1eddi s\u1ed1ng \u1edf H\u00e0 N\u1ed9i v\u1edbi c\u00fa ph\u00e1p:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({ city: \"Hanoi\" })<\/code><\/pre>\n\n\n\n<p>=&gt; K\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1 t\u1ea5t c\u1ea3 ng\u01b0\u1eddi s\u1ed1ng \u1edf H\u00e0 N\u1ed9i:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\n  { \"name\": \"An\", \"age\": 25, \"city\": \"Hanoi\" },\n  { \"name\": \"Binh\", \"age\": 30, \"city\": \"Hanoi\" }\n]<\/code><\/pre>\n\n\n\n<p>C\u00f2n s\u1eed d\u1ee5ng <code>findOne()<\/code> v\u1edbi c\u00fa ph\u00e1p <code>db.users.findOne({ city: \"Hanoi\" })<\/code> s\u1ebd tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3 nh\u01b0 sau:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{ \"name\": \"An\", \"age\": 25, \"city\": \"Hanoi\" }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-id-trong-mongodb-la-gi-vai-tro-c\u1ee7a-id-trong-cac-document-c\u1ee7a-mongodb-la-gi\"><strong>_id trong MongoDB l\u00e0 g\u00ec? Vai tr\u00f2 c\u1ee7a _id trong c\u00e1c document c\u1ee7a MongoDB l\u00e0 g\u00ec?<\/strong><\/h3>\n\n\n\n<p>Trong MongoDB, m\u1ed7i document \u0111\u1ec1u c\u00f3 m\u1ed9t tr\u01b0\u1eddng \u0111\u1eb7c bi\u1ec7t t\u00ean l\u00e0 <code>_id<\/code>, d\u00f9ng \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh duy nh\u1ea5t b\u1ea3n ghi \u0111\u00f3 trong m\u1ed9t collection. Ta c\u00f3 th\u1ec3 coi <code>_id<\/code> gi\u1ed1ng nh\u01b0 kh\u00f3a ch\u00ednh (primary key) trong c\u01a1 s\u1edf d\u1eef li\u1ec7u quan h\u1ec7 (RDBMS). Id c\u00f3 c\u00e1c \u0111\u1eb7c \u0111i\u1ec3m nh\u01b0 sau:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>M\u1ed7i document b\u1eaft bu\u1ed9c ph\u1ea3i c\u00f3 <code>_id<\/code>, n\u1ebfu b\u1ea1n kh\u00f4ng t\u1ef1 t\u1ea1o, MongoDB s\u1ebd t\u1ef1 \u0111\u1ed9ng t\u1ef1 t\u1ea1o.<\/li>\n\n\n\n<li>M\u1ed7i <code>_id<\/code> l\u00e0 duy nh\u1ea5t trong m\u1ed9t collection, gi\u00fap \u0111\u1ea3m b\u1ea3o kh\u00f4ng c\u00f3 hai document tr\u00f9ng nhau.<\/li>\n\n\n\n<li>MongoDB t\u1ef1 t\u1ea1o index m\u1eb7c \u0111\u1ecbnh tr\u00ean tr\u01b0\u1eddng <code>_id<\/code>, n\u00ean vi\u1ec7c t\u00ecm ki\u1ebfm b\u1eb1ng <code>_id<\/code> lu\u00f4n r\u1ea5t nhanh.<\/li>\n\n\n\n<li><code>_id<\/code> c\u00f3 th\u1ec3 l\u00e0 b\u1ea5t k\u1ef3 ki\u1ec3u d\u1eef li\u1ec7u n\u00e0o (string, number, ObjectId, UUID&#8230;) mi\u1ec5n l\u00e0 unique.<\/li>\n\n\n\n<li><code>_id<\/code> l\u00e0 immutable &#8211; kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i sau khi document \u0111\u01b0\u1ee3c t\u1ea1o (tr\u1eeb khi x\u00f3a v\u00e0 t\u1ea1o l\u1ea1i).<\/li>\n<\/ul>\n\n\n\n<p>Theo m\u1eb7c \u0111\u1ecbnh, <code>_id<\/code> \u0111\u01b0\u1ee3c MongoDB t\u1ea1o d\u01b0\u1edbi d\u1ea1ng <code>ObjectId<\/code>, l\u00e0 m\u1ed9t chu\u1ed7i 12 byte ch\u1ee9a:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>4 byte \u0111\u1ea7u: th\u1eddi gian t\u1ea1o (timestamp) &#8211; t\u00ednh b\u1eb1ng gi\u00e2y t\u1eeb Unix epoch<\/li>\n\n\n\n<li>5 byte ti\u1ebfp: m\u00e3 m\u00e1y (machine ID)<\/li>\n\n\n\n<li>3 byte cu\u1ed1i: b\u1ed9 \u0111\u1ebfm t\u1ef1 t\u0103ng (counter) &#8211; \u0111\u1ea3m b\u1ea3o t\u00ednh duy nh\u1ea5t trong c\u00f9ng m\u1ed9t gi\u00e2y<\/li>\n<\/ul>\n\n\n\n<p>V\u00ed d\u1ee5:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"_id\": ObjectId(\"6753e5d9a12c4f0045e2e7d9\"),\n  \"name\": \"An Nguyen\",\n  \"email\": \"an@example.com\"\n}<\/code><\/pre>\n\n\n\n<p>=&gt; T\u1eeb <code>ObjectId<\/code> n\u00e0y, ta c\u00f3 th\u1ec3 suy ra th\u1eddi \u0111i\u1ec3m document \u0111\u01b0\u1ee3c t\u1ea1o (d\u1ef1a tr\u00ean ph\u1ea7n timestamp \u1edf \u0111\u1ea7u m\u00e3) b\u1eb1ng ph\u01b0\u01a1ng th\u1ee9c getTimestamp(): ObjectId(&#8220;6753e5d9a12c4f0045e2e7d9&#8221;).getTimestamp().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-khi-nao-database-th\u1ef1c-s\u1ef1-d\u01b0\u1ee3c-t\u1ea1o-nbsp\"><strong>Khi n\u00e0o \u201cdatabase\u201d th\u1ef1c s\u1ef1 \u0111\u01b0\u1ee3c t\u1ea1o?&nbsp;<\/strong><\/h3>\n\n\n\n<p>Trong MongoDB, database kh\u00f4ng \u0111\u01b0\u1ee3c t\u1ea1o ngay khi b\u1ea1n g\u00f5 l\u1ec7nh <code>use &lt;t\u00ean_database<\/code>&gt;, m\u00e0 ch\u1ec9 th\u1ef1c s\u1ef1 \u0111\u01b0\u1ee3c t\u1ea1o ra khi c\u00f3 d\u1eef li\u1ec7u b\u00ean trong. L\u1ec7nh use ch\u1ec9 chuy\u1ec3n m\u00f4i tr\u01b0\u1eddng l\u00e0m vi\u1ec7c hi\u1ec7n t\u1ea1i sang database \u0111\u00f3, ch\u1ee9 ch\u01b0a t\u1ea1o ra database th\u1eadt. V\u00ed d\u1ee5:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use shopDB<\/code><\/pre>\n\n\n\n<p>MongoDB l\u00fac n\u00e0y ch\u01b0a t\u1ea1o ra \u201cshopDB\u201d trong h\u1ec7 th\u1ed1ng v\u00ec ch\u01b0a c\u00f3 collection ho\u1eb7c document n\u00e0o. Database ch\u1ec9 \u0111\u01b0\u1ee3c t\u1ea1o th\u1ef1c t\u1ebf khi ta:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Th\u00eam d\u1eef li\u1ec7u (d\u00f9ng insertOne(), insertMany(), \u2026), ho\u1eb7c<\/li>\n\n\n\n<li>T\u1ea1o collection th\u1ee7 c\u00f4ng b\u1eb1ng db.createCollection().<\/li>\n<\/ul>\n\n\n\n<p>V\u00ed d\u1ee5:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use shopDB\ndb.products.insertOne({ name: \"T-Shirt\", price: 199000 })<\/code><\/pre>\n\n\n\n<p>=&gt; Ngay khi document \u0111\u1ea7u ti\u00ean \u0111\u01b0\u1ee3c th\u00eam, MongoDB s\u1ebd t\u1ea1o ra shopDB v\u00e0 collection products.<\/p>\n\n\n\n<p>B\u00ean c\u1ea1nh \u0111\u00f3, ta c\u0169ng c\u00f3 th\u1ec3 t\u1ea1o database th\u00f4ng qua l\u1ec7nh t\u1ea1o collection:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use shopDB\ndb.createCollection(\"products\")<\/code><\/pre>\n\n\n\n<p>L\u01b0u \u00fd: \u0110\u1ec3 ki\u1ec3m tra database \u0111\u00e3 \u0111\u01b0\u1ee3c t\u1ea1o th\u1ef1c s\u1ef1, d\u00f9ng l\u1ec7nh show dbs. Khi \u0111\u00f3 ch\u1ec9 nh\u1eefng database c\u00f3 d\u1eef li\u1ec7u m\u1edbi xu\u1ea5t hi\u1ec7n trong danh s\u00e1ch.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-mongodb-l\u01b0u-tr\u1eef-hinh-\u1ea3nh-va-video-dung-l\u01b0\u1ee3ng-l\u1edbn-nh\u01b0-th\u1ebf-nao\"><strong>MongoDB l\u01b0u tr\u1eef h\u00ecnh \u1ea3nh v\u00e0 video dung l\u01b0\u1ee3ng l\u1edbn nh\u01b0 th\u1ebf n\u00e0o?<\/strong><\/h3>\n\n\n\n<p>MongoDB kh\u00f4ng l\u01b0u tr\u1ef1c ti\u1ebfp to\u00e0n b\u1ed9 file l\u1edbn trong m\u1ed9t document, m\u00e0 s\u1eed d\u1ee5ng m\u1ed9t c\u01a1 ch\u1ebf \u0111\u1eb7c bi\u1ec7t g\u1ecdi l\u00e0 GridFS \u0111\u1ec3 chia nh\u1ecf file th\u00e0nh nhi\u1ec1u ph\u1ea7n v\u00e0 l\u01b0u v\u00e0o c\u00e1c collection ri\u00eang. Nh\u1edd \u0111\u00f3, MongoDB c\u00f3 th\u1ec3 x\u1eed l\u00fd, truy\u1ec1n t\u1ea3i v\u00e0 qu\u1ea3n l\u00fd d\u1eef li\u1ec7u \u0111a ph\u01b0\u01a1ng ti\u1ec7n (\u1ea3nh, video, PDF&#8230;) hi\u1ec7u qu\u1ea3 h\u01a1n &#8211; \u0111\u1eb7c bi\u1ec7t ph\u00f9 h\u1ee3p v\u1edbi c\u00e1c \u1ee9ng d\u1ee5ng web, cloud storage, ho\u1eb7c video streaming.<\/p>\n\n\n\n<p>Trong \u0111\u00f3, GridFS (Grid File System) l\u00e0 h\u1ec7 th\u1ed1ng l\u01b0u tr\u1eef file l\u1edbn c\u1ee7a MongoDB, \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf \u0111\u1ec3 x\u1eed l\u00fd c\u00e1c t\u1ec7p c\u00f3 k\u00edch th\u01b0\u1edbc v\u01b0\u1ee3t qu\u00e1 gi\u1edbi h\u1ea1n 16MB\/document. Thay v\u00ec l\u01b0u c\u1ea3 file v\u00e0o m\u1ed9t document, GridFS chia nh\u1ecf file th\u00e0nh nhi\u1ec1u ph\u1ea7n (chunk), m\u1ed7i ph\u1ea7n c\u00f3 k\u00edch th\u01b0\u1edbc m\u1eb7c \u0111\u1ecbnh 255 KB v\u00e0 l\u01b0u t\u1eebng ph\u1ea7n \u0111\u00f3 v\u00e0o collection ri\u00eang. MongoDB s\u1ebd t\u1ea1o hai collection cho m\u1ed7i file:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>fs.files<\/code> \u2192 ch\u1ee9a th\u00f4ng tin metadata (t\u00ean, k\u00edch th\u01b0\u1edbc, lo\u1ea1i file, \u2026)<\/li>\n\n\n\n<li><code>fs.chunks<\/code> \u2192 ch\u1ee9a d\u1eef li\u1ec7u nh\u1ecb ph\u00e2n \u0111\u00e3 \u0111\u01b0\u1ee3c chia nh\u1ecf (c\u00e1c ph\u1ea7n c\u1ee7a file), m\u1ed7i chunk c\u00f3 tr\u01b0\u1eddng files_id tham chi\u1ebfu \u0111\u1ebfn document trong fs.files<\/li>\n<\/ul>\n\n\n\n<p>\u0110\u1ec3 hi\u1ec3u \u0111\u01a1n gi\u1ea3n th\u00ec gi\u1ea3 s\u1eed ta c\u00f3 video 50MB:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>MongoDB chia video th\u00e0nh nhi\u1ec1u m\u1ea3nh 255 KB (kho\u1ea3ng 200 chunks).<\/li>\n\n\n\n<li>L\u01b0u t\u1eebng m\u1ea3nh \u0111\u00f3 v\u00e0o <code>fs.chunks<\/code> v\u1edbi tr\u01b0\u1eddng n \u0111\u00e1nh s\u1ed1 th\u1ee9 t\u1ef1 chunk (0, 1, 2,&#8230;).<\/li>\n\n\n\n<li>L\u01b0u th\u00f4ng tin file g\u1ed1c (t\u00ean, s\u1ed1 chunk, lo\u1ea1i file, MD5 hash \u2026) v\u00e0o <code>fs.files<\/code>.<\/li>\n\n\n\n<li>Khi c\u1ea7n \u0111\u1ecdc l\u1ea1i, MongoDB gh\u00e9p c\u00e1c chunk theo th\u1ee9 t\u1ef1 \u0111\u1ec3 tr\u1ea3 v\u1ec1 file ho\u00e0n ch\u1ec9nh, h\u1ed7 tr\u1ee3 streaming v\u00e0 \u0111\u1ecdc t\u1eebng ph\u1ea7n (range requests).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-mongodb-c\u1ea5p-d\u1ed9-nang-cao\"><span class=\"ez-toc-section\" id=\"Cau_hoi_phong_van_MongoDB_cap_do_nang_cao\"><\/span><strong>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB c\u1ea5p \u0111\u1ed9 n\u00e2ng cao<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-replica-set-amp-high-availability-trong-mongodb-la-gi\"><strong>Replica Set &amp; High Availability trong MongoDB l\u00e0 g\u00ec?<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Replica Set<\/strong>:&nbsp; Trong MongoDB, Replica Set l\u00e0 t\u1eadp h\u1ee3p nhi\u1ec1u m\u00e1y ch\u1ee7 (node) c\u00f9ng l\u01b0u tr\u1eef b\u1ea3n sao c\u1ee7a c\u00f9ng m\u1ed9t d\u1eef li\u1ec7u. M\u1ee5c \u0111\u00edch l\u00e0 \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o d\u1eef li\u1ec7u lu\u00f4n an to\u00e0n v\u00e0 h\u1ec7 th\u1ed1ng lu\u00f4n s\u1eb5n s\u00e0ng, ngay c\u1ea3 khi m\u1ed9t m\u00e1y ch\u1ee7 g\u1eb7p s\u1ef1 c\u1ed1. M\u1ed9t Replica Set th\u01b0\u1eddng g\u1ed3m:\n<ul class=\"wp-block-list\">\n<li>Primary: m\u00e1y ch\u1ee7 ch\u00ednh, n\u01a1i th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c ghi (write) v\u00e0 \u0111\u1ecdc m\u1eb7c \u0111\u1ecbnh, ch\u1ec9 c\u00f3 duy nh\u1ea5t 1 Primary trong m\u1ed9t Replica Set<\/li>\n\n\n\n<li>Secondary: m\u1ed9t ho\u1eb7c nhi\u1ec1u m\u00e1y ch\u1ee7 sao ch\u00e9p d\u1eef li\u1ec7u t\u1eeb Primary theo th\u1eddi gian th\u1ef1c b\u1eb1ng c\u00e1ch \u0111\u1ecdc oplog (operation log)<\/li>\n\n\n\n<li>Arbiter (tu\u1ef3 ch\u1ecdn): node ch\u1ec9 tham gia b\u1ea7u ch\u1ecdn khi c\u1ea7n ch\u1ecdn Primary m\u1edbi, nh\u01b0ng kh\u00f4ng l\u01b0u d\u1eef li\u1ec7u.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>Gi\u1ea3 s\u1eed ta c\u00f3 3 server trong m\u1ed9t Replica Set g\u1ed3m 1 Primary v\u00e0 2 Secondary, n\u1ebfu Primary b\u1ecb l\u1ed7i ho\u1eb7c m\u1ea5t k\u1ebft n\u1ed1i, MongoDB s\u1ebd t\u1ef1 \u0111\u1ed9ng b\u1ea7u m\u1ed9t Secondary m\u1edbi l\u00e0m Primary. V\u00ec v\u1eady \u1ee9ng d\u1ee5ng v\u1eabn ho\u1ea1t \u0111\u1ed9ng, ng\u01b0\u1eddi d\u00f9ng kh\u00f4ng b\u1ecb gi\u00e1n \u0111o\u1ea1n v\u00e0 d\u1eef li\u1ec7u v\u1eabn \u0111\u01b0\u1ee3c b\u1ea3o to\u00e0n.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>High Availability<\/strong>: High Availability (HA) ngh\u0129a l\u00e0 h\u1ec7 th\u1ed1ng lu\u00f4n ho\u1ea1t \u0111\u1ed9ng \u1ed5n \u0111\u1ecbnh, kh\u00f4ng ng\u1eebng tr\u1ec7, d\u00f9 c\u00f3 l\u1ed7i ph\u1ea7n c\u1ee9ng, m\u1ea5t m\u1ea1ng hay downtime. Replica Set ch\u00ednh l\u00e0 c\u00e1ch MongoDB \u0111\u1ea1t \u0111\u01b0\u1ee3c High Availability th\u00f4ng qua:\n<ul class=\"wp-block-list\">\n<li>T\u1ef1 \u0111\u1ed9ng sao l\u01b0u (replication) d\u1eef li\u1ec7u gi\u1eefa c\u00e1c node.<\/li>\n\n\n\n<li>T\u1ef1 \u0111\u1ed9ng failover khi c\u00f3 s\u1ef1 c\u1ed1 v\u1edbi th\u1eddi gian chuy\u1ec3n \u0111\u1ed5i th\u01b0\u1eddng &lt; 12 gi\u00e2y.<\/li>\n\n\n\n<li>Kh\u00f4ng c\u1ea7n can thi\u1ec7p th\u1ee7 c\u00f4ng \u0111\u1ec3 kh\u00f4i ph\u1ee5c ho\u1ea1t \u0111\u1ed9ng.<\/li>\n\n\n\n<li>Read scaling b\u1eb1ng c\u00e1ch ph\u00e2n t\u1ea3i \u0111\u1ecdc sang Secondary nodes.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>=&gt; T\u00f3m l\u1ea1i, Replica Set trong MongoDB gi\u00fap \u0111\u1ea3m b\u1ea3o High Availability b\u1eb1ng c\u00e1ch duy tr\u00ec nhi\u1ec1u b\u1ea3n sao d\u1eef li\u1ec7u tr\u00ean c\u00e1c node kh\u00e1c nhau. N\u1ebfu Primary g\u1eb7p s\u1ef1 c\u1ed1, m\u1ed9t Secondary s\u1ebd t\u1ef1 \u0111\u1ed9ng \u0111\u01b0\u1ee3c b\u1ea7u l\u00e0m Primary m\u1edbi, nh\u1edd \u0111\u00f3 h\u1ec7 th\u1ed1ng li\u00ean t\u1ee5c ho\u1ea1t \u0111\u1ed9ng, kh\u00f4ng m\u1ea5t d\u1eef li\u1ec7u v\u00e0 kh\u00f4ng downtime.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-hay-trinh-bay-v\u1ec1-khai-ni\u1ec7m-sharding-va-khi-nao-nen-s\u1eed-d\u1ee5ng\"><strong>H\u00e3y tr\u00ecnh b\u00e0y v\u1ec1 kh\u00e1i ni\u1ec7m Sharding v\u00e0 khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng?<\/strong><\/h3>\n\n\n\n<p>Sharding l\u00e0 c\u01a1 ch\u1ebf chia nh\u1ecf d\u1eef li\u1ec7u (partitioning) c\u1ee7a MongoDB, cho ph\u00e9p ph\u00e2n t\u00e1n d\u1eef li\u1ec7u ra nhi\u1ec1u m\u00e1y ch\u1ee7 kh\u00e1c nhau thay v\u00ec l\u01b0u to\u00e0n b\u1ed9 trong m\u1ed9t server duy nh\u1ea5t. M\u1ee5c ti\u00eau c\u1ee7a Sharding l\u00e0 gi\u00fap MongoDB c\u00f3 th\u1ec3 x\u1eed l\u00fd kh\u1ed1i l\u01b0\u1ee3ng d\u1eef li\u1ec7u c\u1ef1c l\u1edbn v\u00e0 t\u0103ng hi\u1ec7u n\u0103ng truy v\u1ea5n khi h\u1ec7 th\u1ed1ng ph\u00e1t tri\u1ec3n. N\u00f3i \u0111\u01a1n gi\u1ea3n h\u01a1n l\u00e0 n\u1ebfu d\u1eef li\u1ec7u qu\u00e1 nhi\u1ec1u khi\u1ebfn m\u1ed9t server \u201cqu\u00e1 t\u1ea3i\u201d, MongoDB s\u1ebd chia d\u1eef li\u1ec7u th\u00e0nh nhi\u1ec1u ph\u1ea7n nh\u1ecf (shard) v\u00e0 ph\u00e2n ph\u1ed1i ch\u00fang sang nhi\u1ec1u m\u00e1y kh\u00e1c nhau. Khi b\u1eadt Sharding, MongoDB s\u1ebd c\u00f3 ba th\u00e0nh ph\u1ea7n ch\u00ednh:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Shard<\/strong>: N\u01a1i l\u01b0u tr\u1eef d\u1eef li\u1ec7u th\u1eadt s\u1ef1, m\u1ed7i shard ch\u1ee9a m\u1ed9t ph\u1ea7n c\u1ee7a to\u00e0n b\u1ed9 dataset. M\u1ed7i shard n\u00ean l\u00e0 m\u1ed9t Replica Set \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o high availability.<\/li>\n\n\n\n<li><strong>Config Server<\/strong>: L\u01b0u th\u00f4ng tin c\u1ea5u h\u00ecnh v\u00e0 b\u1ea3n \u0111\u1ed3 ph\u00e2n ph\u1ed1i d\u1eef li\u1ec7u gi\u1eefa c\u00e1c shard (metadata v\u1ec1 shard key ranges, chunk distribution). C\u0169ng n\u00ean ch\u1ea1y d\u01b0\u1edbi d\u1ea1ng Replica Set (CSRS &#8211; Config Server Replica Set).<\/li>\n\n\n\n<li><strong>Query Router (mongos)<\/strong>: Ti\u1ebfp nh\u1eadn truy v\u1ea5n t\u1eeb \u1ee9ng d\u1ee5ng, x\u00e1c \u0111\u1ecbnh d\u1eef li\u1ec7u n\u1eb1m \u1edf shard n\u00e0o v\u00e0 g\u1eedi y\u00eau c\u1ea7u \u0111\u1ebfn shard \u0111\u00f3, sau \u0111\u00f3 merge k\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1 cho client<\/li>\n<\/ul>\n\n\n\n<p>V\u00ed d\u1ee5 ta c\u00f3 m\u1ed9t collection users c\u00f3 100 tri\u1ec7u b\u1ea3n ghi th\u00ec MongoDB c\u00f3 th\u1ec3 chia th\u00e0nh 3 shard:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Shard 1 ch\u1ee9a user \u1edf khu v\u1ef1c ch\u00e2u \u00c1<\/li>\n\n\n\n<li>Shard 2 ch\u1ee9a user \u1edf ch\u00e2u \u00c2u<\/li>\n\n\n\n<li>Shard 3 ch\u1ee9a user \u1edf M\u1ef9<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-v\u1eady-nh\u1eefng-tr\u01b0\u1eddng-h\u1ee3p-nao-thi-dung-sharding\"><strong>V\u1eady nh\u1eefng tr\u01b0\u1eddng h\u1ee3p n\u00e0o th\u00ec d\u00f9ng Sharding?<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Khi d\u1eef li\u1ec7u qu\u00e1 l\u1edbn \u0111\u1ec3 l\u01b0u tr\u00ean m\u1ed9t server duy nh\u1ea5t<\/strong>: N\u1ebfu dung l\u01b0\u1ee3ng d\u1eef li\u1ec7u v\u01b0\u1ee3t qu\u00e1 kh\u1ea3 n\u0103ng l\u01b0u tr\u1eef ho\u1eb7c x\u1eed l\u00fd c\u1ee7a m\u1ed9t m\u00e1y ch\u1ee7, Sharding gi\u00fap chia nh\u1ecf d\u1eef li\u1ec7u th\u00e0nh nhi\u1ec1u ph\u1ea7n (shard) v\u00e0 ph\u00e2n ph\u1ed1i sang nhi\u1ec1u m\u00e1y kh\u00e1c nhau. Nh\u1edd \u0111\u00f3, ta c\u00f3 th\u1ec3 m\u1edf r\u1ed9ng h\u1ec7 th\u1ed1ng theo chi\u1ec1u ngang (horizontal scaling) d\u1ec5 d\u00e0ng ch\u1ec9 b\u1eb1ng c\u00e1ch th\u00eam server m\u1edbi.<\/li>\n\n\n\n<li><strong>Khi h\u1ec7 th\u1ed1ng c\u1ea7n x\u1eed l\u00fd l\u01b0\u1ee3ng truy v\u1ea5n v\u00e0 ghi d\u1eef li\u1ec7u kh\u1ed5ng l\u1ed3:<\/strong> Trong c\u00e1c \u1ee9ng d\u1ee5ng c\u00f3 l\u01b0\u1ee3ng ng\u01b0\u1eddi d\u00f9ng l\u1edbn (v\u00ed d\u1ee5: m\u1ea1ng x\u00e3 h\u1ed9i, s\u00e0n th\u01b0\u01a1ng m\u1ea1i \u0111i\u1ec7n t\u1eed), m\u1ed7i gi\u00e2y c\u00f3 th\u1ec3 c\u00f3 h\u00e0ng ngh\u00ecn truy v\u1ea5n \u0111\u1ecdc\/ghi. Sharding cho ph\u00e9p nhi\u1ec1u server x\u1eed l\u00fd song song, t\u1eeb \u0111\u00f3 gi\u00fap t\u0103ng t\u1ed1c \u0111\u1ed9 ph\u1ea3n h\u1ed3i v\u00e0 tr\u00e1nh ngh\u1ebdn c\u1ed5 chai (bottleneck).<\/li>\n\n\n\n<li><strong>Khi d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c ph\u00e2n b\u1ed1 theo khu v\u1ef1c \u0111\u1ecba l\u00fd<\/strong>: N\u1ebfu \u1ee9ng d\u1ee5ng c\u1ee7a b\u1ea1n c\u00f3 ng\u01b0\u1eddi d\u00f9ng \u1edf nhi\u1ec1u qu\u1ed1c gia (v\u00ed d\u1ee5: ch\u00e2u \u00c1, ch\u00e2u \u00c2u, M\u1ef9), ta c\u00f3 th\u1ec3 ph\u00e2n chia d\u1eef li\u1ec7u theo khu v\u1ef1c (region-based sharding). L\u1ee3i \u00edch s\u1ebd l\u00e0 gi\u00fap truy v\u1ea5n c\u1ee7a ng\u01b0\u1eddi d\u00f9ng \u0111\u01b0\u1ee3c x\u1eed l\u00fd g\u1ea7n v\u1edbi v\u1ecb tr\u00ed h\u1ecd h\u01a1n, t\u1eeb \u0111\u00f3 gi\u1ea3m \u0111\u1ed9 tr\u1ec5 (latency) v\u00e0 t\u0103ng tr\u1ea3i nghi\u1ec7m ng\u01b0\u1eddi d\u00f9ng.<\/li>\n\n\n\n<li><strong>Khi c\u1ea7n m\u1edf r\u1ed9ng h\u1ec7 th\u1ed1ng theo t\u1eebng giai \u0111o\u1ea1n (scale-out)<\/strong>: V\u1edbi Sharding, b\u1ea1n kh\u00f4ng c\u1ea7n thay \u0111\u1ed5i c\u1ea5u tr\u00fac \u1ee9ng d\u1ee5ng, ch\u1ec9 c\u1ea7n th\u00eam m\u00e1y ch\u1ee7 m\u1edbi (shard) \u0111\u1ec3 ch\u1ee9a th\u00eam d\u1eef li\u1ec7u. \u0110i\u1ec1u n\u00e0y gi\u00fap m\u1edf r\u1ed9ng linh ho\u1ea1t, ph\u00f9 h\u1ee3p v\u1edbi c\u00e1c h\u1ec7 th\u1ed1ng ph\u00e1t tri\u1ec3n nhanh m\u00e0 v\u1eabn mu\u1ed1n ti\u1ebft ki\u1ec7m chi ph\u00ed ban \u0111\u1ea7u.<\/li>\n\n\n\n<li><strong>Khi hi\u1ec7u su\u1ea5t truy v\u1ea5n gi\u1ea3m d\u1ea7n do d\u1eef li\u1ec7u qu\u00e1 l\u1edbn<\/strong>: N\u1ebfu b\u1ea1n nh\u1eadn th\u1ea5y c\u00e1c truy v\u1ea5n ng\u00e0y c\u00e0ng ch\u1eadm v\u00ec l\u01b0\u1ee3ng d\u1eef li\u1ec7u t\u0103ng l\u00ean, Sharding c\u00f3 th\u1ec3 gi\u00fap ph\u00e2n t\u00e1n workload gi\u1eefa nhi\u1ec1u m\u00e1y. V\u00ec m\u1ed7i shard ch\u1ec9 x\u1eed l\u00fd m\u1ed9t ph\u1ea7n d\u1eef li\u1ec7u n\u00ean s\u1ebd gi\u00fap gi\u1ea3m t\u1ea3i, t\u0103ng hi\u1ec7u n\u0103ng v\u00e0 t\u1ed1i \u01b0u ch\u1ec9 m\u1ee5c (index).<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-mongodb-gi\u1ea3i-quy\u1ebft-concurrency-control-nh\u01b0-th\u1ebf-nao\"><strong>MongoDB gi\u1ea3i quy\u1ebft concurrency control nh\u01b0 th\u1ebf n\u00e0o?<\/strong><\/h3>\n\n\n\n<p>Concurrency Control (ki\u1ec3m so\u00e1t truy c\u1eadp \u0111\u1ed3ng th\u1eddi) l\u00e0 c\u01a1 ch\u1ebf gi\u00fap nhi\u1ec1u ng\u01b0\u1eddi ho\u1eb7c nhi\u1ec1u ti\u1ebfn tr\u00ecnh c\u00f3 th\u1ec3 \u0111\u1ecdc v\u00e0 ghi d\u1eef li\u1ec7u c\u00f9ng l\u00fac m\u00e0 kh\u00f4ng g\u00e2y xung \u0111\u1ed9t ho\u1eb7c sai l\u1ec7ch d\u1eef li\u1ec7u. N\u00f3i m\u1ed9t c\u00e1ch d\u1ec5 hi\u1ec3u l\u00e0 khi c\u00f3 nhi\u1ec1u ng\u01b0\u1eddi c\u00f9ng thao t\u00e1c tr\u00ean c\u00f9ng m\u1ed9t d\u1eef li\u1ec7u v\u00ed d\u1ee5 nh\u01b0 hai ng\u01b0\u1eddi c\u00f9ng ch\u1ec9nh s\u1eeda h\u1ed3 s\u01a1 kh\u00e1ch h\u00e0ng ho\u1eb7c c\u00f9ng c\u1eadp nh\u1eadt s\u1ed1 d\u01b0 t\u00e0i kho\u1ea3n th\u00ec Concurrency Control s\u1ebd \u0111\u1ea3m b\u1ea3o c\u00e1c thay \u0111\u1ed5i di\u1ec5n ra theo tr\u1eadt t\u1ef1 h\u1ee3p l\u00fd, \u0111\u1ec3 k\u1ebft qu\u1ea3 cu\u1ed1i c\u00f9ng lu\u00f4n ch\u00ednh x\u00e1c. N\u1ebfu kh\u00f4ng c\u00f3 Concurrency Control, hai thay \u0111\u1ed5i n\u00e0y c\u00f3 th\u1ec3 \u201c\u0111\u00e8\u201d l\u00ean nhau, khi\u1ebfn h\u1ec7 th\u1ed1ng l\u01b0u sai d\u1eef li\u1ec7u ho\u1eb7c m\u1ea5t th\u00f4ng tin.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-hay-trinh-bay-v\u1ec1-transaction-amp-write-concern-read-preference-trong-moi-tr\u01b0\u1eddng-phan-tan\"><strong>H\u00e3y tr\u00ecnh b\u00e0y v\u1ec1 Transaction &amp; Write Concern \/ Read Preference trong m\u00f4i tr\u01b0\u1eddng ph\u00e2n t\u00e1n<\/strong><\/h3>\n\n\n\n<p>Trong m\u00f4i tr\u01b0\u1eddng c\u01a1 s\u1edf d\u1eef li\u1ec7u ph\u00e2n t\u00e1n (distributed environment), d\u1eef li\u1ec7u c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef tr\u00ean nhi\u1ec1u m\u00e1y ch\u1ee7 (node) kh\u00e1c nhau \u0111\u1ec3 t\u0103ng hi\u1ec7u n\u0103ng v\u00e0 \u0111\u1ed9 tin c\u1eady. V\u00ec v\u1eady, MongoDB cung c\u1ea5p c\u00e1c c\u01a1 ch\u1ebf nh\u01b0 Transaction, Write Concern v\u00e0 Read Preference \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o d\u1eef li\u1ec7u lu\u00f4n ch\u00ednh x\u00e1c v\u00e0 \u0111\u1ed3ng b\u1ed9 d\u00f9 h\u1ec7 th\u1ed1ng ho\u1ea1t \u0111\u1ed9ng tr\u00ean nhi\u1ec1u node.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-transaction-giao-d\u1ecbch-d\u1ea3m-b\u1ea3o-tinh-toan-v\u1eb9n-d\u1eef-li\u1ec7u\"><strong>Transaction &#8211; Giao d\u1ecbch \u0111\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n d\u1eef li\u1ec7u<\/strong><\/h4>\n\n\n\n<p>Transaction (giao d\u1ecbch) cho ph\u00e9p g\u1ed9p nhi\u1ec1u thao t\u00e1c ghi (write) th\u00e0nh m\u1ed9t kh\u1ed1i logic duy nh\u1ea5t, v\u00e0 \u0111\u1ea3m b\u1ea3o \u201cho\u1eb7c t\u1ea5t c\u1ea3 thao t\u00e1c \u0111\u1ec1u th\u00e0nh c\u00f4ng, ho\u1eb7c t\u1ea5t c\u1ea3 b\u1ecb h\u1ee7y (rollback) n\u1ebfu c\u00f3 l\u1ed7i\u201d. Nh\u1edd v\u1eady m\u00e0 MongoDB c\u00f3 th\u1ec3 duy tr\u00ec c\u00e1c t\u00ednh ACID (Atomicity, Consistency, Isolation, Durability) t\u01b0\u01a1ng t\u1ef1 c\u00e1c h\u1ec7 SQL truy\u1ec1n th\u1ed1ng. V\u00ed d\u1ee5 khi chuy\u1ec3n ti\u1ec1n gi\u1eefa hai t\u00e0i kho\u1ea3n:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>session.startTransaction();\ndb.accounts.updateOne({ name: \"A\" }, { $inc: { balance: -100 } });\ndb.accounts.updateOne({ name: \"B\" }, { $inc: { balance: +100 } });\nsession.commitTransaction();<\/code><\/pre>\n\n\n\n<p>=&gt; N\u1ebfu m\u1ed9t thao t\u00e1c th\u1ea5t b\u1ea1i, to\u00e0n b\u1ed9 giao d\u1ecbch s\u1ebd b\u1ecb rollback \u0111\u1ec3 d\u1eef li\u1ec7u kh\u00f4ng b\u1ecb sai l\u1ec7ch.<\/p>\n\n\n\n<p>L\u01b0u \u00fd v\u1ec1 Transactions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>T\u1eeb MongoDB 4.0: h\u1ed7 tr\u1ee3 multi-document transactions trong replica sets<\/li>\n\n\n\n<li>T\u1eeb MongoDB 4.2: h\u1ed7 tr\u1ee3 distributed transactions trong sharded clusters<\/li>\n\n\n\n<li>Transactions c\u00f3 overhead v\u1ec1 performance &#8211; ch\u1ec9 d\u00f9ng khi c\u1ea7n thi\u1ebft<\/li>\n\n\n\n<li>Timeout m\u1eb7c \u0111\u1ecbnh: 60 gi\u00e2y (c\u00f3 th\u1ec3 c\u1ea5u h\u00ecnh)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-write-concern-m\u1ee9c-d\u1ed9-d\u1ea3m-b\u1ea3o-khi-ghi-d\u1eef-li\u1ec7u\"><strong>Write Concern &#8211; M\u1ee9c \u0111\u1ed9 \u0111\u1ea3m b\u1ea3o khi ghi d\u1eef li\u1ec7u<\/strong><\/h3>\n\n\n\n<p>Write Concern x\u00e1c \u0111\u1ecbnh m\u1ee9c \u0111\u1ed9 x\u00e1c nh\u1eadn (acknowledgment) m\u00e0 MongoDB c\u1ea7n nh\u1eadn \u0111\u01b0\u1ee3c sau khi ghi d\u1eef li\u1ec7u.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>C\u1ea5u h\u00ecnh<\/strong><\/td><td><strong>\u00dd ngh\u0129a<\/strong><\/td><\/tr><tr><td><code>{ w: 1 }<\/code><\/td><td>Ghi th\u00e0nh c\u00f4ng khi Primary x\u00e1c nh\u1eadn. (Nhanh nh\u01b0ng \u00edt an to\u00e0n)<\/td><\/tr><tr><td><code>{ w: \"majority\" }<\/code><\/td><td>Ch\u1edd \u0111a s\u1ed1 node trong Replica Set x\u00e1c nh\u1eadn. (An to\u00e0n cao h\u01a1n, ch\u1eadm h\u01a1n m\u1ed9t ch\u00fat)<\/td><\/tr><tr><td><code>{ w: 0 }<\/code><\/td><td>Kh\u00f4ng c\u1ea7n x\u00e1c nh\u1eadn. (Hi\u1ec7u n\u0103ng cao, nh\u01b0ng r\u1ee7i ro m\u1ea5t d\u1eef li\u1ec7u)<\/td><\/tr><tr><td><code>{ w: 2 }<\/code><\/td><td>Ch\u1edd Primary + \u00edt nh\u1ea5t 1 Secondary x\u00e1c nh\u1eadn<\/td><\/tr><tr><td><code>{ w: 3 }<\/code><\/td><td>Ch\u1edd Primary + \u00edt nh\u1ea5t 2 Secondary x\u00e1c nh\u1eadn<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Trong h\u1ec7 th\u1ed1ng ph\u00e2n t\u00e1n, Write Concern gi\u00fap c\u00e2n b\u1eb1ng gi\u1eefa t\u1ed1c \u0111\u1ed9 ghi v\u00e0 \u0111\u1ed9 an to\u00e0n. V\u00e0 n\u1ebfu h\u1ec7 th\u1ed1ng y\u00eau c\u1ea7u \u0111\u1ed9 tin c\u1eady cao nh\u01b0 giao d\u1ecbch t\u00e0i ch\u00ednh th\u00ec n\u00ean d\u00f9ng w: &#8220;majority&#8221;.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-read-preference-l\u1ef1a-ch\u1ecdn-n\u01a1i-d\u1ecdc-d\u1eef-li\u1ec7u\"><strong>Read Preference &#8211; L\u1ef1a ch\u1ecdn n\u01a1i \u0111\u1ecdc d\u1eef li\u1ec7u<\/strong><\/h4>\n\n\n\n<p>Trong Replica Set, d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c l\u01b0u \u1edf nhi\u1ec1u node (Primary v\u00e0 Secondary). Read Preference cho ph\u00e9p b\u1ea1n ch\u1ecdn \u0111\u1ecdc d\u1eef li\u1ec7u t\u1eeb \u0111\u00e2u t\u00f9y v\u00e0o m\u1ee5c ti\u00eau hi\u1ec7u n\u0103ng ho\u1eb7c t\u00ednh nh\u1ea5t qu\u00e1n. C\u00f2n trong h\u1ec7 th\u1ed1ng ph\u00e2n t\u00e1n, Read Preference gi\u00fap c\u00e2n b\u1eb1ng t\u1ea3i (load balancing) v\u00e0 gi\u1ea3m \u0111\u1ed9 tr\u1ec5 (latency) khi ng\u01b0\u1eddi d\u00f9ng \u1edf nhi\u1ec1u khu v\u1ef1c \u0111\u1ecba l\u00fd kh\u00e1c nhau.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Ch\u1ebf \u0111\u1ed9<\/strong><\/td><td><strong>M\u00f4 t\u1ea3<\/strong><\/td><\/tr><tr><td><code>primary<\/code><\/td><td>Lu\u00f4n \u0111\u1ecdc t\u1eeb Primary n\u00ean d\u1eef li\u1ec7u lu\u00f4n m\u1edbi nh\u1ea5t, gi\u00fap \u0111\u1ea3m b\u1ea3o nh\u1ea5t qu\u00e1n.<\/td><\/tr><tr><td><code>secondary<\/code><\/td><td>\u0110\u1ecdc t\u1eeb Secondary n\u00ean gi\u00fap gi\u1ea3m t\u1ea3i cho Primary, nh\u01b0ng c\u00f3 th\u1ec3 \u0111\u1ecdc d\u1eef li\u1ec7u stale (ch\u01b0a \u0111\u01b0\u1ee3c replicate \u0111\u1ea7y \u0111\u1ee7).<\/td><\/tr><tr><td><code>primaryPreferred<\/code><\/td><td>\u01afu ti\u00ean Primary, n\u1ebfu kh\u00f4ng c\u00f3 th\u00ec \u0111\u1ecdc Secondary. T\u1ed1t cho high availability.<\/td><\/tr><tr><td><code>secondaryPreferred<\/code><\/td><td>\u01afu ti\u00ean Secondary, n\u1ebfu kh\u00f4ng c\u00f3 th\u00ec \u0111\u1ecdc Primary. T\u1ed1t cho read-heavy workload.<\/td><\/tr><tr><td><code>nearest<\/code><\/td><td>\u0110\u1ecdc t\u1eeb node g\u1ea7n nh\u1ea5t (t\u00ednh theo \u0111\u1ed9 tr\u1ec5 m\u1ea1ng). T\u1ed1t cho \u1ee9ng d\u1ee5ng ph\u00e2n t\u00e1n \u0111a khu v\u1ef1c (geo-distributed applications).<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Trong m\u00f4i tr\u01b0\u1eddng ph\u00e2n t\u00e1n, Transaction \u0111\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n d\u1eef li\u1ec7u, Write Concern ki\u1ec3m so\u00e1t \u0111\u1ed9 tin c\u1eady khi ghi v\u00e0 Read Preference t\u1ed1i \u01b0u n\u01a1i \u0111\u1ecdc d\u1eef li\u1ec7u. Ba c\u01a1 ch\u1ebf n\u00e0y ph\u1ed1i h\u1ee3p gi\u00fap MongoDB v\u1eeba an to\u00e0n, v\u1eeba linh ho\u1ea1t v\u00e0 hi\u1ec7u n\u0103ng cao ngay c\u1ea3 khi ch\u1ea1y tr\u00ean nhi\u1ec1u m\u00e1y ch\u1ee7 kh\u1eafp th\u1ebf gi\u1edbi.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-hay-so-sanh-aggregation-pipeline-va-mapreduce-trong-mongodb\"><strong>H\u00e3y so s\u00e1nh Aggregation Pipeline v\u00e0 MapReduce trong MongoDB<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Ti\u00eau ch\u00ed<\/strong><\/td><td><strong>Aggregation Pipeline<\/strong><\/td><td><strong>MapReduce<\/strong><\/td><\/tr><tr><td><strong>Kh\u00e1i ni\u1ec7m<\/strong><\/td><td>L\u00e0 c\u01a1 ch\u1ebf x\u1eed l\u00fd d\u1eef li\u1ec7u theo chu\u1ed7i c\u00e1c giai \u0111o\u1ea1n (pipeline). M\u1ed7i giai \u0111o\u1ea1n th\u1ef1c hi\u1ec7n m\u1ed9t t\u00e1c v\u1ee5 nh\u01b0 l\u1ecdc (<code>$match<\/code>), gom nh\u00f3m (<code>$group<\/code>), t\u00ednh to\u00e1n (<code>$sum<\/code>, <code>$avg<\/code>), s\u1eafp x\u1ebfp (<code>$sort<\/code>)\u2026<\/td><td>L\u00e0 m\u00f4 h\u00ecnh x\u1eed l\u00fd d\u1eef li\u1ec7u truy\u1ec1n th\u1ed1ng g\u1ed3m hai h\u00e0m JavaScript: <code>map()<\/code> (d\u1eef li\u1ec7u key\u2013value) v\u00e0 <code>reduce()<\/code> (t\u1ed5ng h\u1ee3p k\u1ebft qu\u1ea3 theo key).<\/td><\/tr><tr><td><strong>C\u00e1ch ho\u1ea1t \u0111\u1ed9ng<\/strong><\/td><td>D\u1eef li\u1ec7u \u0111\u01b0\u1ee3c chuy\u1ec3n qua t\u1eebng stage theo th\u1ee9 t\u1ef1 \u2192 k\u1ebft qu\u1ea3 c\u1ee7a stage n\u00e0y l\u00e0 \u0111\u1ea7u v\u00e0o c\u1ee7a stage sau.<\/td><td>MongoDB s\u1ebd ch\u1ea1y c\u00e1c h\u00e0m JavaScript tr\u00ean m\u1ed7i document \u2192 thu k\u1ebft qu\u1ea3 t\u1eeb h\u00e0m <code>map()<\/code>\u2192 t\u1ed5ng h\u1ee3p l\u1ea1i b\u1eb1ng <code>reduce()<\/code>.<\/td><\/tr><tr><td><strong>Ng\u00f4n ng\u1eef &amp; c\u00fa ph\u00e1p<\/strong><\/td><td>D\u00f9ng c\u00fa ph\u00e1p native MongoDB, g\u1ea7n v\u1edbi JSON n\u00ean d\u1ec5 \u0111\u1ecdc v\u00e0 d\u1ec5 vi\u1ebft.<\/td><td>D\u00f9ng JavaScript functions, ph\u1ea3i vi\u1ebft nhi\u1ec1u code th\u1ee7 c\u00f4ng h\u01a1n.<\/td><\/tr><tr><td><strong>Hi\u1ec7u n\u0103ng x\u1eed l\u00fd<\/strong><\/td><td>R\u1ea5t nhanh v\u00ec \u0111\u01b0\u1ee3c ch\u1ea1y trong engine C++ c\u1ee7a MongoDB, t\u1eadn d\u1ee5ng index v\u00e0 t\u1ed1i \u01b0u b\u1ed9 nh\u1edb.<\/td><td>Ch\u1eadm h\u01a1n v\u00ec ch\u1ea1y qua JavaScript interpreter, t\u1ed1n t\u00e0i nguy\u00ean CPU v\u00e0 RAM.<\/td><\/tr><tr><td><strong>Kh\u1ea3 n\u0103ng song song h\u00f3a (Parallelism)<\/strong><\/td><td>\u0110\u01b0\u1ee3c MongoDB t\u1ed1i \u01b0u t\u1ef1 \u0111\u1ed9ng n\u00ean c\u00e1c stages c\u00f3 th\u1ec3 ch\u1ea1y song song n\u1ed9i b\u1ed9.<\/td><td>H\u1ed7 tr\u1ee3 song song h\u00f3a nh\u01b0ng \u00edt t\u1ed1i \u01b0u, ph\u1ee5 thu\u1ed9c v\u00e0o script ng\u01b0\u1eddi vi\u1ebft.<\/td><\/tr><tr><td><strong>Kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng<\/strong><\/td><td>R\u1ea5t t\u1ed1t, c\u00f3 th\u1ec3 k\u1ebft h\u1ee3p <code>$merge<\/code>, <code>$out<\/code> \u0111\u1ec3 x\u1eed l\u00fd batch l\u1edbn ho\u1eb7c xu\u1ea5t k\u1ebft qu\u1ea3 ra collection.<\/td><td>C\u00f3 th\u1ec3 x\u1eed l\u00fd d\u1eef li\u1ec7u l\u1edbn nh\u01b0ng hi\u1ec7u n\u0103ng k\u00e9m v\u00e0 kh\u00f4ng t\u1ed1i \u01b0u khi m\u1edf r\u1ed9ng.<\/td><\/tr><tr><td><strong>T\u00ednh linh ho\u1ea1t<\/strong><\/td><td>\u0110\u1ee7 m\u1ea1nh cho h\u1ea7u h\u1ebft c\u00e1c b\u00e0i to\u00e1n t\u1ed5ng h\u1ee3p, v\u00e0 c\u00f2n h\u1ed7 tr\u1ee3 vi\u1ebft logic JS qua <code>$function<\/code> ho\u1eb7c <code>$accumulator<\/code>.<\/td><td>Linh ho\u1ea1t cao v\u00ec cho ph\u00e9p vi\u1ebft logic JavaScript t\u00f9y \u00fd, nh\u01b0ng ph\u1ea3i \u0111\u00e1nh \u0111\u1ed5i hi\u1ec7u n\u0103ng.<\/td><\/tr><tr><td><strong>\u1ee8ng d\u1ee5ng ph\u1ed5 bi\u1ebfn<\/strong><\/td><td>B\u00e1o c\u00e1o, dashboard, ph\u00e2n t\u00edch d\u1eef li\u1ec7u real-time, t\u00ednh to\u00e1n th\u1ed1ng k\u00ea, ETL nh\u1eb9.<\/td><td>X\u1eed l\u00fd d\u1eef li\u1ec7u ph\u1ee9c t\u1ea1p ho\u1eb7c y\u00eau c\u1ea7u thao t\u00e1c \u0111\u1eb7c bi\u1ec7t b\u1eb1ng JavaScript (tr\u01b0\u1edbc khi Aggregation ra \u0111\u1eddi).<\/td><\/tr><tr><td><strong>Gi\u1edbi h\u1ea1n &amp; h\u1ea1n ch\u1ebf<\/strong><\/td><td>Gi\u1edbi h\u1ea1n document 16MB, 1000 stages, 100MB b\u1ed9 nh\u1edb (c\u00f3 th\u1ec3 t\u0103ng b\u1eb1ng <code>allowDiskUse: true<\/code>).<\/td><td>R\u1ea5t t\u1ed1n t\u00e0i nguy\u00ean, kh\u00f4ng ph\u00f9 h\u1ee3p cho production l\u1edbn, d\u1ec5 l\u00e0m ch\u1eadm h\u1ec7 th\u1ed1ng.<\/td><\/tr><tr><td><strong>Kh\u1ea3 n\u0103ng b\u1ea3o tr\u00ec &amp; debug<\/strong><\/td><td>C\u1ea5u tr\u00fac d\u1ea1ng pipeline d\u1ec5 \u0111\u1ecdc, d\u1ec5 b\u1ea3o tr\u00ec, d\u1ec5 test t\u1eebng giai \u0111o\u1ea1n.<\/td><td>Code JavaScript kh\u00f3 debug v\u00e0 b\u1ea3o tr\u00ec h\u01a1n, \u0111\u1eb7c bi\u1ec7t khi logic ph\u1ee9c t\u1ea1p.<\/td><\/tr><tr><td><strong>Tr\u1ea1ng th\u00e1i hi\u1ec7n t\u1ea1i<\/strong><\/td><td>L\u00e0 chu\u1ea9n hi\u1ec7n nay, \u0111\u01b0\u1ee3c MongoDB li\u00ean t\u1ee5c c\u1ea3i ti\u1ebfn v\u00e0 khuy\u1ebfn ngh\u1ecb s\u1eed d\u1ee5ng.<\/td><td>\u0110\u00e3 b\u1ecb deprecated (kh\u00f4ng khuy\u1ebfn kh\u00edch d\u00f9ng) t\u1eeb MongoDB 5.0, b\u1ecb remove ho\u00e0n to\u00e0n t\u1eeb MongoDB 6.0+.<\/td><\/tr><tr><td><strong>T\u1ed5ng k\u1ebft<\/strong><\/td><td>Nhanh h\u01a1n, nh\u1eb9 h\u01a1n, d\u1ec5 d\u00f9ng v\u00e0 an to\u00e0n cho production.<\/td><td>Linh ho\u1ea1t h\u01a1n nh\u01b0ng ch\u1eadm, ph\u1ee9c t\u1ea1p v\u00e0 \u0111\u00e3 l\u1ed7i th\u1eddi.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-hay-trinh-bay-cac-lo\u1ea1i-index-nang-cao-trong-mongodb-compound-multikey-ttl-text-geospatial-va-nh\u1eefng-tr\u01b0\u1eddng-h\u1ee3p-s\u1eed-d\u1ee5ng\"><strong>H\u00e3y tr\u00ecnh b\u00e0y c\u00e1c lo\u1ea1i Index n\u00e2ng cao trong MongoDB (Compound, Multikey, TTL, Text, Geospatial) v\u00e0 nh\u1eefng tr\u01b0\u1eddng h\u1ee3p s\u1eed d\u1ee5ng<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Lo\u1ea1i Index<\/strong><\/td><td><strong>Ch\u1ee9c n\u0103ng<\/strong><\/td><td><strong>\u1ee8ng d\u1ee5ng<\/strong><\/td><td><strong>V\u00ed d\u1ee5 minh h\u1ecda<\/strong><\/td><\/tr><tr><td><strong>Compound Index<\/strong><\/td><td>K\u1ebft h\u1ee3p nhi\u1ec1u tr\u01b0\u1eddng (fields) trong m\u1ed9t index duy nh\u1ea5t.<\/td><td>Khi truy v\u1ea5n th\u01b0\u1eddng xuy\u00ean s\u1eed d\u1ee5ng nhi\u1ec1u \u0111i\u1ec1u ki\u1ec7n tr\u00ean nhi\u1ec1u tr\u01b0\u1eddng. Gi\u00fap MongoDB d\u00f9ng m\u1ed9t index thay v\u00ec ph\u1ea3i t\u1ea1o nhi\u1ec1u index ri\u00eang.<\/td><td><code>js db.users.createIndex({ age: 1, city: 1 })<\/code>&nbsp;<br>\u2192 T\u1ed1i \u01b0u cho truy v\u1ea5n: <code>db.users.find({ age: 25, city: \"Hanoi\" })<\/code><\/td><\/tr><tr><td><strong>Multikey Index<\/strong><\/td><td>T\u1ef1 \u0111\u1ed9ng \u0111\u01b0\u1ee3c t\u1ea1o khi index h\u00f3a m\u1ea3ng (array). M\u1ed7i ph\u1ea7n t\u1eed trong m\u1ea3ng \u0111\u01b0\u1ee3c index ri\u00eang bi\u1ec7t.<\/td><td>Khi field ch\u1ee9a danh s\u00e1ch gi\u00e1 tr\u1ecb nh\u01b0 tags, categories, skills,\u2026<\/td><td><code>js db.products.createIndex({ tags: 1 })&nbsp;<\/code><br>\u2192 Gi\u00fap t\u00ecm nhanh c\u00e1c s\u1ea3n ph\u1ea9m c\u00f3 <code>tags<\/code> ch\u1ee9a \u201celectronics\u201d.<\/td><\/tr><tr><td><strong>TTL Index (Time To Live)<\/strong><\/td><td>T\u1ef1 \u0111\u1ed9ng x\u00f3a document sau m\u1ed9t th\u1eddi gian nh\u1ea5t \u0111\u1ecbnh.<\/td><td>Khi l\u01b0u d\u1eef li\u1ec7u t\u1ea1m th\u1eddi nh\u01b0 logs, session, token, cache,\u2026<\/td><td><code>js db.sessions.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 })<\/code>&nbsp;<br>\u2192 Document s\u1ebd b\u1ecb x\u00f3a sau 1 gi\u1edd.<br>Background task ch\u1ea1y m\u1ed7i 60 gi\u00e2y \u0111\u1ec3 cleanup.<\/td><\/tr><tr><td><strong>Text Index<\/strong><\/td><td>Cho ph\u00e9p t\u00ecm ki\u1ebfm to\u00e0n v\u0103n (full-text search)trong c\u00e1c chu\u1ed7i v\u0103n b\u1ea3n.<\/td><td>Khi c\u1ea7n t\u00ecm ki\u1ebfm n\u1ed9i dung theo t\u1eeb kh\u00f3a trong c\u00e1c tr\u01b0\u1eddng v\u0103n b\u1ea3n. H\u1ed7 tr\u1ee3 stemming, stop words, scoring.<\/td><td><code>js db.articles.createIndex({ content: \"text\", title: \"text\" })<\/code>&nbsp;<br>\u2192 D\u00f9ng cho: <code>db.articles.find({ $text: { $search: \"mongodb index\" } })<\/code><\/td><\/tr><tr><td><strong>Geospatial Index<\/strong><\/td><td>H\u1ed7 tr\u1ee3 truy v\u1ea5n v\u1ecb tr\u00ed \u0111\u1ecba l\u00fd (location-based queries).<\/td><td>Khi \u1ee9ng d\u1ee5ng li\u00ean quan \u0111\u1ebfn b\u1ea3n \u0111\u1ed3, kho\u1ea3ng c\u00e1ch, v\u1ecb tr\u00ed, nh\u01b0 t\u00ecm qu\u00e1n \u0103n g\u1ea7n nh\u1ea5t. Hai lo\u1ea1i: 2d (flat), 2dsphere (h\u00ecnh c\u1ea7u &#8211; khuy\u1ebfn kh\u00edch).<\/td><td><code>js db.places.createIndex({ location: \"2dsphere\" }) <\/code><br>\u2192 T\u00ecm \u0111i\u1ec3m g\u1ea7n v\u1ecb tr\u00ed ng\u01b0\u1eddi d\u00f9ng: <code>$near<\/code>, <code>$geoWithin<\/code>\u2026<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-gi\u1ea3-s\u1eed-h\u1ec7-th\u1ed1ng-mongodb-c\u1ee7a-b\u1ea1n-b\u1eaft-d\u1ea7u-x\u1eed-ly-ch\u1eadm-khi-d\u1eef-li\u1ec7u-tang-len-b\u1ea1n-s\u1ebd-lam-gi-d\u1ec3-t\u1ed1i-\u01b0u-hi\u1ec7u-nang-truy-v\u1ea5n\"><strong>Gi\u1ea3 s\u1eed h\u1ec7 th\u1ed1ng MongoDB c\u1ee7a b\u1ea1n b\u1eaft \u0111\u1ea7u x\u1eed l\u00fd ch\u1eadm khi d\u1eef li\u1ec7u t\u0103ng l\u00ean, b\u1ea1n s\u1ebd l\u00e0m g\u00ec \u0111\u1ec3 t\u1ed1i \u01b0u hi\u1ec7u n\u0103ng truy v\u1ea5n?<\/strong><\/h3>\n\n\n\n<p>\u0110\u1ec3 t\u1ed1i \u01b0u h\u00f3a truy v\u1ea5n trong MongoDB, ta s\u1ebd \u0111i theo h\u01b0\u1edbng ph\u00e2n t\u00edch &#8211; t\u1ed1i \u01b0u &#8211; ki\u1ec3m ch\u1ee9ng.<\/p>\n\n\n\n<p>C\u1ee5 th\u1ec3, c\u00f3 m\u1ed9t s\u1ed1 k\u1ef9 thu\u1eadt quan tr\u1ecdng sau:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>T\u1ea1o v\u00e0 s\u1eed d\u1ee5ng Index h\u1ee3p l\u00fd<\/strong><\/li>\n<\/ul>\n\n\n\n<p>Index l\u00e0 c\u00f4ng c\u1ee5 quan tr\u1ecdng nh\u1ea5t gi\u00fap MongoDB truy c\u1eadp d\u1eef li\u1ec7u nhanh h\u01a1n. N\u1ebfu kh\u00f4ng c\u00f3 index, h\u1ec7 th\u1ed1ng s\u1ebd ph\u1ea3i qu\u00e9t to\u00e0n b\u1ed9 collection (<code>COLLSCAN<\/code>), g\u00e2y t\u1ed1n th\u1eddi gian v\u00e0 t\u00e0i nguy\u00ean. M\u1ed9t s\u1ed1 ph\u01b0\u01a1ng ph\u00e1p t\u1ed1i \u01b0u index l\u00e0:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>T\u1ea1o index tr\u00ean c\u00e1c tr\u01b0\u1eddng th\u01b0\u1eddng \u0111\u01b0\u1ee3c d\u00f9ng trong \u0111i\u1ec1u ki\u1ec7n l\u1ecdc (<code>find<\/code>), s\u1eafp x\u1ebfp (<code>sort<\/code>), ho\u1eb7c join (<code>$lookup<\/code>).<\/li>\n\n\n\n<li>D\u00f9ng compound index khi truy v\u1ea5n nhi\u1ec1u tr\u01b0\u1eddng c\u00f9ng l\u00fac.<\/li>\n\n\n\n<li>Tr\u00e1nh t\u1ea1o qu\u00e1 nhi\u1ec1u index (m\u1ed7i index t\u1ed1n RAM v\u00e0 l\u00e0m ch\u1eadm thao t\u00e1c ghi).<\/li>\n\n\n\n<li>X\u00f3a c\u00e1c indexes kh\u00f4ng s\u1eed d\u1ee5ng b\u1eb1ng c\u00e1ch theo d\u00f5i m\u1ee9c s\u1eed d\u1ee5ng v\u1edbi $indexStats.<\/li>\n\n\n\n<li>Ki\u1ec3m tra truy v\u1ea5n c\u00f3 d\u00f9ng index kh\u00f4ng b\u1eb1ng l\u1ec7nh:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({ age: 25, city: \"Hanoi\" }).explain(\"executionStats\")<\/code><\/pre>\n\n\n\n<p>=&gt; N\u1ebfu k\u1ebft qu\u1ea3 hi\u1ec3n th\u1ecb <code>IXSCAN<\/code>, ngh\u0129a l\u00e0 truy v\u1ea5n \u0111\u00e3 s\u1eed d\u1ee5ng index th\u00e0nh c\u00f4ng.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Gi\u1edbi h\u1ea1n d\u1eef li\u1ec7u tr\u1ea3 v\u1ec1 (Projection)<\/strong><\/li>\n<\/ul>\n\n\n\n<p>MongoDB cho ph\u00e9p ch\u1ec9 \u0111\u1ecbnh r\u00f5 nh\u1eefng tr\u01b0\u1eddng c\u1ea7n l\u1ea5y, gi\u00fap gi\u1ea3m k\u00edch th\u01b0\u1edbc d\u1eef li\u1ec7u v\u00e0 th\u1eddi gian x\u1eed l\u00fd. V\u00ed d\u1ee5:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({}, { name: 1, email: 1 })<\/code><\/pre>\n\n\n\n<p>=&gt; Thay v\u00ec l\u1ea5y to\u00e0n b\u1ed9 document, ch\u1ec9 hai tr\u01b0\u1eddng name v\u00e0 email \u0111\u01b0\u1ee3c tr\u1ea3 v\u1ec1, ti\u1ebft ki\u1ec7m b\u0103ng th\u00f4ng v\u00e0 b\u1ed9 nh\u1edb RAM.<\/p>\n\n\n\n<p>L\u01b0u \u00fd: S\u1eed d\u1ee5ng _id: 0 \u0111\u1ec3 lo\u1ea1i b\u1ecf _id n\u1ebfu kh\u00f4ng c\u1ea7n, v\u00ec _id lu\u00f4n \u0111\u01b0\u1ee3c tr\u1ea3 v\u1ec1 m\u1eb7c \u0111\u1ecbnh.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Vi\u1ebft Aggregation Pipeline hi\u1ec7u qu\u1ea3<\/strong><\/li>\n<\/ul>\n\n\n\n<p>Aggregation l\u00e0 c\u00f4ng c\u1ee5 m\u1ea1nh \u0111\u1ec3 x\u1eed l\u00fd v\u00e0 t\u1ed5ng h\u1ee3p d\u1eef li\u1ec7u, nh\u01b0ng n\u1ebfu vi\u1ebft sai c\u00e1ch s\u1ebd l\u00e0m ch\u1eadm hi\u1ec7u n\u0103ng. M\u1ed9t s\u1ed1 m\u1eb9o t\u1ed1i \u01b0u aggregation g\u1ed3m:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u0110\u1eb7t <code>$match<\/code> v\u00e0 <code>$project<\/code> \u1edf \u0111\u1ea7u pipeline \u0111\u1ec3 l\u1ecdc v\u00e0 gi\u1edbi h\u1ea1n d\u1eef li\u1ec7u s\u1edbm.<\/li>\n\n\n\n<li>Tr\u00e1nh <code>$unwind<\/code> tr\u00ean c\u00e1c m\u1ea3ng l\u1edbn n\u1ebfu kh\u00f4ng c\u1ea7n thi\u1ebft.<\/li>\n\n\n\n<li>S\u1eed d\u1ee5ng t\u00f9y ch\u1ecdn <code>allowDiskUse: true<\/code> \u0111\u1ec3 cho ph\u00e9p MongoDB ghi t\u1ea1m ra \u0111\u0129a khi d\u1eef li\u1ec7u qu\u00e1 l\u1edbn. v\u01b0\u1ee3t qu\u00e1 100MB per stage.<\/li>\n\n\n\n<li>S\u1eed d\u1ee5ng $limit sau $sort \u0111\u1ec3 gi\u1ea3m s\u1ed1 documents c\u1ea7n sort.<\/li>\n\n\n\n<li>T\u1eadn d\u1ee5ng indexes cho $match v\u00e0 $sort \u1edf \u0111\u1ea7u pipeline.<\/li>\n\n\n\n<li>Tr\u00e1nh $lookup trong v\u00f2ng l\u1eb7p l\u1edbn &#8211; c\u00e2n nh\u1eafc denormalization.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-mongodb-atlas-la-gi-va-no-khac-gi-so-v\u1edbi-mongodb-t\u1ef1-tri\u1ec3n-khai\"><strong>MongoDB Atlas l\u00e0 g\u00ec v\u00e0 n\u00f3 kh\u00e1c g\u00ec so v\u1edbi MongoDB t\u1ef1 tri\u1ec3n khai?<\/strong><\/h3>\n\n\n\n<p>MongoDB Atlas l\u00e0 n\u1ec1n t\u1ea3ng Database-as-a-Service (DBaaS) do ch\u00ednh MongoDB Inc. cung c\u1ea5p, cho ph\u00e9p ng\u01b0\u1eddi d\u00f9ng tri\u1ec3n khai, qu\u1ea3n l\u00fd v\u00e0 m\u1edf r\u1ed9ng c\u01a1 s\u1edf d\u1eef li\u1ec7u MongoDB tr\u1ef1c ti\u1ebfp tr\u00ean n\u1ec1n t\u1ea3ng \u0111\u00e1m m\u00e2y m\u00e0 kh\u00f4ng c\u1ea7n c\u00e0i \u0111\u1eb7t th\u1ee7 c\u00f4ng. Atlas c\u00f3 th\u1ec3 ch\u1ea1y tr\u00ean nhi\u1ec1u n\u1ec1n t\u1ea3ng \u0111\u00e1m m\u00e2y ph\u1ed5 bi\u1ebfn nh\u01b0:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>AWS (Amazon Web Services)<\/li>\n\n\n\n<li>Google Cloud Platform (GCP)<\/li>\n\n\n\n<li>Microsoft Azure<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-di\u1ec3m-khac-bi\u1ec7t-gi\u1eefa-mongodb-atlas-va-mongodb-t\u1ef1-tri\u1ec3n-khai-self-hosted-mongodb\"><strong>\u0110i\u1ec3m kh\u00e1c bi\u1ec7t gi\u1eefa MongoDB Atlas v\u00e0 MongoDB t\u1ef1 tri\u1ec3n khai (Self-Hosted MongoDB)<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Ti\u00eau ch\u00ed<\/strong><\/td><td><strong>MongoDB Atlas (Cloud)<\/strong><\/td><td><strong>Self-Hosted MongoDB<\/strong><\/td><\/tr><tr><td>C\u00e1ch tri\u1ec3n khai<\/td><td>T\u1ef1 \u0111\u1ed9ng tri\u1ec3n khai ch\u1ec9 v\u1edbi v\u00e0i c\u00fa click tr\u00ean giao di\u1ec7n web. Kh\u00f4ng c\u1ea7n c\u1ea5u h\u00ecnh m\u00e1y ch\u1ee7.<\/td><td>C\u1ea7n c\u00e0i \u0111\u1eb7t th\u1ee7 c\u00f4ng tr\u00ean m\u00e1y ch\u1ee7 (local ho\u1eb7c server ri\u00eang).<\/td><\/tr><tr><td>Qu\u1ea3n l\u00fd h\u1ec7 th\u1ed1ng<\/td><td>\u0110\u01b0\u1ee3c MongoDB Atlas qu\u1ea3n l\u00fd to\u00e0n b\u1ed9 (backup, c\u1eadp nh\u1eadt, m\u1edf r\u1ed9ng, b\u1ea3o m\u1eadt), OS patching, n\u00e2ng c\u1ea5p phi\u00ean b\u1ea3n MongoDB.<\/td><td>Ng\u01b0\u1eddi d\u00f9ng t\u1ef1 ch\u1ecbu tr\u00e1ch nhi\u1ec7m qu\u1ea3n tr\u1ecb, sao l\u01b0u, b\u1ea3o m\u1eadt v\u00e0 n\u00e2ng c\u1ea5p.<\/td><\/tr><tr><td>B\u1ea3o m\u1eadt<\/td><td>T\u00edch h\u1ee3p s\u1eb5n SSL, IP Whitelisting, v\u00e0 qu\u1ea3n l\u00fd truy c\u1eadp n\u00e2ng cao (IAM), encryption at rest, LDAP\/Kerberos authentication.<\/td><td>Ph\u1ea3i t\u1ef1 c\u1ea5u h\u00ecnh b\u1ea3o m\u1eadt, t\u01b0\u1eddng l\u1eeda, x\u00e1c th\u1ef1c v\u00e0 quy\u1ec1n truy c\u1eadp.<\/td><\/tr><tr><td>Kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng (Scalability)<\/td><td>T\u1ef1 \u0111\u1ed9ng scale up\/down ch\u1ec9 b\u1eb1ng v\u00e0i thao t\u00e1c, auto-scaling clusters, tri\u1ec3n khai \u0111a khu v\u1ef1c.<\/td><td>Ph\u1ea3i c\u1ea5u h\u00ecnh th\u1ee7 c\u00f4ng khi c\u1ea7n m\u1edf r\u1ed9ng h\u1ec7 th\u1ed1ng.<\/td><\/tr><tr><td>Chi ph\u00ed<\/td><td>T\u00ednh ph\u00ed d\u1ef1a tr\u00ean t\u00e0i nguy\u00ean v\u00e0 m\u1ee9c s\u1eed d\u1ee5ng (Pay-as-you-go). C\u00f3 g\u00f3i mi\u1ec5n ph\u00ed \u0111\u1ec3 th\u1eed nghi\u1ec7m.<\/td><td>Chi ph\u00ed th\u1ea5p h\u01a1n n\u1ebfu t\u1ef1 qu\u1ea3n l\u00fd, nh\u01b0ng t\u1ed1n c\u00f4ng v\u1eadn h\u00e0nh v\u00e0 b\u1ea3o tr\u00ec.<\/td><\/tr><tr><td>Gi\u00e1m s\u00e1t &amp; hi\u1ec7u n\u0103ng<\/td><td>C\u00f3 s\u1eb5n c\u00f4ng c\u1ee5 Atlas Dashboard \u0111\u1ec3 gi\u00e1m s\u00e1t hi\u1ec7u n\u0103ng, c\u1ea3nh b\u00e1o l\u1ed7i, v\u00e0 ph\u00e2n t\u00edch truy v\u1ea5n.<\/td><td>Ph\u1ea3i c\u00e0i th\u00eam c\u00f4ng c\u1ee5 nh\u01b0 mongostat, mongotop, ho\u1eb7c gi\u1ea3i ph\u00e1p monitoring ri\u00eang.<\/td><\/tr><tr><td>T\u00edch h\u1ee3p &amp; Automation<\/td><td>H\u1ed7 tr\u1ee3 s\u1eb5n ch\u1ee9c n\u0103ng backup t\u1ef1 \u0111\u1ed9ng, global cluster, trigger (serverless functions).<\/td><td>Ph\u1ea3i t\u1ef1 vi\u1ebft script ho\u1eb7c c\u00e0i th\u00eam c\u00f4ng c\u1ee5 \u0111\u1ec3 t\u1ef1 \u0111\u1ed9ng h\u00f3a.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-khi-nao-nen-dung-t\u1eebng-lo\u1ea1i\"><strong>Khi n\u00e0o n\u00ean d\u00f9ng t\u1eebng lo\u1ea1i?<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>T\u00ecnh hu\u1ed1ng<\/strong><\/td><td><strong>Gi\u1ea3i ph\u00e1p ph\u00f9 h\u1ee3p<\/strong><\/td><\/tr><tr><td>D\u1ef1 \u00e1n nh\u1ecf, demo, h\u1ecdc t\u1eadp ho\u1eb7c startup c\u1ea7n ch\u1ea1y nhanh, kh\u00f4ng t\u1ed1n th\u1eddi gian setup<\/td><td>MongoDB Atlas (d\u00f9ng free tier M0)<\/td><\/tr><tr><td>Doanh nghi\u1ec7p mu\u1ed1n m\u00f4i tr\u01b0\u1eddng production \u1ed5n \u0111\u1ecbnh, c\u00f3 backup v\u00e0 b\u1ea3o m\u1eadt cao, t\u1eadp trung v\u00e0o business logic thay v\u00ec infrastructure<\/td><td>MongoDB Atlas<\/td><\/tr><tr><td>D\u1ef1 \u00e1n n\u1ed9i b\u1ed9, y\u00eau c\u1ea7u ch\u1ea1y offline ho\u1eb7c c\u00f3 h\u1ea1 t\u1ea7ng ri\u00eang (on-premises), c\u00f3 y\u00eau c\u1ea7u tu\u00e2n th\u1ee7 v\u1ec1 v\u1ecb tr\u00ed l\u01b0u tr\u1eef d\u1eef li\u1ec7u (data residency compliance)<\/td><td>Self-Hosted MongoDB<\/td><\/tr><tr><td>Khi mu\u1ed1n to\u00e0n quy\u1ec1n ki\u1ec3m so\u00e1t c\u1ea5u h\u00ecnh v\u00e0 chi ph\u00ed ph\u1ea7n c\u1ee9ng, c\u00f3 team DevOps m\u1ea1nh<\/td><td>Self-Hosted MongoDB<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-mongodb-query-based-interview-questions-bai-t\u1eadp-th\u1ef1c-hanh\"><span class=\"ez-toc-section\" id=\"MongoDB_Query-Based_Interview_Questions_%E2%80%93_Bai_tap_thuc_hanh\"><\/span><strong>MongoDB Query-Based Interview Questions &#8211; B\u00e0i t\u1eadp th\u1ef1c h\u00e0nh<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Gi\u1ea3 s\u1eed ta c\u00f3 5 collection: <code>users<\/code>, <code>products<\/code>, <code>orders<\/code>, <code>articles<\/code>, <code>places<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ users\n&#91;\n  { _id: 1, name: \"An\",   email: \"an@ex.com\",   age: 25, city: \"Hanoi\",   skills: &#91;\"node\", \"mongo\"], createdAt: ISODate(\"2025-01-01\") },\n  { _id: 2, name: \"Binh\", email: \"binh@ex.com\", age: 32, city: \"Hanoi\",   skills: &#91;\"python\"],        createdAt: ISODate(\"2025-01-10\") },\n  { _id: 3, name: \"Cuong\",email: \"cuong@ex.com\",age: 22, city: \"Da Nang\", skills: &#91;\"node\"],           createdAt: ISODate(\"2025-02-01\") }\n]\n\n\/\/ products\n&#91;\n  { _id: 101, name: \"Laptop A\", price: 1200, category: \"electronics\", tags: &#91;\"laptop\",\"computer\"], inStock: true },\n  { _id: 102, name: \"Phone B\",  price: 800,  category: \"electronics\", tags: &#91;\"phone\",\"mobile\"],     inStock: true },\n  { _id: 103, name: \"Shirt C\",  price: 40,   category: \"fashion\",     tags: &#91;\"shirt\",\"men\"],        inStock: false }\n]\n\n\/\/ orders\n&#91;\n  { _id: 10001, userId: 1, status: \"completed\", items: &#91;{ productId: 101, qty: 1 }], total: 1200, orderedAt: ISODate(\"2025-03-01\") },\n  { _id: 10002, userId: 1, status: \"pending\",   items: &#91;{ productId: 102, qty: 2 }], total: 1600, orderedAt: ISODate(\"2025-03-05\") },\n  { _id: 10003, userId: 2, status: \"completed\", items: &#91;{ productId: 103, qty: 3 }], total: 120,  orderedAt: ISODate(\"2025-03-10\") }\n]\n\n\/\/ articles\n&#91;\n  { _id: \"a1\", title: \"MongoDB Indexing Best Practices\", content: \"Compound, multikey, TTL...\", tags: &#91;\"mongodb\",\"index\"] },\n  { _id: \"a2\", title: \"Text Search in MongoDB\",          content: \"How to use $text ...\",      tags: &#91;\"mongodb\",\"text\"]  }\n]\n\n\/\/ places (GeoJSON)\n&#91;\n  { _id: \"p1\", name: \"Pho 24\",   category: \"food\",    location: { type: \"Point\", coordinates: &#91;106.700, 10.776] } },\n  { _id: \"p2\", name: \"Cafe ABC\", category: \"coffee\",  location: { type: \"Point\", coordinates: &#91;106.702, 10.778] } },\n  { _id: \"p3\", name: \"Park XYZ\", category: \"park\",    location: { type: \"Point\", coordinates: &#91;106.710, 10.770] } }\n]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-crud-c\u01a1-b\u1ea3n-projection-sort\"><strong>1. CRUD c\u01a1 b\u1ea3n + Projection + Sort<\/strong><\/h3>\n\n\n\n<p><strong>Y\u00eau c\u1ea7u:<\/strong> L\u1ea5y danh s\u00e1ch ng\u01b0\u1eddi d\u00f9ng \u1edf <em>Hanoi<\/em>, ch\u1ec9 tr\u1ea3 v\u1ec1 name v\u00e0 email, s\u1eafp x\u1ebfp name t\u0103ng d\u1ea7n, gi\u1edbi h\u1ea1n 2 k\u1ebft qu\u1ea3.<\/p>\n\n\n\n<p><strong>=&gt; Query<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find(\n  { city: \"Hanoi\" },\n  { _id: 0, name: 1, email: 1 }\n).sort({ name: 1 }).limit(2)<\/code><\/pre>\n\n\n\n<p>Trong \u0111\u00f3:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>D\u00f9ng <code>projection<\/code> \u0111\u1ec3 gi\u1ea3m d\u1eef li\u1ec7u tr\u1ea3 v\u1ec1.<\/li>\n\n\n\n<li><code>sort<\/code> + <code>limit<\/code> gi\u00fap hi\u1ec3n th\u1ecb g\u1ecdn v\u00e0 nhanh (ph\u00f9 h\u1ee3p UI ph\u00e2n trang).<\/li>\n\n\n\n<li>_id: 0 lo\u1ea1i b\u1ecf field _id kh\u1ecfi k\u1ebft qu\u1ea3.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-truy-v\u1ea5n-m\u1ea3ng-multikey-amp-toan-t\u1eed-so-kh\u1edbp\"><strong>2. Truy v\u1ea5n m\u1ea3ng (Multikey) &amp; To\u00e1n t\u1eed so kh\u1edbp<\/strong><\/h3>\n\n\n\n<p><strong>Y\u00eau c\u1ea7u:<\/strong> T\u00ecm user c\u00f3 skill \u201cnode\u201d.<\/p>\n\n\n\n<p>=&gt; Query:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({ skills: \"node\" })<\/code><\/pre>\n\n\n\n<p>Trong \u0111\u00f3:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Field <code>skills<\/code> l\u00e0 m\u1ea3ng \u2192 multikey index (n\u1ebfu t\u1ea1o) s\u1ebd index t\u1eebng ph\u1ea7n t\u1eed.<\/li>\n\n\n\n<li>So kh\u1edbp tr\u1ef1c ti\u1ebfp <code>\"node\"<\/code> l\u00e0 \u0111\u1ee7 (kh\u00f4ng c\u1ea7n <code>$in<\/code>).<\/li>\n<\/ul>\n\n\n\n<p><strong>=&gt; T\u1ed1i \u01b0u:<\/strong> T\u1ea1o index <code>db.users.createIndex({ skills: 1 })<\/code> n\u1ebfu truy v\u1ea5n n\u00e0y ph\u1ed5 bi\u1ebfn.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-update-co-di\u1ec1u-ki\u1ec7n-array-operators\"><strong>3. Update c\u00f3 \u0111i\u1ec1u ki\u1ec7n + Array Operators<\/strong><\/h3>\n\n\n\n<p><strong>Y\u00eau c\u1ea7u<\/strong>: <code>orders._id=10002<\/code> \u0111\u1ed5i <code>status<\/code> sang <code>\"completed\"<\/code> v\u00e0 t\u0103ng <code>qty<\/code> c\u1ee7a m\u1ecdi item th\u00eam 1<\/p>\n\n\n\n<p><strong>=&gt; Query<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.orders.updateOne(\n  { _id: 10002 },\n  {\n    $set: { status: \"completed\" },\n    $inc: { \"items.$&#91;].qty\": 1 }   \/\/ $&#91;] = all array elements\n  }\n)<\/code><\/pre>\n\n\n\n<p>Trong \u0111\u00f3:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$[]<\/code> \u00e1p d\u1ee5ng cho t\u1ea5t c\u1ea3 ph\u1ea7n t\u1eed m\u1ea3ng.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-4-aggregation-l\u1ecdc-s\u1edbm-gom-nhom-s\u1eafp-x\u1ebfp-top-k\"><strong>4. Aggregation: L\u1ecdc s\u1edbm \u2192 Gom nh\u00f3m \u2192 S\u1eafp x\u1ebfp \u2192 Top-K<\/strong><\/h3>\n\n\n\n<p><strong>Y\u00eau c\u1ea7u<\/strong>: T\u1ed5ng doanh thu (<code>total<\/code>) theo <code>userId<\/code> cho c\u00e1c \u0111\u01a1n <code>\"completed\"<\/code>, l\u1ea5y Top 2.<\/p>\n\n\n\n<p><strong>=&gt; Pipeline:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.orders.aggregate(&#91;\n  { $match: { status: \"completed\" } },               \/\/ l\u1ecdc s\u1edbm\n  { $group: { _id: \"$userId\", revenue: { $sum: \"$total\" } } },\n  { $sort: { revenue: -1 } },\n  { $limit: 2 }\n])<\/code><\/pre>\n\n\n\n<p>Trong \u0111\u00f3:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u0110\u1eb7t <code>$match<\/code> \u0111\u1ea7u pipeline \u0111\u1ec3 gi\u1ea3m d\u1eef li\u1ec7u \u0111i qua c\u00e1c stage sau (t\u1ed1i \u01b0u hi\u1ec7u n\u0103ng).<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-5-lookup-gi\u1ed1ng-join-unwind-project\"><strong>5. <\/strong>$lookup<strong> (gi\u1ed1ng JOIN) + <\/strong>$unwind<strong> + <\/strong>$project<\/h3>\n\n\n\n<p><strong>Y\u00eau c\u1ea7u<\/strong>: L\u1ea5y chi ti\u1ebft \u0111\u01a1n h\u00e0ng <code>10001<\/code> k\u00e8m t\u00ean s\u1ea3n ph\u1ea9m c\u1ee7a t\u1eebng item.<\/p>\n\n\n\n<p><strong>=&gt; Pipeline<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.orders.aggregate(&#91;\n  { $match: { _id: 10001 } },\n  { $unwind: \"$items\" },\n  { $lookup: {\n      from: \"products\",\n      localField: \"items.productId\",\n      foreignField: \"_id\",\n      as: \"prod\"\n  }},\n  { $unwind: \"$prod\" },\n  { $project: {\n      _id: 0,\n      orderId: \"$_id\",\n      productName: \"$prod.name\",\n      qty: \"$items.qty\",\n      lineTotal: { $multiply: &#91;\"$prod.price\", \"$items.qty\"] }\n  }}\n])<\/code><\/pre>\n\n\n\n<p>Trong \u0111\u00f3:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$lookup<\/code> \u201cn\u1ed1i\u201d d\u1eef li\u1ec7u gi\u1eefa collection.<\/li>\n\n\n\n<li><code>$unwind<\/code> bi\u1ebfn m\u1ed7i item th\u00e0nh 1 d\u00f2ng \u0111\u1ec3 t\u00ednh <code>lineTotal<\/code>.<\/li>\n\n\n\n<li><code>$project<\/code> gi\u00fap tr\u1ea3 v\u1ec1 \u0111\u00fang th\u00f4ng tin c\u1ea7n.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-6-text-search-score\"><strong>6. Text Search + Score<\/strong><\/h3>\n\n\n\n<p><strong>Y\u00eau c\u1ea7u:<\/strong> T\u00ecm b\u00e0i vi\u1ebft c\u00f3 ch\u1ee9a \u201cmongodb index\u201d, \u01b0u ti\u00ean score cao.<\/p>\n\n\n\n<p><strong>Chu\u1ea9n b\u1ecb index:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.articles.createIndex({ title: \"text\", content: \"text\" })<\/code><\/pre>\n\n\n\n<p><strong>Query:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.articles.find(\n  { $text: { $search: \"mongodb index\" } },\n  { score: { $meta: \"textScore\" }, title: 1 }\n).sort({ score: { $meta: \"textScore\" } })<\/code><\/pre>\n\n\n\n<p>Trong \u0111\u00f3:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Text index ch\u1ec9 c\u00f3 1 tr\u00ean m\u1ed7i collection (nh\u01b0ng g\u1ed3m nhi\u1ec1u field).<\/li>\n\n\n\n<li><code>$meta: \"textScore\"<\/code> \u0111\u1ec3 l\u1ea5y v\u00e0 s\u1eafp x\u1ebfp theo \u0111\u1ed9 ph\u00f9 h\u1ee3p.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-7-geospatial-tim-d\u1ecba-di\u1ec3m\"><strong>7. Geospatial: t\u00ecm \u0111\u1ecba \u0111i\u1ec3m<\/strong><\/h3>\n\n\n\n<p><strong>Y\u00eau c\u1ea7u:<\/strong> T\u00ecm qu\u00e1n trong b\u00e1n k\u00ednh 2km quanh t\u1ecda \u0111\u1ed9 <code>[106.700, 10.776]<\/code>.<\/p>\n\n\n\n<p><strong>Chu\u1ea9n b\u1ecb index:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.places.createIndex({ location: \"2dsphere\" })<\/code><\/pre>\n\n\n\n<p><strong>Query:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.places.find({\n  location: {\n    $near: {\n      $geometry: { type: \"Point\", coordinates: &#91;106.700, 10.776] },\n      $maxDistance: 2000\n    }\n  }\n})<\/code><\/pre>\n\n\n\n<p>Trong \u0111\u00f3:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>2dsphere<\/code> cho t\u1ecda \u0111\u1ed9 th\u1eadt (kinh \u0111\u1ed9\/v\u0129 \u0111\u1ed9) , h\u1ed7 tr\u1ee3 GeoJSON.<\/li>\n\n\n\n<li><code>$near<\/code> t\u1ef1 s\u1eafp x\u1ebfp theo \u0111\u1ed9 g\u1ea7n; r\u1ea5t h\u1eefu \u00edch cho app b\u1ea3n \u0111\u1ed3.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-8-index-explain-t\u1ed1i-\u01b0u-hi\u1ec7u-nang\"><strong>8. Index + <\/strong><strong>.explain()<\/strong><strong> (t\u1ed1i \u01b0u hi\u1ec7u n\u0103ng):<\/strong><\/h3>\n\n\n\n<p><strong>Y\u00eau c\u1ea7u:<\/strong> Truy v\u1ea5n s\u1ea3n ph\u1ea9m theo <code>category<\/code> + <code>price<\/code> t\u0103ng d\u1ea7n. T\u1ea1o index ph\u00f9 h\u1ee3p v\u00e0 ki\u1ec3m tra.<\/p>\n\n\n\n<p><strong>Index g\u1ee3i \u00fd:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.createIndex({ category: 1, price: 1 })<\/code><\/pre>\n\n\n\n<p><strong>Query:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find(\n  { category: \"electronics\" }\n).sort({ price: 1 }).explain(\"executionStats\")<\/code><\/pre>\n\n\n\n<p>Trong \u0111\u00f3:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Compound index (category\u2192price) \u201ccover\u201d c\u1ea3 l\u1ecdc v\u00e0 sort n\u00ean s\u1ebd tr\u00e1nh \u0111\u01b0\u1ee3c in-memory sort.<\/li>\n\n\n\n<li><code>explain()<\/code> cho bi\u1ebft c\u00f3 <code>IXSCAN<\/code> (t\u1ed1t) hay <code>COLLSCAN<\/code> (c\u1ea7n t\u1ed1i \u01b0u).<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-9-mini-transaction-2-c\u1eadp-nh\u1eadt-nh\u1ea5t-quan\"><strong>9. Mini-Transaction (2 c\u1eadp nh\u1eadt nh\u1ea5t qu\u00e1n)<\/strong><\/h3>\n\n\n\n<p><strong>Y\u00eau c\u1ea7u<\/strong>: Tr\u1eeb 100 t\u1eeb t\u00e0i kho\u1ea3n A, c\u1ed9ng 100 v\u00e0o t\u00e0i kho\u1ea3n B trong m\u1ed9t transaction.<\/p>\n\n\n\n<p><strong>=&gt; Query<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const session = db.getMongo().startSession();\nsession.startTransaction();\ntry {\n  const acc = session.getDatabase(\"shop\").collection(\"accounts\");\n  acc.updateOne({ _id: \"A\" }, { $inc: { balance: -100 } });\n  acc.updateOne({ _id: \"B\" }, { $inc: { balance:  100 } });\n  session.commitTransaction();\n} catch (e) {\n  session.abortTransaction();\n  throw e;\n} finally {\n  session.endSession();\n}<\/code><\/pre>\n\n\n\n<p>Trong \u0111\u00f3:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Transaction \u0111\u1ea3m b\u1ea3o atomicity: ho\u1eb7c c\u1ea3 hai c\u1eadp nh\u1eadt th\u00e0nh c\u00f4ng, ho\u1eb7c rollback.&nbsp;<\/li>\n\n\n\n<li>Ph\u1ea3i truy\u1ec1n (pass) \u0111\u1ed1i t\u01b0\u1ee3ng session v\u00e0o t\u1ea5t c\u1ea3 c\u00e1c thao t\u00e1c trong transaction.<\/li>\n<\/ul>\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>MongoDB kh\u00f4ng ch\u1ec9 l\u00e0 m\u1ed9t c\u01a1 s\u1edf d\u1eef li\u1ec7u NoSQL th\u00f4ng th\u01b0\u1eddng&nbsp; m\u00e0 l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd cho c\u00e1c \u1ee9ng d\u1ee5ng hi\u1ec7n \u0111\u1ea1i c\u1ea7n x\u1eed l\u00fd d\u1eef li\u1ec7u linh ho\u1ea1t, quy m\u00f4 l\u1edbn v\u00e0 t\u1ed1c \u0111\u1ed9 cao. Vi\u1ec7c chu\u1ea9n b\u1ecb th\u1eadt t\u1ed1t c\u00e1c l\u00fd thuy\u1ebft, th\u1ef1c h\u00e0nh c\u0169ng nh\u01b0 luy\u1ec7n t\u1eadp tr\u01b0\u1edbc c\u00e1c c\u00e2u h\u1ecfi s\u1ebd gi\u00fap b\u1ea1n tr\u1ea3 l\u1eddi t\u1ef1 tin trong m\u1ecdi v\u00f2ng ph\u1ecfng v\u1ea5n k\u1ef9 thu\u1eadt. ITviec hy v\u1ecdng b\u00e0i vi\u1ebft tr\u00ean \u0111\u00e3 cung c\u1ea5p cho b\u1ea1n nh\u1eefng ki\u1ebfn th\u1ee9c b\u1ed5 \u00edch gi\u00fap b\u1ea1n chu\u1ea9n b\u1ecb th\u1eadt t\u1ed1t cho c\u00e1c c\u01a1 h\u1ed9i ngh\u1ec1 nghi\u1ec7p nh\u00e9.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB \u0111\u00e3 tr\u1edf th\u00e0nh m\u1ed9t trong nh\u1eefng NoSQL Database \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng r\u1ed9ng r\u00e3i nh\u1ea5t hi\u1ec7n nay nh\u1edd kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng linh ho\u1ea1t, l\u01b0u tr\u1eef d\u1eef li\u1ec7u d\u1ea1ng t\u00e0i li\u1ec7u v\u00e0 hi\u1ec7u n\u0103ng cao. N\u1ebfu b\u1ea1n \u0111ang chu\u1ea9n b\u1ecb cho bu\u1ed5i ph\u1ecfng v\u1ea5n v\u1ecb tr\u00ed Backend Developer, Data Engineer, Database Administrator hay b\u1ea5t k\u1ef3 [&hellip;]<\/p>\n","protected":false},"author":233,"featured_media":93960,"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-93878","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>Top 20+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB th\u01b0\u1eddng g\u1eb7p - ITviec Blog<\/title>\n<meta name=\"description\" content=\"\u00d4n t\u1eadp top nh\u1eefng c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB th\u01b0\u1eddng g\u1eb7p k\u00e8m v\u00ed d\u1ee5 th\u1ef1c h\u00e0nh, gi\u00fap b\u1ea1n t\u1ef1 tin chinh ph\u1ee5c m\u1ecdi bu\u1ed5i ph\u1ecfng v\u1ea5n.\" \/>\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\/cau-hoi-phong-van-mongodb\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Top 20+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB th\u01b0\u1eddng g\u1eb7p\" \/>\n<meta property=\"og:description\" content=\"MongoDB \u0111\u00e3 tr\u1edf th\u00e0nh m\u1ed9t trong nh\u1eefng NoSQL Database \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng r\u1ed9ng r\u00e3i nh\u1ea5t hi\u1ec7n nay nh\u1edd kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng linh ho\u1ea1t, l\u01b0u tr\u1eef d\u1eef li\u1ec7u d\u1ea1ng t\u00e0i li\u1ec7u v\u00e0\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-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-12-30T15:36:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-12-30T15:36:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/12\/cau-hoi-phong-van-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=\"Nhat Anh\" \/>\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=\"Nhat Anh\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc\" \/>\n\t<meta name=\"twitter:data2\" content=\"33 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Top 20+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB th\u01b0\u1eddng g\u1eb7p - ITviec Blog","description":"\u00d4n t\u1eadp top nh\u1eefng c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB th\u01b0\u1eddng g\u1eb7p k\u00e8m v\u00ed d\u1ee5 th\u1ef1c h\u00e0nh, gi\u00fap b\u1ea1n t\u1ef1 tin chinh ph\u1ee5c m\u1ecdi bu\u1ed5i ph\u1ecfng v\u1ea5n.","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\/cau-hoi-phong-van-mongodb\/","og_locale":"vi_VN","og_type":"article","og_title":"Top 20+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB th\u01b0\u1eddng g\u1eb7p","og_description":"MongoDB \u0111\u00e3 tr\u1edf th\u00e0nh m\u1ed9t trong nh\u1eefng NoSQL Database \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng r\u1ed9ng r\u00e3i nh\u1ea5t hi\u1ec7n nay nh\u1edd kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng linh ho\u1ea1t, l\u01b0u tr\u1eef d\u1eef li\u1ec7u d\u1ea1ng t\u00e0i li\u1ec7u v\u00e0","og_url":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-mongodb\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2025-12-30T15:36:29+00:00","article_modified_time":"2025-12-30T15:36:56+00:00","og_image":[{"width":800,"height":421,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/12\/cau-hoi-phong-van-mongodb-scaled.png","type":"image\/png"}],"author":"Nhat Anh","twitter_card":"summary_large_image","twitter_creator":"@ITviec","twitter_site":"@ITviec","twitter_misc":{"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi":"Nhat Anh","\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc":"33 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-mongodb\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-mongodb\/"},"author":{"name":"Nhat Anh","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/149bc2a01a01f229e57b9cdf61f3ebd0"},"headline":"Top 20+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB th\u01b0\u1eddng g\u1eb7p","datePublished":"2025-12-30T15:36:29+00:00","dateModified":"2025-12-30T15:36:56+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-mongodb\/"},"wordCount":8342,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-mongodb\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/12\/cau-hoi-phong-van-mongodb-scaled.png","articleSection":["Chuy\u00ean m\u00f4n IT","Database"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-mongodb\/","url":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-mongodb\/","name":"Top 20+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB th\u01b0\u1eddng g\u1eb7p - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-mongodb\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-mongodb\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/12\/cau-hoi-phong-van-mongodb-scaled.png","datePublished":"2025-12-30T15:36:29+00:00","dateModified":"2025-12-30T15:36:56+00:00","description":"\u00d4n t\u1eadp top nh\u1eefng c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB th\u01b0\u1eddng g\u1eb7p k\u00e8m v\u00ed d\u1ee5 th\u1ef1c h\u00e0nh, gi\u00fap b\u1ea1n t\u1ef1 tin chinh ph\u1ee5c m\u1ecdi bu\u1ed5i ph\u1ecfng v\u1ea5n.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-mongodb\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/cau-hoi-phong-van-mongodb\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-mongodb\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/12\/cau-hoi-phong-van-mongodb-scaled.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/12\/cau-hoi-phong-van-mongodb-scaled.png","width":800,"height":421,"caption":"c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n mongodb - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-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":"Top 20+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n MongoDB th\u01b0\u1eddng g\u1eb7p"}]},{"@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\/149bc2a01a01f229e57b9cdf61f3ebd0","name":"Nhat Anh","image":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/04\/pham-nhat-anh-vippro-196x200.jpeg","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/04\/pham-nhat-anh-vippro-196x200.jpeg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/04\/pham-nhat-anh-vippro-196x200.jpeg","caption":"Nhat Anh"},"url":"https:\/\/itviec.com\/blog\/author\/nhat-anh\/"}]}},"_links":{"self":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/93878","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\/233"}],"replies":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/comments?post=93878"}],"version-history":[{"count":5,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/93878\/revisions"}],"predecessor-version":[{"id":93962,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/93878\/revisions\/93962"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/93960"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=93878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=93878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=93878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}