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

221
Views
MongoDB Full Text Search in an Array of objects

I have a Schema like this:

const AyoSchema = new Schema({
images: Array, 

Images is an array in which objects are stored in such format:

{
id: a uuid here, 
name: a string here,
url: a url here, 
topic: a string here 
}

What i want to do is, I want to search the name property of all objects in images array without invloving much work of indexes,

How shall i do?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

There are a couple of ways you could handle it. If you want to return only the matching documents, it's a little more complicated.

I assume you want to return only the matched items. In order to do so you will need to use the aggregation pipeline, specifically the $unwind and $match operators.

Check out a live demo here

Consider the following:

Database

[
  {
    _id: ObjectId("111111111111111111111111"),
    images: [
      {
        id: ObjectId("123123224454323123121314"),
        name: "foo",
        url: "cdn.domain.com/images/foo",
        topic: "lorem ipsum"
      },
      {
        id: ObjectId("222123224454323123121314"),
        name: "bar",
        url: "cdn.domain.com/images/bar",
        topic: "lorem ipsum"
      },
      {
        id: ObjectId("333323224454323123121314"),
        name: "baz",
        url: "cdn.domain.com/images/baz",
        topic: "lorem ipsum"
      }
    ]
  },
  {
    _id: ObjectId("222222222222222222222222"),
    images: [
      {
        id: ObjectId("888823224454323123121314"),
        name: "text",
        url: "cdn.domain.com/images/text",
        topic: "lorem ipsum"
      },
      {
        id: ObjectId("999993224454323123121314"),
        name: "foo",
        url: "cdn.domain.com/images/pic",
        topic: "lorem ipsum"
      }
    ]
  }
]

Query

db.collection.aggregate([
  {
    $unwind: "$images"
  },
  {
    $match: {
      "images.name": "foo" // <-- replace "foo" with your query
    }
  }
])

Result

[
  {
    "_id": ObjectId("111111111111111111111111"),
    "images": {
      "id": ObjectId("123123224454323123121314"),
      "name": "foo",
      "topic": "lorem ipsum",
      "url": "cdn.domain.com/images/foo"
    }
  },
  {
    "_id": ObjectId("222222222222222222222222"),
    "images": {
      "id": ObjectId("999993224454323123121314"),
      "name": "foo",
      "topic": "lorem ipsum",
      "url": "cdn.domain.com/images/pic"
    }
  }
]

Update

To include regex.

Live demo

Query

db.collection.aggregate([
  {
    $unwind: "$images"
  },
  {
    $match: {
      "images.name": {
        "$regex": "fo*"
      }
    }
  }
])
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!