Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

353
Views
How to find data based on sub-field name in MongoDB using Mongoose?

There is a sub-field called 'name' in MongoDB Collection (User):

[
{
    "teacher": {
        "name": "Alex",
        "email": "alex@domain.com"
    },
    "waiter": {
        "name": "Feliks",
        "email": "feliks@domain.com"
    },
    "pilot": [
    {
        "name": "Sam",
        "email": "sam@domain.com"
    },
    {
        "name": "alice",
        "email": "alice@domain.com"
    }
    ],
},
{
    "teacher": {
        "name": "max",
        "email": "max@domain.com"
    },
    "waiter": {
        "name": "Sam",
        "email": "sam@domain.com"
    },
    "pilot": [
    {
        "name": "richard",
        "email": "richard@domain.com"
    },
    {
        "name": "alice",
        "email": "alice@domain.com"
    }
    ],
}
]

How can I find data based on the field 'name'. For example, when I'm looking for 'Sam', it should return me the all documents since 'Sam' is in "waiter" and "pilot" in first and second documents respectively.

I cannot do something like:

User.find({"teacher.name": "Sam", "waiter.name": "Sam", "pilot.name": "Sam" })

This will return me nothing as it is an AND logic. What I need is an OR logic.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use the $or operator. So the query should look like this:

User.find({ $or: [
                  { "teacher.name": "Sam" }, 
                  { "waiter.name": "Sam" }, 
                  { "pilot.name": "Sam" }
                 ] 
          });

Read here for me details.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!