The Journey of #100DaysOfCode (@Ayman_Dandan)

๐ƒ๐š๐ฒ ๐Ÿ๐ŸŽ of 100DaysOfCode

  • ๐˜ผ๐™œ๐™š ๐˜พ๐™–๐™ก๐™˜๐™ช๐™ก๐™–๐™ฉ๐™ค๐™ง -

The Age Calculator takes a userโ€™s date of birth and calculates their exact age in years, months, and days. Itโ€™s a simple yet practical tool that showcases date manipulation and calculation in JavaScript.

I learned in this project about the date class and how to handle various date formats.

3 Likes

๐ƒ๐š๐ฒ ๐Ÿ๐Ÿ of 100DaysOfCode

  • ๐˜พ๐™ž๐™ง๐™˜๐™ช๐™ก๐™–๐™ง ๐™‹๐™ง๐™ค๐™œ๐™ง๐™š๐™จ๐™จ ๐˜ฝ๐™–๐™ง -

The Circular Progress Bar visually represents progress or completion status using a circular graphic. This type of progress bar is commonly used in dashboards, fitness trackers, and more.

I learned how to create circular shapes and animations using CSS and SVG (using stroke and stroke-dasharray to animate the SVG), dynamically update the progress value with JavaScript, and implement smooth transitions and animations for visual appeal.
Additionally, I tackled handling edge cases to ensure the progress bar functions correctly at all values.

4 Likes

๐ƒ๐š๐ฒ ๐Ÿ๐Ÿ of #100DaysOfCode

  • ๐™๐™ค๐™–๐™จ๐™ฉ ๐™‰๐™ค๐™ฉ๐™ž๐™›๐™ž๐™˜๐™–๐™ฉ๐™ž๐™ค๐™ฃ -

The Toast :bread: or Snackbar :chocolate_bar: Notification is a small, temporary pop-up message that appears at the bottom or top of the screen to inform users of an event or action. Itโ€™s a subtle way to provide feedback without interrupting the user experience.

Today, I focused on creating a responsive and animated toast notification using HTML, CSS, and JavaScript. I explored different ways to trigger the notification, control its duration, and apply smooth fade-in and fade-out effects. Additionally, I learned how to handle multiple notifications and ensure they stack correctly without overlapping.

3 Likes

๐ƒ๐š๐ฒ ๐Ÿ๐Ÿ‘ of #100DaysOfCode

  • ๐˜ฟ๐™ง๐™–๐™œ & ๐˜ฟ๐™ง๐™ค๐™ฅ -

Today, I built a Drag and Drop interface that allows users to move elements around on the page with a simple drag action. This feature is commonly used in interactive lists, image galleries, and customizable dashboards to enhance user experience.

I learned how to implement drag-and-drop functionality using the native HTML5 Drag and Drop API, combined with JavaScript to handle drag events. I also explored how to create smooth and intuitive interactions, manage drop zones, and ensure that elements are placed correctly after being dropped. Additionally, I worked on improving the visual feedback during the drag process to make the interface more user-friendly.

3 Likes

๐ƒ๐š๐ฒ ๐Ÿ๐Ÿ’ of #100DaysOfCode

  • ๐™๐™ž๐™˜ ๐™๐™–๐™˜ ๐™๐™ค๐™š ๐™‚๐™–๐™ข๐™š -

Today, I developed a classic Tic Tac Toe game where two players can compete to get three in a row. Itโ€™s a fun project that combines logic, strategy, and interactive gameplay.

I learned how to implement the game logic for detecting wins, losses, and draws, as well as how to dynamically update the game board using JavaScript. I also explored techniques for resetting the game, tracking player turns, and enhancing the user interface to make the game more engaging. It was a great exercise in structuring code for clarity and reusability.

3 Likes

๐ƒ๐š๐ฒ ๐Ÿ๐Ÿ“ of #100DaysOfCode :tada:

  • ๐™Ž๐™ฅ๐™ž๐™ฃ ๐™’๐™๐™š๐™š๐™ก -

I built a Spin Wheel game today where users can spin the wheel to land on random values that might represent prizes or outcomes. To create the spin wheel, I leveraged Chart.js, a powerful JavaScript library that simplifies the process of creating various types of charts and visualizations.

I learned how to use Chart.js to create a visually appealing spin wheel, taking advantage of its pie chart functionality to represent the different segments. I explored customizing the chart to rotate and simulate spinning, as well as adding animations and interactivity to make the experience more engaging. Integrating Chart.js into this project made it easier to handle the visual aspects, allowing me to focus more on the game logic and user experience.

3 Likes

๐ƒ๐š๐ฒ ๐Ÿ๐Ÿ” of #100DaysOfCode

  • ๐™„๐™ข๐™–๐™œ๐™š ๐˜พ๐™ค๐™ข๐™ฅ๐™–๐™ง๐™ž๐™จ๐™ž๐™ค๐™ฃ ๐™Ž๐™ก๐™ž๐™™๐™š๐™ง -

I built an Image Comparison Slider today, a tool that allows users to compare two images by sliding a handle back and forth. This type of slider is commonly used to highlight before-and-after scenarios, such as in photo editing or product comparisons.

I learned how to implement a draggable slider using JavaScript to control the width of the images dynamically. I also explored techniques for creating a smooth and responsive sliding experience, ensuring the images align correctly as the user interacts with the slider. This project was a great exercise in combining CSS for layout and JavaScript for interactivity.

2 Likes

๐ƒ๐š๐ฒ ๐Ÿ๐Ÿ• of #100DaysOfCode

  • ๐™ƒ๐™–๐™ฃ๐™œ๐™ข๐™–๐™ฃ -

I created a Hangman game today, a classic word-guessing challenge where players try to figure out a hidden word by guessing letters. With each incorrect guess, a part of the hangman figure is drawn, and the game ends if the figure is fully drawn before the word is guessed.

I learned how to implement game logic for tracking correct and incorrect guesses, updating the displayed word, and managing the state of the hangman figure. I also explored ways to make the game more interactive by adding visual feedback for the player. This project was a fun dive into creating a simple yet engaging game with JavaScript.

3 Likes

๐ƒ๐š๐ฒ ๐Ÿ๐Ÿ– of #100DaysOfCode

  • ๐˜ผ๐™ก๐™–๐™ง๐™ข ๐˜ผ๐™ฅ๐™ฅ ๐™ฌ๐™ž๐™ฉ๐™ ๐™ˆ๐™ช๐™ก๐™ฉ๐™ž๐™ฅ๐™ก๐™š ๐˜ผ๐™ก๐™–๐™ง๐™ข๐™จ -

I built an Alarm App today that allows users to set multiple alarms with custom times. This feature-rich app lets you schedule various alarms for different tasks throughout the day, ensuring you stay on top of your schedule.

I learned how to work with the JavaScript ๐˜‹๐˜ข๐˜ต๐˜ฆ object to track time and trigger alarms accurately. By comparing the current time with the set alarm times, I was able to implement a system that efficiently handles multiple alarms without relying on timers like ๐˜ด๐˜ฆ๐˜ต๐˜›๐˜ช๐˜ฎ๐˜ฆ๐˜ฐ๐˜ถ๐˜ต or ๐˜ด๐˜ฆ๐˜ต๐˜๐˜ฏ๐˜ต๐˜ฆ๐˜ณ๐˜ท๐˜ข๐˜ญ. This approach provided a deeper understanding of date and time manipulation in JavaScript.

3 Likes

๐ƒ๐š๐ฒ ๐Ÿ๐Ÿ— of #100DaysOfCode

  • ๐™Ž๐™ก๐™ž๐™™๐™ž๐™ฃ๐™œ ๐™„๐™ข๐™–๐™œ๐™š ๐™‹๐™ช๐™ฏ๐™ฏ๐™ก๐™š -

I developed a Sliding Image Puzzle today, where users can rearrange pieces of an image by sliding them into the correct position to complete the picture. Itโ€™s a fun and challenging game that tests both logic and spatial reasoning.

I learned how to break an image into smaller pieces and manage their positions using JavaScript. I implemented logic for detecting valid moves and ensuring that only adjacent pieces can slide into the empty space. This project also gave me the opportunity to work on enhancing the user interface, making the puzzle visually appealing and intuitive to interact with.

3 Likes

๐ƒ๐š๐ฒ ๐Ÿ‘๐ŸŽ of #100DaysOfCode :tada:

  • ๐™ˆ๐™ž๐™ฃ๐™š๐™จ๐™ฌ๐™š๐™š๐™ฅ๐™š๐™ง :bomb: -

This time, I created a Minesweeper game! :rocket:
A classic puzzle game where players uncover cells on a grid while avoiding hidden mines. The goal is to clear the board without detonating any mines, using numbered clues to determine the locations of the mines.

I learned a lot when creating this complex game, from implementing game logic for generating a randomized grid with mines and numbers, to managing game states.

  • ๐—ฆ๐—ผ๐—บ๐—ฒ ๐—œ๐—บ๐—ฝ๐—น๐—ฒ๐—บ๐—ฒ๐—ป๐˜๐—ฒ๐—ฑ ๐—™๐—ฒ๐—ฎ๐˜๐˜‚๐—ฟ๐—ฒ๐˜€:
    โ†’ Different difficulty levels each with a specific number of mines and grid size.
    โ†’ Custom difficulty (popup) where you can specify the height, width and number of mines.
    โ†’ Timer & high scores for each difficulty to track performance.
    โ†’ Explosion sound is played when you land on a mine. :bomb:

A feature thatโ€™s ๐™ฃ๐™ค๐™ฉ ๐™ž๐™ข๐™ฅ๐™ก๐™š๐™ข๐™š๐™ฃ๐™ฉ๐™š๐™™ yet:
โ†’ Right clicking to add a flag that marks the cell as a potential mine and stops you from accidentally clicking on the cell.

Feel free to try the game and give some feedback. :hugs:

3 Likes

๐ƒ๐š๐ฒ ๐Ÿ‘๐Ÿ of #100DaysOfCode

  • ๐‘ต๐’๐’…๐’†.๐‘ฑ๐‘บ -

Node.js is a game-changer for developers who want to build fast, scalable applications. With its asynchronous, event-driven architecture and non-blocking I/O, Node.js handles multiple operations with ease, making it perfect for real-time applications like chat apps and online games.

๐—ช๐—ต๐—ฎ๐˜ ๐—ถ๐˜€ ๐—ก๐—ผ๐—ฑ๐—ฒ.๐—ท๐˜€?
Node.js is a runtime environment that allows you to execute JavaScript code outside of a web browser. Itโ€™s built on the V8 JavaScript engine, which is the same engine that powers Google Chrome, making it incredibly fast and efficient.

~Here are some key points about Node.js:
->๐€๐ฌ๐ฒ๐ง๐œ๐ก๐ซ๐จ๐ง๐จ๐ฎ๐ฌ ๐š๐ง๐ ๐„๐ฏ๐ž๐ง๐ญ-๐ƒ๐ซ๐ข๐ฏ๐ž๐ง:
Node.js is designed to handle many operations at once. Instead of waiting for tasks to complete, it moves on to other tasks and deals with the results when theyโ€™re ready. This makes it highly efficient for I/O-heavy tasks.
->๐๐จ๐ง-๐๐ฅ๐จ๐œ๐ค๐ข๐ง๐  ๐ˆ/๐Ž:
This feature allows Node.js to handle multiple connections at the same time without the need to create a new thread for each connection, making it lightweight and scalable.
->๐’๐ข๐ง๐ ๐ฅ๐ž-๐“๐ก๐ซ๐ž๐š๐๐ž๐:
Despite being able to handle multiple operations concurrently, Node.js uses a single thread to process tasks. This might seem counterintuitive, but thanks to its event-driven architecture, it can manage thousands of connections at once.
->๐‚๐ซ๐จ๐ฌ๐ฌ-๐๐ฅ๐š๐ญ๐Ÿ๐จ๐ซ๐ฆ:
Node.js is compatible with various operating systems, including Windows, Linux, and macOS.
->๐‘๐ข๐œ๐ก ๐„๐œ๐จ๐ฌ๐ฒ๐ฌ๐ญ๐ž๐ฆ:
The Node Package Manager (npm) offers a vast collection of libraries and tools, making development faster and easier.

If youโ€™re exploring backend development, Node.js is definitely worth your time. Itโ€™s cross-platform, powerful, and widely used in the industry.

2 Likes

๐ƒ๐š๐ฒ ๐Ÿ‘๐Ÿ of #100DaysOfCode

  • ๐‘ป๐’‰๐’† ๐‘ฐ๐’Ž๐’‘๐’๐’“๐’•๐’‚๐’๐’„๐’† ๐’๐’‡ ๐‘ถ๐’“๐’ˆ๐’‚๐’๐’Š๐’›๐’†๐’… ๐‘ญ๐’Š๐’๐’†๐’” ๐’Š๐’ ๐‘ต๐’๐’…๐’†.๐’‹๐’” ๐‘ท๐’“๐’๐’‹๐’†๐’„๐’•๐’” -

Whether youโ€™re just starting out or scaling up a project, the way you structure your files and folders in Node.js can make or break your development process. Hereโ€™s why it matters:

โ†’ ๐Œ๐š๐ข๐ง๐ญ๐š๐ข๐ง๐š๐›๐ข๐ฅ๐ข๐ญ๐ฒ: A clean and logical structure helps you quickly locate files, making updates and debugging more efficient.

โ†’ ๐’๐œ๐š๐ฅ๐š๐›๐ข๐ฅ๐ข๐ญ๐ฒ: A clear structure supports the growth of your project. When adding new features or modules, a well-organized directory system allows for seamless integration without cluttering your codebase.

โ†’ ๐‚๐จ๐ฅ๐ฅ๐š๐›๐จ๐ซ๐š๐ญ๐ข๐จ๐ง: Working with a team? A standardized file structure ensures everyoneโ€™s on the same page. It reduces onboarding time for new developers and makes code reviews more efficient.

โ†’ ๐๐ž๐ฌ๐ญ ๐๐ซ๐š๐œ๐ญ๐ข๐œ๐ž๐ฌ: Following conventions like placing controllers, models, and routers in their respective directories not only keeps your project clean but also aligns with industry standards, making your project easier to understand for others.

โ†’ ๐„๐ซ๐ซ๐จ๐ซ ๐‘๐ž๐๐ฎ๐œ๐ญ๐ข๐จ๐ง: Properly organizing your files helps in avoiding circular dependencies, incorrect imports, and other common errors that can arise from a disorganized codebase.

Organizing your project isnโ€™t just about aestheticsโ€”itโ€™s about creating a solid foundation for sustainable development.

2 Likes

๐ƒ๐š๐ฒ ๐Ÿ‘๐Ÿ’ of #100DaysOfCode

  • ๐‘ด๐’๐’๐’ˆ๐’๐‘ซ๐‘ฉ ๐‘บ๐’„๐’‰๐’†๐’Ž๐’‚๐’” -

If youโ€™re working with Node.js and MongoDB, understanding Mongoose schemas is crucial for creating robust and scalable applications. Mongoose schemas act as blueprints, defining the structure and rules for your data.

๐Š๐ž๐ฒ ๐…๐ž๐š๐ญ๐ฎ๐ซ๐ž๐ฌ:
โ†’ ๐…๐ข๐ž๐ฅ๐ ๐“๐ฒ๐ฉ๐ž๐ฌ: Define the data types of each field (String, Number, Date, etc.).
โ†’ ๐•๐š๐ฅ๐ข๐๐š๐ญ๐ข๐จ๐ง: Enforce rules like required fields, value limits, and custom logic.
โ†’ ๐ˆ๐ง๐๐ž๐ฑ๐ž๐ฌ: Improve query performance with schema-defined indexes.
โ†’ ๐•๐ข๐ซ๐ญ๐ฎ๐š๐ฅ๐ฌ: Virtuals are fields that donโ€™t get persisted to the database but can be used in code, often for computed properties.
โ†’ ๐Œ๐ž๐ญ๐ก๐จ๐๐ฌ: Custom instance methods can be added to the schema to perform operations on documents.

The attached schema defines a User model with fields for name, email, age, and a timestamp for when the document is created. Itโ€™s a powerful way to enforce data integrity and enhance your MongoDB queries.
1723496128433

1 Like

๐ƒ๐š๐ฒ ๐Ÿ‘๐Ÿ“ of #100DaysOfCode

  • ๐‘ฑ๐‘พ๐‘ป ๐’‚๐’๐’… ๐‘ฏ๐‘ป๐‘ป๐‘ท ๐‘บ๐’•๐’‚๐’•๐’–๐’” ๐‘ช๐’๐’…๐’†๐’” ๐’Š๐’ ๐‘ต๐’๐’…๐’†.๐’‹๐’” -

When working with JSON Web Tokens (JWT) in Node.js, understanding how various HTTP status codes relate to authentication and authorization processes is crucial. These status codes help communicate the result of client-server interactions, particularly when handling user access and security.

Hereโ€™s a comprehensive guide to the HTTP status codes youโ€™ll frequently encounter when implementing JWT in your applications:

:unlock: ๐Ÿ๐ŸŽ๐ŸŽ ๐Ž๐Š: ๐’๐ฎ๐œ๐œ๐ž๐ฌ๐ฌ! Your request was processed, and the JWT is valid, granting access to the resource.

:new: ๐Ÿ๐ŸŽ๐Ÿ ๐‚๐ซ๐ž๐š๐ญ๐ž๐: A new resource has been successfully created, such as a user account, and a JWT is issued.

:warning: ๐Ÿ’๐ŸŽ๐ŸŽ ๐๐š๐ ๐‘๐ž๐ช๐ฎ๐ž๐ฌ๐ญ: The server couldnโ€™t understand the request due to invalid syntax. This might happen if the request lacks necessary parameters or the JWT is malformed.

:no_entry_sign: ๐Ÿ’๐ŸŽ๐Ÿ ๐”๐ง๐š๐ฎ๐ญ๐ก๐จ๐ซ๐ข๐ณ๐ž๐: Access denied. This occurs when the JWT is missing, expired, or invalid, meaning authentication has failed.

:no_pedestrians: ๐Ÿ’๐ŸŽ๐Ÿ‘ ๐…๐จ๐ซ๐›๐ข๐๐๐ž๐ง: Youโ€™re authenticated, but you donโ€™t have the right permissions to access this resource, even with a valid JWT.

:question: ๐Ÿ’๐ŸŽ๐Ÿ’ ๐๐จ๐ญ ๐…๐จ๐ฎ๐ง๐: The requested resource couldnโ€™t be found. This isnโ€™t specific to JWT but is often encountered in API development.

:hourglass_flowing_sand: ๐Ÿ’๐Ÿ๐Ÿ— ๐€๐ฎ๐ญ๐ก๐ž๐ง๐ญ๐ข๐œ๐š๐ญ๐ข๐จ๐ง ๐“๐ข๐ฆ๐ž๐จ๐ฎ๐ญ: The session has expired, often due to a JWT that is no longer valid because of expiration.

:clipboard: ๐Ÿ’๐Ÿ๐Ÿ ๐”๐ง๐ฉ๐ซ๐จ๐œ๐ž๐ฌ๐ฌ๐š๐›๐ฅ๐ž ๐„๐ง๐ญ๐ข๐ญ๐ฒ: The server understands the request but canโ€™t
process it due to semantic errors, like an invalid JWT payload.

:warning: ๐Ÿ“๐ŸŽ๐ŸŽ ๐ˆ๐ง๐ญ๐ž๐ซ๐ง๐š๐ฅ ๐’๐ž๐ซ๐ฏ๐ž๐ซ ๐„๐ซ๐ซ๐จ๐ซ: Something went wrong on the server, possibly during JWT processing or verification.

:construction: ๐Ÿ“๐ŸŽ๐Ÿ‘ ๐’๐ž๐ซ๐ฏ๐ข๐œ๐ž ๐”๐ง๐š๐ฏ๐š๐ข๐ฅ๐š๐›๐ฅ๐ž: The server is currently unable to handle the request, which could affect JWT validation or issuance.
1723571287952

2 Likes

๐ƒ๐š๐ฒ ๐Ÿ‘๐Ÿ” of #100DaysOfCode

  • ๐‘ญ๐’“๐’๐’Ž ๐‘บ๐’„๐’‰๐’†๐’Ž๐’‚ ๐’•๐’ ๐‘ด๐’๐’…๐’†๐’ -

After defining a schema, the next step is to create a model. A model in Mongoose provides an interface to the database for CRUD (Create, Read, Update, Delete) operations. Itโ€™s the bridge between your code and the database.

-๐–๐ก๐ฒ ๐ฎ๐ฌ๐ž ๐ฆ๐จ๐๐ž๐ฅ๐ฌ?
Models are where the magic happens! They allow you to interact with the MongoDB collection that corresponds to the defined schema, making data handling straightforward and efficient.

Attached is an example a model User is created from the schema and exported to be used in other files. Then this model is used to create and save a new user to the database.

๐˜•๐˜ฆ๐˜น๐˜ต ๐˜ถ๐˜ฑ: ๐˜๐˜ฐ๐˜ธ ๐˜ต๐˜ฐ ๐˜ค๐˜ณ๐˜ฆ๐˜ข๐˜ต๐˜ฆ ๐˜ข๐˜ฏ๐˜ฅ ๐˜ช๐˜ฏ๐˜ด๐˜ฆ๐˜ณ๐˜ต ๐˜ฅ๐˜ฐ๐˜ค๐˜ถ๐˜ฎ๐˜ฆ๐˜ฏ๐˜ต๐˜ด ๐˜ช๐˜ฏ๐˜ต๐˜ฐ ๐˜บ๐˜ฐ๐˜ถ๐˜ณ ๐˜”๐˜ฐ๐˜ฏ๐˜จ๐˜ฐ๐˜‹๐˜‰ ๐˜ค๐˜ฐ๐˜ญ๐˜ญ๐˜ฆ๐˜ค๐˜ต๐˜ช๐˜ฐ๐˜ฏ ๐˜ธ๐˜ช๐˜ต๐˜ฉ ๐˜ฆ๐˜ข๐˜ด๐˜ฆ.

2 Likes

๐ƒ๐š๐ฒ ๐Ÿ‘๐Ÿ• of #100DaysOfCode

  • ๐‘ช๐’“๐’†๐’‚๐’•๐’Š๐’๐’ˆ ๐‘ซ๐’๐’„๐’–๐’Ž๐’†๐’๐’•๐’” ๐’˜๐’Š๐’•๐’‰ ๐‘ด๐’๐’๐’ˆ๐’๐’๐’”๐’† -

Creating documents in MongoDB is a breeze with Mongoose. The ๐’„๐’“๐’†๐’‚๐’•๐’† function allows you to insert new documents into your collection, using the model youโ€™ve defined.

๐–๐ก๐ฒ ๐ข๐ฌ ๐ญ๐ก๐ข๐ฌ ๐ฎ๐ฌ๐ž๐Ÿ๐ฎ๐ฅ?
The create function not only saves the data to the database but also returns the saved document, making it easy to confirm the operation or use the result in your code.

In the attached snippet, the ๐˜œ๐˜ด๐˜ฆ๐˜ณ.๐˜ค๐˜ณ๐˜ฆ๐˜ข๐˜ต๐˜ฆ method inserts a new document into the User collection and returns the saved document in the promise.

2 Likes

๐ƒ๐š๐ฒ ๐Ÿ‘๐Ÿ– of #100DaysOfCode

  • ๐‘ญ๐’Š๐’๐’…๐’Š๐’๐’ˆ ๐‘ซ๐’๐’„๐’–๐’Ž๐’†๐’๐’•๐’” ๐’Š๐’ ๐‘ด๐’๐’๐’ˆ๐’๐‘ซ๐‘ฉ ๐’˜๐’Š๐’•๐’‰ ๐‘ด๐’๐’๐’ˆ๐’๐’๐’”๐’† -

One of the most powerful features of Mongoose is its ability to query your database with ease. The ๐Ÿ๐ข๐ง๐ function is a versatile tool that allows you to search for documents that match certain criteria.

๐–๐ก๐ฒ ๐ข๐ฌ ๐ข๐ญ ๐ฉ๐จ๐ฐ๐ž๐ซ๐Ÿ๐ฎ๐ฅ?
With find, you can retrieve multiple documents at once, filter results, and even control what fields are returned. Itโ€™s the backbone of many database operations in your application.

In the attached snippet, User.find is used to retrieve all users who are 25 years old. The result is an array of documents that match the query.

2 Likes

๐ƒ๐š๐ฒ ๐Ÿ‘๐Ÿ— of #100DaysOfCode

  • ๐‘ธ๐’–๐’†๐’“๐’š๐’Š๐’๐’ˆ ๐’–๐’”๐’Š๐’๐’ˆ ๐’‡๐’Š๐’๐’…๐‘ถ๐’๐’† ๐’Š๐’ ๐‘ด๐’๐’๐’ˆ๐’๐’๐’”๐’† -

When you want to retrieve a single document that matches specific criteria, Mongooseโ€™s ๐’‡๐’Š๐’๐’…๐‘ถ๐’๐’† is the function to use. It returns the first document that matches the query or null if no document is found.

๐–๐ก๐ฒ ๐ข๐ฌ ๐ญ๐ก๐ข๐ฌ ๐ฎ๐ฌ๐ž๐Ÿ๐ฎ๐ฅ?
๐’‡๐’Š๐’๐’…๐‘ถ๐’๐’† is perfect for scenarios where youโ€™re looking for a unique document or the first match in a collection. Itโ€™s a quick way to fetch a single record without needing to filter through an entire array of results.

In the attached snippet, ๐’‡๐’Š๐’๐’…๐‘ถ๐’๐’† is used to find the first user with the email john@example.com. If no user is found, it returns null.

2 Likes

๐ƒ๐š๐ฒ ๐Ÿ’๐ŸŽ of #100DaysOfCode

  • ๐‘ฝ๐’‚๐’๐’Š๐’…๐’‚๐’•๐’๐’“๐’” ๐’Š๐’ ๐‘ด๐’๐’๐’ˆ๐’๐’๐’”๐’† -

When working with Mongoose, validators play a crucial role in ensuring the integrity and correctness of your data. Mongoose offers a variety of validators that can be applied directly to schema fields, and it also allows you to create custom validators to meet specific needs. Letโ€™s dive into the different types of validators you can use in Mongoose.

๐Ÿ. ๐๐ฎ๐ข๐ฅ๐ญ-๐ข๐ง ๐•๐š๐ฅ๐ข๐๐š๐ญ๐จ๐ซ๐ฌ
Mongoose provides a set of built-in validators that cover common validation scenarios. These are directly applied to schema fields and handle most validation tasks out of the box.

๐—›๐—ฒ๐—ฟ๐—ฒโ€™๐˜€ ๐—ฎ ๐—น๐—ผ๐—ผ๐—ธ ๐—ฎ๐˜ ๐˜€๐—ผ๐—บ๐—ฒ ๐—ฐ๐—ผ๐—บ๐—บ๐—ผ๐—ป ๐—ฏ๐˜‚๐—ถ๐—น๐˜-๐—ถ๐—ป ๐˜ƒ๐—ฎ๐—น๐—ถ๐—ฑ๐—ฎ๐˜๐—ผ๐—ฟ๐˜€:
-๐ซ๐ž๐ช๐ฎ๐ข๐ซ๐ž๐: Ensures a field is not empty.
-๐ฆ๐ข๐ง and ๐ฆ๐š๐ฑ: Enforce minimum and maximum values for numbers.
-๐ฆ๐ข๐ง๐ฅ๐ž๐ง๐ ๐ญ๐ก and ๐ฆ๐š๐ฑ๐ฅ๐ž๐ง๐ ๐ญ๐ก: Set minimum and maximum lengths for strings.
-๐ž๐ง๐ฎ๐ฆ: Restricts a field to a set of predefined values.
-๐ฆ๐š๐ญ๐œ๐ก: Validates a string against a regular expression pattern.

๐Ÿ. ๐‚๐ฎ๐ฌ๐ญ๐จ๐ฆ ๐•๐š๐ฅ๐ข๐๐š๐ญ๐จ๐ซ๐ฌ
Sometimes, built-in validators arenโ€™t enough, and you need more control. Mongoose allows you to define custom validators that let you implement complex validation logic.

Custom validators are perfect for scenarios that require specific business rules or logic that isnโ€™t covered by built-in validators.

๐Ÿ‘. ๐€๐ฌ๐ฒ๐ง๐œ๐ก๐ซ๐จ๐ง๐จ๐ฎ๐ฌ ๐•๐š๐ฅ๐ข๐๐š๐ญ๐จ๐ซ๐ฌ
Sometimes, validation involves operations like checking the uniqueness of a value against the database. Mongoose supports asynchronous validators for these cases.



2 Likes