用 nestjs+mongoose 调用 aws documentdb 报Projection cannot have a mix of inclusion and exclusion.
在使用 NestJS 结合 Mongoose 调用 AWS DocumentDB 时,报错信息 "Projection cannot have a mix of inclusion and exclusion." 指的是查询中的投影(projection)部分不能同时包含字段的包含和排除。
当你在 Mongoose 中构建一个查询,并且试图指定要返回或排除的字段时,通常会使用类似下面的语法:
Model.find({}, { field1: 1, field2: 0 });
上述代码尝试返回 field1
字段,并排除 field2
字段。这在 MongoDB 的某些版本中是不被允许的。通常,你只能选择包括特定字段或排除特定字段,但不能两者兼有。唯一的例外是 _id
字段,你可以在包含其他字段的情况下排除它:
Model.find({}, { field1: 1, _id: 0 }); // 正确
为了解决这个问题,确保你的查询中的投影完全是基于包含或排除,但不要混合使用它们。例如:
// 包含特定字段
Model.find({}, { field1: 1, field2: 1 });
// 排除特定字段
Model.find({}, { field1: 0, field2: 0 });
// 包含 field1 和 field3,排除 _id
Model.find({}, { _id: 0, field1: 1, field3: 1 });
请检查你的代码,确保查询的投影参数遵循上述规则。如果问题仍然存在,请提供具体的查询代码,以便进一步分析问题所在。