Reputation: 481
I am trying write a query in mongo that will convert Totalfleetunits into an integer and then selects all the documents where Totalfleetunits is greater than 1000. Some of the Totalfleetunit fields have characters in them, so it should ignore these documents and only consider fields that can be converted into integers. I am running into many issues:
This is my code so far. What am I missing?
db.car.find($and: [{parseInt(Totalfleetunits) : {$gte: 1000} }, {Totalfleetunits : {$type: 16}}])
These are the errors I recieve.
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: ).
Here is an example of some of the documents within my 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" : "?"
}
The output I would like is for it to produce the total count of all the dealership that have over 1000 plus total fleet units.
Upvotes: 0
Views: 62
Reputation: 481
Thanks for all the suggestions. I solved my problem. I wrote a for loop to parse all Totalfleetunits into integers. Then my query works fine.
//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()
Upvotes: 1