{"id":6410,"date":"2020-12-18T18:34:57","date_gmt":"2020-12-18T18:34:57","guid":{"rendered":"http:\/\/ismiletechnologies.com\/?p=6410"},"modified":"2023-02-24T13:55:34","modified_gmt":"2023-02-24T08:25:34","slug":"mongodb-vs-mongoose-which-to-use-and-when-to-use","status":"publish","type":"post","link":"https:\/\/ismiletechnologies.com\/en_us\/devsecops\/mongodb-vs-mongoose-which-to-use-and-when-to-use\/","title":{"rendered":"MongoDB vs Mongoose: Which to use and when to use."},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"6410\" class=\"elementor elementor-6410\" data-elementor-post-type=\"post\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-7a2f361 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"7a2f361\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-5b45177\" data-id=\"5b45177\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-fae83c0 elementor-widget elementor-widget-text-editor\" data-id=\"fae83c0\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>In my few years of code writing and applications building, I have come across various cases and uses of both MongoDB driver and Mongoose. In this article I\u2019ll focus on using Node.js as the backend.<\/p><p>So what is MongoDB, MongoDB is a document modelled database that have come to be widely accepted in the tech community as a whole. Its use cases are numerous but still distinct from other types of database like relational databases (SQL, Postgresql), graph databases like dgraph, etc. Because of it\u2019s nature of being non-relational MongoDB generally do not enforce schemas which made it a little weird at first for programmers coming from sql or similar to sql backgrounds. Thus the need for Object Data Modelling , and that\u2019s when Mongoose came in as an ODM library for MongoDB to help enforce Schemas.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b3255b4 elementor-widget-divider--view-line elementor-widget elementor-widget-divider\" data-id=\"b3255b4\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"divider.default\">\n\t\t\t\t\t\t\t<div class=\"elementor-divider\">\n\t\t\t<span class=\"elementor-divider-separator\">\n\t\t\t\t\t\t<\/span>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2fe0278 elementor-widget elementor-widget-heading\" data-id=\"2fe0278\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Using Schemas<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-bc2cc7b elementor-widget elementor-widget-text-editor\" data-id=\"bc2cc7b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>Schemas are very helpful for strict data type being enforced, maintenance of structure and easy to relate to sql databases. So let\u2019s see some use cases where we see the need for schemas.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-da8753f elementor-widget elementor-widget-text-editor\" data-id=\"da8753f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<ul><li><span style=\"color: #000000;\">Database with multiple sub documents:<\/span> Querying for sub-documents in MongoDB can sometimes be very frustrating and I am a witness of it. Going to the documentations, you even get to see that when performing equality matches on sub-documents, field order matters and the sub-documents must match exactly.<\/li><li><span style=\"color: #000000;\">Databases with complicated structure:<\/span> When working on complicated databases with multiple levels of structure, schemas makes it easier and predictable. Like arrays of objects, possibly containing arrays with objects, looking at the data directly might get you confused and frustrated, but schemas gives insights while guaranteeing data format<\/li><li>Another good reason is the restriction to change the shape of your document unnecessarily. Imagine writing a object to a former named string value, MongoDB tends to write this slowly due to the change in shape of the document, thanks to resizing.<\/li><\/ul>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-155b820 elementor-widget elementor-widget-text-editor\" data-id=\"155b820\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>Mongoose is not the only ODM library, there are hapijs\/joi, MongoDB schemas, etc. And while Mongoose is good especially in areas of inferring data types, we should choose to use the MongoDB schema validation for schemas validation.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<section class=\"elementor-section elementor-inner-section elementor-element elementor-element-a405e11 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"a405e11\" data-element_type=\"section\" data-e-type=\"section\" data-settings=\"{&quot;background_background&quot;:&quot;gradient&quot;}\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-inner-column elementor-element elementor-element-196d237\" data-id=\"196d237\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-e69af0b elementor-widget elementor-widget-heading\" data-id=\"e69af0b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Want uninterrupted development, integration, testing, deployment, and monitoring of enterprise data operations? <\/h2>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-bbae0e8 elementor-widget elementor-widget-text-editor\" data-id=\"bbae0e8\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>Our experts will help enhance data availability, reduce downtime, lower operations costs, and mitigate data risks.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-fe77bd4 elementor-align-left elementor-mobile-align-left elementor-tablet-align-left elementor-widget elementor-widget-button\" data-id=\"fe77bd4\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"button.default\">\n\t\t\t\t\t\t\t\t\t\t<a class=\"elementor-button elementor-button-link elementor-size-xs\" href=\"#elementor-action%3Aaction%3Dpopup%3Aopen%26settings%3DeyJpZCI6IjI5OTEiLCJ0b2dnbGUiOmZhbHNlfQ%3D%3D\">\n\t\t\t\t\t\t<span class=\"elementor-button-content-wrapper\">\n\t\t\t\t\t\t\t\t\t<span class=\"elementor-button-text\">Talk to our experts<\/span>\n\t\t\t\t\t<\/span>\n\t\t\t\t\t<\/a>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<div class=\"elementor-element elementor-element-38afde4 elementor-widget-divider--view-line elementor-widget elementor-widget-divider\" data-id=\"38afde4\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"divider.default\">\n\t\t\t\t\t\t\t<div class=\"elementor-divider\">\n\t\t\t<span class=\"elementor-divider-separator\">\n\t\t\t\t\t\t<\/span>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b5b0282 elementor-widget elementor-widget-heading\" data-id=\"b5b0282\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">So why use MongoDB schema validation over Mongoose?<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6253e3f elementor-widget elementor-widget-text-editor\" data-id=\"6253e3f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>First, we should know that mongoose is not officially from MongoDB team and while it helps in ODM, it could have unintended consequences in the future especially for large projects and can\u2019t be helped or mitigated by the MongoDB team. Also, Relationship(s) between the Mongoose schema and our database model exists only within the confines of our Node.js application.<\/p><p>But yes, you can leverage the power of schemas and the uniqueness of the MongoDB model together using MongoDB schema validation. You can check how to go about it here.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a36f235 elementor-widget elementor-widget-heading\" data-id=\"a36f235\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Don\u2019t use Schemas, why?<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6ec6e84 elementor-widget elementor-widget-text-editor\" data-id=\"6ec6e84\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>While using schemas, you tend to miss the whole point of using MongoDB especially in a dynamically typed language like javascript or python. MongoDB provides you a non-relational document type database that can give rise to data evolution, data form manipulation, support for multiple forms of data while maintaining separation of concerns.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f77c53c elementor-widget elementor-widget-heading\" data-id=\"f77c53c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Let\u2019s see some reasons in details:<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-dbf6fec elementor-widget elementor-widget-text-editor\" data-id=\"dbf6fec\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<ul><li><span style=\"color: #000000;\">The real world is messy:<\/span> If you are on twitter you\u2019ll get to see many jokes about how users are supposed to use their products vs how users try to use it. Increase in users gives rise to new cases, and I can assure you that you can\u2019t get to predict which it would be. This sometimes leads to downed applications which harms the application reach in so many ways. Many a times I have always faulted the allow only this and block every other, rather it should be allow every other except this.<\/li><li>\u00a0<span style=\"color: #000000;\">Freedom from limited data types:<\/span> With MongoDB you don\u2019t get to tie yourself or users down with a single form that data should take, you tend to miss out on the tools and feature that MongoDB gives you to project your data to whatever you desire.<\/li><li><span style=\"color: #000000;\">Performance:<\/span> It isn\u2019t news that MongoDB drivers performs better than Mongoose and if you want to read more on this check it out here<\/li><li><span style=\"color: #000000;\">The Ecosystem:<\/span> MongoDB is expanding more and more, now we have MongoDB Switch for serverless, MongoDB Charts and all these are supported by the native drivers unlike third party libraries like Mongoose.<\/li><\/ul>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c01a883 elementor-widget-divider--view-line elementor-widget elementor-widget-divider\" data-id=\"c01a883\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"divider.default\">\n\t\t\t\t\t\t\t<div class=\"elementor-divider\">\n\t\t\t<span class=\"elementor-divider-separator\">\n\t\t\t\t\t\t<\/span>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-7727757 elementor-widget elementor-widget-heading\" data-id=\"7727757\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">How ISmile Technologies Help: <\/h2>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-1113455 elementor-widget elementor-widget-text-editor\" data-id=\"1113455\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<ul><li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"11\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span style=\"color: #000000;\">Expertise in MongoDB:<\/span> ISmile Technologies is well-versed in MongoDB and has extensive experience in developing, deploying, and managing MongoDB-based applications. This expertise allows them to provide robust support for MongoDB and assist clients in making informed decisions about whether to use MongoDB or Mongoose for their project.\u00a0<\/li><li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"11\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><span style=\"color: #000000;\">Tailored solutions:<\/span> ISmile Technologies creates customized solutions based on clients&#8217; specific needs and requirements. This approach ensures that clients get the most out of MongoDB and Mongoose, depending on their unique business needs, goals, and preferences.\u00a0<\/li><li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"11\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"3\" data-aria-level=\"1\"><span style=\"color: #000000;\">Seamless integration:<\/span> The team at ISmile Technologies understands the importance of seamless integration between databases and applications. We can seamlessly integrate MongoDB and Mongoose with other systems, tools, and platforms, ensuring that clients&#8217; applications run smoothly and efficiently.\u00a0<\/li><li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"11\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"4\" data-aria-level=\"1\"><span style=\"color: #000000;\">Performance optimization:<\/span> ISmile Technologies provides performance optimization services for both MongoDB and Mongoose. We can fine-tune clients&#8217; applications, ensuring we run faster and more efficiently, delivering better results and user experiences.\u00a0<\/li><li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"11\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"4\" data-aria-level=\"1\"><span style=\"color: #000000;\">Comprehensive support:<\/span> ISmile Technologies provides comprehensive support for MongoDB and Mongoose, including maintenance, updates, troubleshooting, and more. This level of support ensures that clients can rely on their databases and applications without any worries, allowing them to focus on their core business activities.\u00a0<\/li><\/ul>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b698c20 elementor-widget elementor-widget-text-editor\" data-id=\"b698c20\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\tISmile Technologies is a trusted technology partner for businesses looking to leverage MongoDB and Mongoose for their projects. We offer tailored solutions, seamless integration, performance optimization, and comprehensive support for both databases. With their expertise, businesses can make informed decisions on which database to use and when to use it, ensuring the success of their projects.\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d939f06 elementor-widget elementor-widget-text-editor\" data-id=\"d939f06\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>MongoDB is giving you a lot more with tools and features not forgetting Compass UI too we have to utilize these tools for our ease rather than limitations. Yet, if there is any needed use cases to use schemas, I\u2019ll advice using MongoDB schemas validation which through the native drivers helps validate your data properly. Mongoose is good and don\u2019t get me wrong, I\u2019m not telling you to drop it suddenly I\u2019m only showing you the need for a better tool and while Mongoose may help those coming from sql backgrounds, MongoDB using native drivers is not hard to learn and very useful.<\/p><p>Want to improve quality, speed, and collaboration and promote a culture of continuous improvement in the area of data analytics?. <span style=\"color: #14a49c;\"><a style=\"color: #14a49c;\" href=\"http:\/\/ismiletechnologies.com\/contact-us\/\">Schedule a Free Evaluation<\/a><\/span>.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>In my few years of code writing and applications building, I have come across various cases and uses of both MongoDB driver and Mongoose. In this article I\u2019ll focus on using Node.js as the backend. So what is MongoDB, MongoDB is a document modelled database that have come to be widely accepted in the tech [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":6523,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72],"tags":[87,88],"class_list":["post-6410","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devsecops","tag-mongodb","tag-mongoose"],"_links":{"self":[{"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/posts\/6410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/comments?post=6410"}],"version-history":[{"count":10,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/posts\/6410\/revisions"}],"predecessor-version":[{"id":37988,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/posts\/6410\/revisions\/37988"}],"wp:attachment":[{"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/media?parent=6410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/categories?post=6410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/tags?post=6410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}