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

87
Visualizações
Mongodb query excludes a field

I have this problem, my query looks like this:

  public findCitys(city: string) {
    return City.find(
      {
        country: 'AT',
        $text: {
          $search: city,
        },
      },
      {
        score: {
          $meta: 'textScore',
        },
      }
    )
      .sort({
        score: {
          $meta: 'textScore',
        },
      })
      .limit(5)
  }

And my model looks like this:

const citySchema = new mongoose.Schema({
  country: {
    type: String,
  },
  name: {
    type: String,
    index: 'text',
  },
  location: {
    type: String,
    coordinates: [Number],
  },
})

When i use the query above i get the result without the location field. Why is that?

I need to note here that location has an 2dsphere index on it

Here an example output:

[                                                                                                                                                           
  {
    _id: new ObjectId("619217adf9f5ad76d27d387f"),
    name: 'Vienna',
    country: 'AT',
    score: 1.1
  }
]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Well i found the problem. It was in my schema. type is an reserved keyword but i need it as an object key

const citySchema = new mongoose.Schema({
  country: {
    type: String,
  },
  name: {
    type: String,
    index: 'text',
  },
  location: {
    type: {
       type: String
    },
    coordinates: [Number],
  },
})

Now it works

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