我对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
}
}
}
答案 0 :(得分:3)
我将价格范围设置为35000到40000,因此它将与特价相匹配。然后我有一个35000到150000的第二个例子,以匹配主要价格。无论如何,希望这会有所帮助。
我认为您缺少的查询的关键部分是“elemMatch”。通过不使用具有嵌入对象的数组作为定价属性,您的模式可能更容易查询。
// testing first part of the $or
var query = {$or:
[{"options":
{
$elemMatch:
{"pricing.special.price":{$exists:true},
"pricing.special.from":{$lt:new Date()},
"pricing.special.to":{$gt:new Date()},
"pricing.special.price":{$gt:35000},
"pricing.special.price":{$lt:40000}
}
}
},
{"options":
{$elemMatch:
{
"pricing.price":{$gt:35000},
"pricing.price":{$lt:40000}
}
}
}]
};
这也匹配。
// testing out of the date range... and increasing the max price
var query = {$or:
[{"options":
{
$elemMatch:
{"pricing.special.price":{$exists:true},
"pricing.special.from":{$lt:new Date("Jan 1, 2014")},
"pricing.special.to":{$gt:new Date("Jan 1, 2014")},
"pricing.special.price":{$gt:35000},
"pricing.special.price":{$lt:150000}
}
}
},
{"options":
{$elemMatch:
{
"pricing.price":{$gt:35000},
"pricing.price":{$lt:150000}
}
}
}]
};