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

133
Views
How I can find if string exists inside mongodb Array

I am trying to find document based on profileID in MongoDB. I want to check if string postid exists in array or not. How I can do that? I tried $all.

Mongo data:

{
  "profileId": "abc",
  "likedPosts": [
    'post1',
    'post2'
  ]
}

Trying to find if post1 exists in array:

findOne({
  profileId
}, {
  likedPosts: {
    $all: ['post1']
  }
}, )
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you want to check if a string exists in an array field you can simply do:

findOne({
  profileId,
  likedPosts: 'post1'
  }
}, )

Docs

Also you might notice i have made the whole query as the first parameter. The second parameter is usually the callback to run, once the query has executed.

about 4 years ago · Juan Pablo Isaza 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!