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

53
Views
Search for specific string in a nested object in mongodb documents using and condition

I am trying to search for documents in mongodb that include a specific search string input by the user. Collection that i am trying to search in is named imported_products and my mongodb document structure looks like following.

 {
┃     _id: 61add3b16c9e0008e6ad325b,
┃     shop: 'someshop',
┃     shopUserData: { name: 'LoTesting', email: 'vixen@yahoo.com' },
┃     productData: {
┃       title: 'Eyglo 3-in-1 Head Strap for Oculus Quest 2 with Face Silicone Cover & Controllers Grips Covers',
┃       price: 45.99,
┃       priceWithCurrency: '$45.99',
┃       images: [Array],
┃       ASIN: 'B08TWJJ3ZW',
┃       marketPlace: 'www.amazon.com',
┃       settings: [Object],
┃       shopifyProductId: 87187377131097
┃     },
┃     created_at: 2021-12-06T09:11:13.327Z
┃   },
┃   {
┃     _id: 61ae236cac749b088d427497,
┃     shop: 'shomeshop',
┃     shopUserData: { name: 'LoTesting', email: 'test@gmail.com' },
┃     productData: {
┃       title: 'Xbox Series X',
┃       description: 'Amazon Prime customers will have priority access to the Xbox Series X until November 24, 2021.',
┃       price: 'price not available',
┃       priceWithCurrency: 'price not available',
┃       images: [Array],
┃       ASIN: 'B08H75RTZ8',
┃       marketPlace: 'www.amazon.com',
┃       settings: [Object],
┃       shopifyProductId: 5736818278172
┃     },
┃     created_at: 2021-12-06T14:51:24.755Z
┃   },

What i need is that, for a specific shop, i need to look up into all of the documents of that shop and inside title property of productData field, i need to search for the query string input by user. So, i need to use $and condition to lookup for the shop and matching query string inside the title property and then i need to return all of the documents matching the condition.

I tried following but i keep getting empty array.

const regex = RegExp("/.*" + searchQuery + ".*/");
response = await db
  .collection("imported_products")
  .find({
    $and: [
      { shop: shop },
      { "productData.title": { $regex: regex, $options: "i" } },
    ],
  })
  .toArray();

Please guide me to the right path. I am using koa.js for server and next.js for client side.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You don't need to manually put $and as by default all conditions are matched. Also you misspelled someshop as shomeshop in document 2, that might be one of the reasons of getting an empty array. You also don't need to use .* in RegExp("/.*" + searchQuery + ".*/") as by default if the text matches any part of the input, the regex will return true and captures the first match. Passing a regex with the operator $regex is also unnecessary. My advice is using searchQuery.replace(/\s+/g, ' ') instead. The example below should work.

db.collection("imported_products").find({
  shop: shop,
  "productData.title": {
    $regex: searchQuery,
    $options: "i"
  }
})

You can test the query here.

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!