【问题标题】:can we use $gte operator in $project phase using aggregations in MongoDB我们可以在 $project 阶段使用 $gte 运算符在 MongoDB 中使用聚合吗
【发布时间】:2018-10-12 02:43:45
【问题描述】:

这里我想通过聚合获取价格大于 200 的 AC3 手机列表

db.Products.aggregate([{$match:{name:"AC3 Phone"}},{$project:{_id:0,cost:{$price:{$gte:200}}}}]

当我在 mongo shell 中执行上述命令时,这是我得到的错误

"ok" : 0,
"errmsg" : "Unrecognized expression '$price'",
"code" : 168,
"codeName" : "InvalidPipelineOperator"

我的疑问是我们可以在投影阶段使用 $gte 运算符在 MongoDB 中使用聚合,如上所述

【问题讨论】:

  • 不清楚你在这里要求什么。您是否希望退回A。退回价格大于200的手机B。退回所有手机,但如果价格更高,请注明不超过200。哪一个听起来像你想要的? AB
  • 我的情况是A

标签: mongodb aggregation-framework


【解决方案1】:

不,您不能将$gte(聚合运算符)与$project 一起使用,您应该使用$match 运算符检查条件。

有关聚合的更多信息,您可以阅读this link

您也可以通过this answer 进行澄清。

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    相关资源
    最近更新 更多