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

151
Views
Mongoose text index on array of subdocuments always returns nothing

I have a collection of documents for users to track companies that looks like this:

const mongoose = require("mongoose");

const TrackingListSchema = new mongoose.Schema({
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "users",
        required: true,
        index: true
    },
    email: {
        type: String,
        required: true,
    },
    companies: [{
        country: {
            type: String,
            required: true
        },
        countryCode: {
            type: String,
            required: true
        },
        name: {
            type: String,
            required: true
        }
    }]
});

TrackingListSchema.index({"companies.name": "text"});

module.exports = mongoose.model("trackinglist", TrackingListSchema);

As you can see, I am creating a text index on the "companies.name" field.

However, when I run a query as such:

TrackingList.aggregate([
    {$match: {
        "companies.countryCode": "us",
            $text: {
               $search: "Some Company",
               $caseSensitive: false
            }
        }},
        {$project: {
            _id: 0,
            email: 1,
        }}
    ])

Nothing is returned even though I am sure that at least one tracking list has a company with that name. What am I doing wrong? Am I creating the text index wrong because it is inside an array? I thought I had this working but now I just can't get it to work.

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