Mongodb查询排除了一个字段



我有这个问题,我的查询如下:

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

我的模型是这样的:

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

当我使用上面的查询时,我得到的结果没有location字段。为什么?

我需要注意的是,location在上有一个2dsphere索引

这里有一个输出示例:

[                                                                                                                                                           
{
_id: new ObjectId("619217adf9f5ad76d27d387f"),
name: 'Vienna',
country: 'AT',
score: 1.1
}
]

我发现了问题。它在我的模式中。type是一个保留关键字,但我需要它作为对象关键字

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

现在它工作

最新更新