Logo

dev-resources.site

for different kinds of informations.

Bootcamping 01: An Unexpected Behavior of Mongoose

Published at
10/10/2024
Categories
findone
mongoose
mongodb
bootcamp
Author
aleflewitt
Categories
4 categories in total
findone
open
mongoose
open
mongodb
open
bootcamp
open
Author
10 person written this
aleflewitt
open
Bootcamping 01: An Unexpected Behavior of Mongoose

When you’re a student in a bootcamp, you’re introduced to so many new
technologies all at once, and sometimes it can be overwhelming. One approach that I’ve found helpful is to try and identify what each technology is adding. Ask yourself - what problem is this technology coming to solve? Why do we need it in the first place?

The tendency to not ask these questions, may stem from a feeling of professionalism that comes with using a new package or technology. It can seem “cool” or “tech-y” to add new jargon to your toolbox, that this can cloud the true professionalism, which is to ask “why do I actually need this package in my project?” Lugging around an unnecessary package can cause more harm than help when deploying and it’s important to attempt to understand the helpfulness of a given package you plan on using in your project.

In coding bootcamps, when teaching usage of the MongoDB database, it’s common to almost immediately use it with the additional package “Mongoose”. Why? What does this third-party library add to the regular MongoDB?

Besides the basic advantages of rigid schemas and models (see this fantastic article by Jesse Hall, Getting Started with MongoDB & Mongoose), there are some hidden advantages, one of which I recently discovered.

A student of mine was using the findOne method, passing it a query value which was “undefined”, (for example, findOne({username}) where username is undefined and in response he was receiving an actual MongoDB document. I was surprised? The MongoDB documentation clearly states that such a case should in-fact respond with “null”!

The answer I discovered lies in one way that Mongoose is “smarter” than MongoDB. Mongoose, in its attempt to only return documents and avoid returning null, won’t ‘agree’ as it were, to send a request of findOne with an undefined query, and so, behind the scenes, when Mongoose’s findOne method is passed an undefined query value, it doesn’t send that query to MongoDB - it first translates the undefined into an empty object, and in-turn, generates a response of an actual document instead of null.

Food for thought: Is this an advantage or a disadvantage? What are your thoughts?

Happy learning! Stay curious, and please share your thoughts!

mongoose Article's
30 articles in total
Favicon
Crudify: Automate Your Mongoose CRUD Operations in NestJS
Favicon
6 Steps to Set Up MongoDB Atlas for Node.js Applications
Favicon
Mysql 101 for Mongoose developer.
Favicon
Tutorial de Instalação: Express com MongoDB e Mongoose
Favicon
Today’s new knowledge #6(Mongoose)
Favicon
Today’s new knowledge #10 (Building a Flexible Query Builder for MongoDB with Mongoose)
Favicon
mongoose connect to express
Favicon
I Fumbled on a Next.js MongoDB Error and Learned the Key Differences Between Mongoose and MongoClient
Favicon
Setup Eslint Prettier in a TypeScript project with mongoose ODM
Favicon
Bootcamping 01: An Unexpected Behavior of Mongoose
Favicon
Common Myths About Mongoose
Favicon
5 Quick And Easy MongoDB Optimizations (part 1)
Favicon
Mongoose Interview Questions
Favicon
MongoDB vs. Mongoose: Understanding Their Roles and Differences
Favicon
We finally have a fullstack framework for MongoDB
Favicon
Mongoose
Favicon
💬 Building a Real-time Chat Feature for Virtual Gift Store Using Socket.IO with MERN Stack 🚀
Favicon
The Power of exec() in Mongoose: Unlocking Better Query Execution
Favicon
Enhancing Mongoose Reference Handling in Node.js
Favicon
Mongoose Documentation
Favicon
How to Connect MongoDB with Node.js: A Comprehensive Guide
Favicon
Updating Non-Primitive Data in an Array Using Transactions and Rollbacks
Favicon
Method Chaining in Mongoose: A Brief Overview
Favicon
Understanding Transactions and Rollbacks in MongoDB
Favicon
Understanding Populating Referencing Fields in Mongoose
Favicon
How to Use Bcrypt for Password Hashing in Node.js
Favicon
Getting Started with Mongoose
Favicon
Running Unit Tests with MongoDB in a Node.js Express Application using Jest
Favicon
Setting up MongoDB using Mongoose in Node.js
Favicon
I built an open-source schema visualisation tool for mongoose

Featured ones: