Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

80
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda