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

79
Views
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 answers
Answer question

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 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!