Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda