https://docs.mongodb.com/manual/reference/operator/aggregation/gte/
,你可以在上面蒙戈DB文檔中看到,$ GTE返回哪個是假的太多數據。
例JSON數據:
{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
查詢用於獲取數據,其中數量大於250:
db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
輸出:
{ "item" : "abc1", "qty" : 300, "qtyGte250" : true }
{ "item" : "abc2", "qty" : 200, "qtyGte250" : false }
{ "item" : "xyz1", "qty" : 250, "qtyGte250" : true }
{ "item" : "VWZ1", "qty" : 300, "qtyGte250" : true }
{ "item" : "VWZ2", "qty" : 180, "qtyGte250" : false }
問題: 那麼我想要數據> qty> 250,但mongo db顯示所有數據,因此當記錄數量如此之高時,網站變得如此緩慢。
我在使用mongoid的rails上使用ruby,並且我有一些查詢需要使用group by子句,所以我必須進行聚合,但這是返回所有數據。 我原來的查詢:完美
data = SomeModel.collection.aggregate([
{"$project" => {
"dayOfMonth" => {"$dayOfMonth" => "$created_time"},
"month" => {"$month" => "$created_time"},
"year" => {"$year" => "$created_time"},
"date_check_gte" => {"$gte" => ["$created_time",start_time]},
"date_check_lte" => {"$lte" => ["$created_time",end_time]},
}},
{"$group" => {
"_id" => { "dayOfMonth" => "$dayOfMonth", "month" => "$month", "year" => "$year"},
"Total" => {"$sum" => 1},
"check_one" => {"$first" => "$date_check_gte"},
"check_two" => {"$first" => "$date_check_lte"}
}},
{"$sort" => {
"Total" => 1
}}
])
組,但是儘管採用GTE和LTE的返回的所有數據。 是否有任何可以完成的工作,以避免虛假數據出現?