作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
https://docs.mongodb.com/manual/reference/operator/aggregation/gte/
正如您在上面的 mongo 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 }
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 }
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
}}
])
最佳答案
获取数量大于 250 的数据的“查询”涉及 $match
过滤文档以仅将符合指定条件的文档传递到下一个管道阶段的管道运算符,而不是 $project
您目前正在做的管道:
db.inventory.aggregate([
{ "$match": { "qty": { "$gte": 250 } } }
)
$project
管道(虽然没有必要,因为只使用一个
$match
管道就足够了):
db.inventory.aggregate([
{
"$project": {
"item": 1,
"qty": 1,
"qtyGte250": { "$gte": [ "$qty", 250 ] },
"_id": 0
}
},
{ "$match": { "qtyGte250": true } }
])
关于mongoDB 有没有办法让聚合 $gte 不显示错误数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39344913/
我是一名优秀的程序员,十分优秀!