它可以建模
const tourSchema = new Schema({
image: { type: String, required: true, trim: true },
numberOfPersons: {
name: { type: String, required: true, trim: true },
description: { type: String, required: true, trim: true },
},
})
const tourModel = mongoose.model("Tour", tourSchema);
当我使用
Select 时const doc = await tourModel.find(
{},
{
image: 1,
numberOfPersons: {
description: 1,
},
}
);
我收到错误
不支持的投影选项:人数:{ 描述:1 }
原因是什么?
您可以使用点表示法来选择 JSON 对象中的字段
const doc = await tourModel.find(
{},
{
'image': 1,
'numberOfPersons.description': 1,
}
);