我正在尝试在mongo中编写一个查询,将Totalfleetunits转换为整数,然后选择Totalfleetunits大于1000的所有文档。其中一些Totalfleetunit字段中包含字符,因此它应忽略这些文档并仅考虑可以转换为整数的字段。我遇到了很多问题:
到目前为止,这是我的代码。我错过了什么?
db.car.find($and: [{parseInt(Totalfleetunits) : {$gte: 1000} }, {Totalfleetunits : {$type: 16}}])
这些是我收到的错误。
Error at line 1, position 16: :.
Error at line 1, position 20: parseInt.
Error at line 1, position 28: <missing ')'>.
Error at line 1, position 46: :.
Error at line 1, position 61: }.
Error at line 1, position 62: ,.
Error at line 1, position 95: ].
Error at line 1, position 96: ).
以下是我的mongodb
中的一些文档的示例 "_id" : ObjectId("5a22c8e562c2e489c5df7186"),
"2016rank" : 141,
"Dealershipgroupname" : "Prestige Automotive Group",
"Address" : "20200 E. Nine Mile Road",
"City/State/Zip" : "Saint Clair Shores, MI 48080",
"Phone" : "(586) 773-2369",
"Companywebsite" : "www.prestigeautomotive.com",
"Topexecutive" : "Gregory Jackson",
"Topexecutivetitle" : "president & CEO",
"Totalnewretailunits" : "6,094",
"Totalusedunits" : "1,910",
"Totalfleetunits" : "4,062",
"Totalwholesaleunits" : 44,
"Total_units" : "12,110",
"Total_number_of _dealerships" : 4,
"Grouprevenuealldepartments*" : "$360,658,677",
"2015rank" : "?"
}
{
"_id" : ObjectId("5a22c8e562c2e489c5df7187"),
"2016rank" : 142,
"Dealershipgroupname" : "Bowers Automotive Group",
"Address" : "2146 Chapman Road",
"City/State/Zip" : "Chattanooga, TN 37421",
"Phone" : "(423) 664-5790",
"Companywebsite" : "bowersag.com",
"Topexecutive" : "Bradley Cobb",
"Topexecutivetitle" : "president",
"Totalnewretailunits" : "6,073",
"Totalusedunits" : "5,518",
"Totalfleetunits" : "-",
"Totalwholesaleunits" : "2,360",
"Total_units" : "13,951",
"Total_number_of _dealerships" : 10,
"Grouprevenuealldepartments*" : "$337,435,813",
"2015rank" : "?"
}
我想要的输出是产生超过1000个总车队单位的所有经销商的总数。
答案 0 :(得分:1)
感谢所有建议。我解决了我的问题。我编写了一个for循环来将所有Totalfleetunits解析为整数。然后我的查询工作正常。
//Do conversion first
db.car.find().forEach( function (x) {
x.Totalfleetunits = parseInt(x.Totalfleetunits, 20);
db.car.save(x);
});
//Then run query
db.car.find({ $and: [{Totalfleetunits : {$gte: 1000} }, {Totalfleetunits : {$type: 16}}] }).count()