简体   繁体   English

如何在 GET 请求中从 mongodb 过滤和 select 特定字段?

[英]How to filter and select specific fields from mongodb in a GET request?

I have just started working with mongoose/mongodb and node.js to build the backend of a react native app I am working on.我刚刚开始使用 mongoose/mongodb 和 node.js 来构建我正在开发的 react native 应用程序的后端。 I'm getting fairly comfortable with querying documents in the database from node but how do I set those "filters" in a GET request (using fetch) from the front end?我对从节点查询数据库中的文档感到相当满意,但是如何在前端的 GET 请求(使用 fetch)中设置这些“过滤器”?

If a user applies a filter to a list to only return products of a certain price, how do I get that filter from the client side over to the server side so node can make the proper query on the database?如果用户将过滤器应用于列表以仅返回特定价格的产品,我如何将该过滤器从客户端获取到服务器端,以便节点可以对数据库进行正确的查询?

Same question for if I only want to return specific fields as opposed to returning every field in a mongodb document.同样的问题是我是否只想返回特定字段而不是返回 mongodb 文档中的每个字段。 How can I let the backend know what fields to return from the client side?如何让后端知道从客户端返回哪些字段?

Hopefully that all makes sense, like I said I'm very new to both mongo and node so I apologize if my explanations are not the best.希望这一切都有意义,就像我说的我对 mongo 和 node 都很陌生,所以如果我的解释不是最好的,我深表歉意。

Thank you to anyone who can help!感谢任何可以提供帮助的人!

What you're looking for is a query language - a syntax for conveying the parameters of a query to a server.您正在寻找的是一种查询语言- 一种用于将查询参数传送到服务器的语法。 There are several options.有几种选择。

Some sites decide to only support simple queries.一些网站决定只支持简单的查询。 The simplest query language, in this case, is the GET query string.在这种情况下,最简单的查询语言是 GET 查询字符串。 For instance, if you're building an on-line store, your customers will typically need to search for products by name and by category, and so you may end up with two query string parameters that you support in URLs, like this:例如,如果您正在建立一个在线商店,您的客户通常需要按名称和类别搜索产品,因此您最终可能会得到两个您在 URL 中支持的查询字符串参数,如下所示:
/products?category=clothes&search=t-shirt

Then, your application must process each of these parameters independently - your back-end must know how to turn this into a MongoDB query.然后,您的应用程序必须独立处理这些参数中的每一个 - 您的后端必须知道如何将其转换为 MongoDB 查询。 For example:例如:

products.find({
  category: { $eq: req.query.category },
  $text: { $search: req.query.search }
  // similarly, you could accept params such as "pricemin" and "pricemax"
  //  and construct a { $gt, $lt } query
});

For more complex systems or applications that require more flexibility, you can allow the client to construct a complete query on their own and pass that to the back-end for evaluation.对于需要更大灵活性的更复杂的系统或应用程序,您可以允许客户端自行构建完整的查询并将其传递给后端进行评估。 Multiple solutions exist that let you achieve this - GraphQL is a popular choice lately due to its good tooling support and ease of use.存在多种解决方案可让您实现这一目标 - GraphQL因其良好的工具支持和易用性而成为最近的流行选择。 Another well-known solution is OData, which is used in some high-profile APIs, such as Microsoft Graph API .另一个广为人知的解决方案是 OData,它用于一些备受瞩目的 API,例如Microsoft Graph API

If using an advanced query language, your back-end will always need to translate between the format that's sent to the server and the format that the database speaks.如果使用高级查询语言,您的后端将始终需要在发送到服务器的格式和数据库使用的格式之间进行转换。 The simplest, non-generic example is the code snippet above - though batteries-included solutions will normally come with their own translators, so you may not need to write any code yourself if a package exists that does that for you.最简单的非通用示例是上面的代码片段 - 尽管包含电池的解决方案通常会附带自己的翻译器,因此如果 package 存在,您可能不需要自己编写任何代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM