Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think using Apache Solr or ElasticSearch you get more flexibility and performance, but this is supported using <a href="http://www.mongodb.org/display/DOCS/Aggregation+Framework" rel="noreferrer">Aggregation Framework</a>.</p> <p>The main problem using MongoDB is you have to query it N Times: First for get matching results and then once per group; while using a full text search engine you get it all in one query.</p> <p><strong>Example</strong></p> <pre><code>//'tags' filter simulates the search //this query gets the products db.products.find({tags: {$all: ["tag1", "tag2"]}}) //this query gets the size facet db.products.aggregate( {$match: {tags: {$all: ["tag1", "tag2"]}}}, {$group: {_id: "$size"}, count: {$sum:1}}, {$sort: {count:-1}} ) //this query gets the color facet db.products.aggregate( {$match: {tags: {$all: ["tag1", "tag2"]}}}, {$group: {_id: "$color"}, count: {$sum:1}}, {$sort: {count:-1}} ) //this query gets the brand facet db.products.aggregate( {$match: {tags: {$all: ["tag1", "tag2"]}}}, {$group: {_id: "$brand"}, count: {$sum:1}}, {$sort: {count:-1}} ) </code></pre> <p>Once the user filters the search using facets, you have to add this filter to query predicate and match predicate as follows.</p> <pre><code>//user clicks on "Brand 1" facet db.products.find({tags: {$all: ["tag1", "tag2"]}, brand: "Brand 1"}) db.products.aggregate( {$match: {tags: {$all: ["tag1", "tag2"]}}, brand: "Brand 1"}, {$group: {_id: "$size"}, count: {$sum:1}}, {$sort: {count:-1}} ) db.products.aggregate( {$match: {tags: {$all: ["tag1", "tag2"]}}, brand: "Brand 1"}, {$group: {_id: "$color"}, count: {$sum:1}}, {$sort: {count:-1}} ) db.products.aggregate( {$match: {tags: {$all: ["tag1", "tag2"]}}, brand: "Brand 1"}, {$group: {_id: "$brand"}, count: {$sum:1}}, {$sort: {count:-1}} ) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload