将字段转换为整数以在Mongo中执行比较

我正在尝试在mongo中编写一个查询,它将Totalfleetunits转换为一个整数,然后选择Totalfleetunits大于1000的所有文档。一些Totalfleetunit字段中包含字符,所以它应该忽略这些文档,只考虑可以转换为整数的字段。我遇到了很多问题:将字段转换为整数以在Mongo中执行比较

这是我的代码到目前为止。我错过了什么?

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加总船队的所有经销店的总数中的一些文件的例子单位。

回答:

感谢您的所有建议。我解决了我的问题。我写了一个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()

以上是 将字段转换为整数以在Mongo中执行比较 的全部内容, 来源链接: utcz.com/qa/259009.html

回到顶部