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
}
]
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