Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoDB query filter price
    primarykey
    data
    text
    <p>I'm faily new to MongoDB, and trying to filter a price within a range. Instead of just filtering on a specific element, I first need to check if a special price exists. If a special price do exist, and it's within the from/to date, it should be used instead of the normal price.</p> <p>What you see below is my attempt at this, but unfortunately its not working. How can I do this correctly?</p> <p>Best regards </p> <pre><code>db.products.find({   "shops":1,   "categories":59,   "options":{      "$elemMatch":{         "quantity":{            "$gt":0         }      }   },   "$or":[      {         "$and":[            {               "$and":[                  {                     "options.pricing.special.from":{                        "$exists":true                     }                  },                  {                     "options.pricing.special.to":{                        "$exists":true                     }                  }               ]            },            {               "$or":[                  {                     "$and":[                        {                           "options.pricing.special.from":{                              "$gt":{                                 "sec":1331291423,                                 "usec":76000                              }                           }                        },                        {                           "options.pricing.special.to":{                              "$lt":{                                 "sec":1331291423,                                 "usec":76000                              }                           }                        },                        {                           "options.pricing.price":{                              "$gte":0,                              "$lte":0                           }                        }                     ]                  },                  {                     "$and":[                        {                           "options.pricing.special.from":{                              "$lte":{                                 "sec":1331291423,                                 "usec":76000                              }                           }                        },                        {                           "options.pricing.special.to":{                              "$gte":{                                 "sec":1331291423,                                 "usec":76000                              }                           }                        },                        {                           "options.pricing.special.price":{                              "$gte":0,                              "$lte":0                           }                        }                     ]                  }               ]            }         ]      },      {         "$and":[            {               "$and":[                  {                     "options.pricing.special.from":{                        "$exists":false                     }                  },                  {                     "options.pricing.special.to":{                        "$exists":false                     }                  }               ]            },            {               "options.pricing.price":{                  "$gte":0,                  "$lte":0               }            }         ]      }   ] }).sort({   "created_at":-1 }) </code></pre> <p><strong>Sample document:</strong></p> <pre><code>{ "_id" : ObjectId("4f59047d8b446bac05000000"), "created_at" : new Date("Wed, 11 Feb 2009 13:54:09 GMT +01:00"), "modified_at" : new Date("Thu, 08 Mar 2012 20:11:17 GMT +01:00"), "sku" : "K2 - N30052005", "name" : { "en" : "Pants - Nintendo", "da" : "Bukser - Nintendo" }, "description" : { "en" : "&lt;p&gt;Super fed buks fra Designers Remix Collection. De har store lommer\r\nved hofterne, som giver buksen et lækkert ridebuks-look. De har smalle\r\nben, som gør modellen super feminin, samtidig med de er højtaljede.\r\nBrug dem med en top eller skjorte, samt et par lækre stiletter. &lt;/p&gt;\r\n&lt;ul&gt;\r\n &lt;li&gt;65 % polyester&lt;/li&gt;\r\n &lt;li&gt;35 % viscose &lt;/li&gt;\r\n &lt;li&gt;Buksen skal sendes til rens - Stryges på indersiden, for at holde den skinnende overflade&lt;/li&gt;\r\n&lt;/ul&gt;", "da" : "&lt;p&gt;Super fed buks fra Designers Remix Collection. De har store lommer\r\nved hofterne, som giver buksen et lækkert ridebuks-look. De har smalle\r\nben, som gør modellen super feminin, samtidig med de er højtaljede.\r\nBrug dem med en top eller skjorte, samt et par lækre stiletter.&amp;nbsp; &lt;/p&gt;\r\n&lt;ul&gt;\r\n &lt;li&gt;65 % polyester&lt;/li&gt;\r\n &lt;li&gt;35 % viscose &lt;/li&gt;\r\n &lt;li&gt;Buksen skal sendes til rens - Stryges på indersiden, for at holde den skinnende overflade&lt;/li&gt;\r\n&lt;/ul&gt;" }, "url_key" : { "en" : "designers-remix-collection-pants-nintendo", "da" : "designers-remix-collection-bukser-nintendo" }, "categories" : [15, 68, 84], "shops" : [2], "images" : [{ "src" : "designers-remix-collection-bukser-nintendo-1-1.jpg", "alt" : "" }, { "src" : "designers-remix-collection-bukser-nintendo-1-2.jpg", "alt" : "" }, { "src" : "designers-remix-collection-bukser-nintendo-1-3.jpg", "alt" : "" }, { "src" : "designers-remix-collection-bukser-nintendo-1-4.jpg", "alt" : "" }], "options" : [{ "size" : { "da" : "34", "en" : "34" }, "quantity" : 0, "shipping" : { "weight" : 0, "depth" : 0, "height" : 0, "width" : 0 }, "pricing" : { "price" : 129900, "cost" : 48100, "retail" : 0, "vat" : 25, "special" : { "price" : 38900, "from" : new Date("Fri, 01 Jul 2011 00:00:00 GMT +02:00"), "to" : new Date("Sun, 30 Jun 2013 00:00:00 GMT +02:00") }, "savings" : 91000, "pct_savings" : 70.0 }, "sortable_sizes" : { "da" : ["XS"], "en" : ["XS"] } }, { "size" : { "da" : "36", "en" : "36" }, "quantity" : 0, "shipping" : { "weight" : 0, "depth" : 0, "height" : 0, "width" : 0 }, "pricing" : { "price" : 129900, "cost" : 48100, "retail" : 0, "vat" : 25, "special" : { "price" : 38900, "from" : new Date("Fri, 01 Jul 2011 00:00:00 GMT +02:00"), "to" : new Date("Sun, 30 Jun 2013 00:00:00 GMT +02:00") }, "savings" : 91000, "pct_savings" : 70.0 }, "sortable_sizes" : { "da" : ["S"], "en" : ["S"] } }, { "size" : { "da" : "38", "en" : "38" }, "quantity" : 0, "shipping" : { "weight" : 0, "depth" : 0, "height" : 0, "width" : 0 }, "pricing" : { "price" : 129900, "cost" : 48100, "retail" : 0, "vat" : 25, "special" : { "price" : 38900, "from" : new Date("Fri, 01 Jul 2011 00:00:00 GMT +02:00"), "to" : new Date("Sun, 30 Jun 2013 00:00:00 GMT +02:00") }, "savings" : 91000, "pct_savings" : 70.0 }, "sortable_sizes" : { "da" : ["M"], "en" : ["M"] } }, { "size" : { "da" : "40", "en" : "40" }, "quantity" : 0, "shipping" : { "weight" : 0, "depth" : 0, "height" : 0, "width" : 0 }, "pricing" : { "price" : 129900, "cost" : 48100, "retail" : 0, "vat" : 25, "special" : { "price" : 38900, "from" : new Date("Fri, 01 Jul 2011 00:00:00 GMT +02:00"), "to" : new Date("Sun, 30 Jun 2013 00:00:00 GMT +02:00") }, "savings" : 91000, "pct_savings" : 70.0 }, "sortable_sizes" : { "da" : ["L"], "en" : ["L"] } }], "attributes" : { "size" : { "name" : { "da" : "Størrelse", "en" : "Size" }, "type" : "select", "required" : true, "visible" : true, "option" : true, "sortable" : false }, "color" : { "name" : { "da" : "Farve", "en" : "Color" }, "type" : "select", "required" : true, "visible" : true, "option" : false, "sortable" : false, "default" : { "da" : "Sort", "en" : "Black" }, "value" : { "da" : ["Sort"], "en" : ["Black"] } }, "type" : { "name" : { "da" : "Type", "en" : "Type" }, "required" : false, "option" : false, "sortable" : false, "value" : { "da" : "tempType", "en" : "tempType" } }, "manufacturer" : { "name" : { "da" : "Designer", "en" : "Designer" }, "required" : false, "option" : false, "sortable" : false, "value" : { "da" : "Designers Remix Collection", "en" : "Designers Remix Collection" } }, "sortable_colors" : { "name" : { "da" : "Sorterbare farver", "en" : "Sortable Colors" }, "type" : "system", "required" : false, "option" : false, "visible" : false, "sortable" : true, "value" : { "en" : ["Black"], "da" : ["Sort"] } }, "sortable_sizes" : { "name" : { "da" : "Sorterbare størrelser", "en" : "Sortable Sizes" }, "type" : "system", "required" : false, "option" : true, "visible" : false, "sortable" : true } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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