如何通过mongoose只传递我在GraphQL中需要的数据



通过GraphQL,GraphQL客户端只能过滤他需要的数据。

然而,相比之下,在我使用mongoose的实现中,所有数据都是从数据库中请求的,而不应用投影,因此会花费不必要的处理和带宽。

Query: {
getCustomers: async (parent, args, { models }, info) => {
const Users = await models.User.find({})
return Users
},
},

我如何将mongoose的投影与GraphQL结合使用,只用于请求数据库,只用于我需要的数据?

请参阅文档。通过投影参数查找方法

// 1 to include and 0 to exclude
// Not necessary to explicitly specify _id: 1 to return the _id field. As defult it is included 
models.User.find({},{user.{propertyname}:1, user.{propertyname}:1,.. ...})