Find document with array that contains a specific value

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Find document with array that contains a specific value

How to Find Documents with an Array that Contains a Specific Value 🍣

Are you struggling to find documents in your MongoDB database that have an array containing a specific value? Look no further! In this blog post, we'll explore a common issue faced by many developers and provide you with easy solutions to tackle this problem using Mongoose.

The Scenario 🍽️

Let's say you have a MongoDB schema for a person collection with the following structure:

person = {
    name: String,
    favoriteFoods: Array
}

The favoriteFoods field is an array populated with strings representing the person's favorite foods.

Now, you want to find all the persons who have "sushi" as their favorite food. How can you accomplish this efficiently using Mongoose?

The Expected Solution πŸ’‘

At first, you might expect to use the $contains operator in MongoDB. However, there's a catch – this operator doesn't exist! So, we need to find an alternative approach to solve this problem.

Solution: Using $in Operator 🎯

Fortunately, MongoDB provides the $in operator that allows us to determine if an array contains any of the specified values. In our case, we can use it to find documents that have "sushi" in their favoriteFoods array.

Here's how you can do it using Mongoose:

PersonModel.find({ favoriteFoods: { $in: ["sushi"] } }, function(error, persons) {
    if (error) {
        console.error(error);
    } else {
        console.log(persons);
    }
});

By passing { favoriteFoods: { $in: ["sushi"] } } as the query object to find(), Mongoose will search for documents where the favoriteFoods array contains "sushi". The resulting documents will then be returned, allowing you to work with the data as needed.

Conclusion πŸŽ‰

Finding documents in MongoDB with an array that contains a specific value doesn't have to be a headache. By using the $in operator in Mongoose, you can easily filter and retrieve the desired documents without any fuss.

Whether you're building a restaurant review app or simply exploring food preferences, this solution will come in handy. So go ahead, try it out, and elevate your MongoDB querying game!

Got any other MongoDB questions or want to share your success stories? Feel free to leave a comment below or reach out on social media. We'd love to hear from you! πŸ˜„

Keep coding and bon appΓ©tit! πŸš€πŸ½οΈ

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

πŸ”₯ πŸ’» πŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! πŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings πŸ’₯βœ‚οΈ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide πŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? πŸ€” Well, my