我是MongoDB的新手,試圖過濾一個範圍內的價格。我首先需要檢查是否存在特殊價格,而不是僅對特定元素進行過濾。如果確實存在一個特殊的價格,並且它是在從/到目前爲止,它應該被使用,而不是正常價格。MongoDB查詢過濾器價格
你在下面看到的是我對此的嘗試,但不幸的是它不工作。我怎樣才能正確地做到這一點?
問候
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
})
樣品文件:
{
"_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" : "<p>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. </p>\r\n<ul>\r\n <li>65 % polyester</li>\r\n <li>35 % viscose </li>\r\n <li>Buksen skal sendes til rens - Stryges på indersiden, for at holde den skinnende overflade</li>\r\n</ul>",
"da" : "<p>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. </p>\r\n<ul>\r\n <li>65 % polyester</li>\r\n <li>35 % viscose </li>\r\n <li>Buksen skal sendes til rens - Stryges på indersiden, for at holde den skinnende overflade</li>\r\n</ul>"
},
"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
}
}
}
你能否給我們一些樣品文件。 – 2012-03-09 21:01:21
包括在要求的問題:) – Kristian 2012-03-09 21:34:57