【问题标题】:Mongo greater than comparison returns 0Mongo 大于比较返回 0
【发布时间】:2017-12-02 13:23:17
【问题描述】:

我正在尝试计算所有车队总单位数超过 1000 的汽车经销商。这是我编写的代码,但是它返回 0,我知道这个数据集中有很多记录超过 1000 个。

db.Car_Dealership.find({Totalfleetunits : {$gte: 1000} }).count()

这是我的数据库中的一个示例,两条记录的车队总数都超过 1000。任何想法为什么它返回 0?

     "_id" : ObjectId("5a203ab0b9574375830354d4"),
        "2016rank" : 6,
        "Dealershipgroupname" : "Hendrick Automotive Group",
        "Address" : "6000 Monroe Road",
        "City/State/Zip" : "Charlotte, NC 28212",
        "Phone" : "(704) 568-5550",
        "Companywebsite" : "www.hendrickauto.com",
        "Topexecutive" : "Rick Hendrick",
        "Topexecutivetitle" : "chairman",
        "Totalnewretailunits" : "117,946",
        "Totalusedunits" : "88,458",
        "Totalfleetunits" : "4,646",
        "Totalwholesaleunits" : "56,569",
        "Total_units" : "267,619",
        "Total_number_of _dealerships" : 103,
        "Grouprevenuealldepartments*" : "$8,551,253,132",
        "2015rank" : 6
}
{
        "_id" : ObjectId("5a203ab0b9574375830354d5"),
        "2016rank" : 5,
        "Dealershipgroupname" : "Sonic Automotive Inc.?",
        "Address" : "4401 Colwick Road",
        "City/State/Zip" : "Charlotte, NC 28211",
        "Phone" : "(704) 566-2400",
        "Companywebsite" : "www.sonicautomotive.com",
        "Topexecutive" : "B. Scott Smith",
        "Topexecutivetitle" : "CEO",
        "Totalnewretailunits" : "134,288",
        "Totalusedunits" : "119,174",
        "Totalfleetunits" : "1,715",
        "Totalwholesaleunits" : "35,098",
        "Total_units" : "290,275",
        "Total_number_of _dealerships" : 112,
        "Grouprevenuealldepartments*" : "$9,731,778,000",
        "2015rank" : 4

【问题讨论】:

  • 您可以在这里发布您的架构吗?
  • 如何比较字符串和数字? mongodb 不是 javascript
  • 是的,这就是问题所在。但无论如何,我们可以在query 中为您提供javascript。 :)
  • 是的,但这是您拥有的超慢选项,请尝试更新您的架构

标签: mongodb


【解决方案1】:

这是因为Totalfleetunits 的值是stringType

现在要解决您的问题,您必须做出选择。

选项 1:

您可以将Totalfleetunits 的架构更改为Number 的类型,并将所有文档Totalfleetvalues 的值从字符串更改为数字。喜欢,

“Totalfleetunits”:“4,646”需要更改为“Totalfleetunits” :“4646”

选项 2:

您可以在查询中使用javascript,首先从您的值中删除,,然后检查Totalfleetunitsgreater than or equal to ( >= ) 值。只需要更改我下面给出的一行代码。

db.Car_Dealership.find("this.Totalfleetunits.replace(',','') >= 1000").count()

【讨论】:

  • 我收到一条错误消息,说替换不是函数
  • 是的,我可以在 mongo 中编写 javascript
  • 不,它说 this.Totalfleetunits.replace 不是函数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2013-05-16
相关资源
最近更新 更多