根据特定字段和属性过滤mongoose文档

时间:2022-09-11 19:22:21

I'm developing a website using the MEAN stack (MongoDB/Express/Angular/Node).

我正在使用MEAN堆栈(MongoDB / Express / Angular / Node)开发一个网站。

I have a product schema with 12 different fields/properties, including size, color, brand, model, etc. What is the best and most efficient way to filter products, in Angular or on the server-side?And how can i chain the results if the client had selected more than one property?What would that look like?

我有一个包含12个不同字段/属性的产品架构,包括大小,颜色,品牌,型号等。在Angular或服务器端过滤产品的最佳和最有效的方法是什么?我如何链接结果如果客户选择了多个房产?那会是什么样子?

1 个解决方案

#1


1  

Assuming there will be a lot of products, it will be too much to download to the client in order to filter using Angular. It doesn't scale very well. As the list of products gets bigger and bigger, it will be less and less performant. The better way would, generally, be to let MongoDB do the filtering for you. It's very fast.

假设会有很多产品,下载到客户端以便使用Angular进行过滤将会太多。它不能很好地扩展。随着产品列表越来越大,性能越来越低。通常,更好的方法是让MongoDB为您进行过滤。它非常快。

But, you can control the filtering from Angular by posting to the server the filtering term you want on the endpoint used for that method of filtering, for example, using the http module

但是,您可以通过在用于该过滤方法的端点上向服务器发布所需的过滤术语来控制Angular的过滤,例如,使用http模块

http.post('/api/filter/' + methodOfFiltering, { 'term': termtoFilterBy },  function(dataReturned) {
    // use dataReturned to do something with the data
});

Put this in an angular service method, so you can inject it into any of your controllers/components.

将其置于角度服务方法中,以便将其注入任何控制器/组件。

Create an endpoint that will use the method and the keyword in the mongoose query. I'm assuming that you're using Express for your server routes.

创建一个将在mongoose查询中使用该方法和关键字的端点。我假设您使用Express作为服务器路由。

app.post('/api/filter/:method', function(req, res) {

    var method = req.params.method;
    var termToFilterBy = req.body.term;

    productSchema.find({method: termToFilterBy}, function(err, products) {
        res.send(products);
    });

});

Let me know if this helps.

如果这有帮助,请告诉我。

#1


1  

Assuming there will be a lot of products, it will be too much to download to the client in order to filter using Angular. It doesn't scale very well. As the list of products gets bigger and bigger, it will be less and less performant. The better way would, generally, be to let MongoDB do the filtering for you. It's very fast.

假设会有很多产品,下载到客户端以便使用Angular进行过滤将会太多。它不能很好地扩展。随着产品列表越来越大,性能越来越低。通常,更好的方法是让MongoDB为您进行过滤。它非常快。

But, you can control the filtering from Angular by posting to the server the filtering term you want on the endpoint used for that method of filtering, for example, using the http module

但是,您可以通过在用于该过滤方法的端点上向服务器发布所需的过滤术语来控制Angular的过滤,例如,使用http模块

http.post('/api/filter/' + methodOfFiltering, { 'term': termtoFilterBy },  function(dataReturned) {
    // use dataReturned to do something with the data
});

Put this in an angular service method, so you can inject it into any of your controllers/components.

将其置于角度服务方法中,以便将其注入任何控制器/组件。

Create an endpoint that will use the method and the keyword in the mongoose query. I'm assuming that you're using Express for your server routes.

创建一个将在mongoose查询中使用该方法和关键字的端点。我假设您使用Express作为服务器路由。

app.post('/api/filter/:method', function(req, res) {

    var method = req.params.method;
    var termToFilterBy = req.body.term;

    productSchema.find({method: termToFilterBy}, function(err, products) {
        res.send(products);
    });

});

Let me know if this helps.

如果这有帮助,请告诉我。