我希望能够计算每个州的经销商数量。我遇到的麻烦是我将City / State / Zip全部放在一列中。所以,我需要一种方法来从中抽象出状态。
这是我的一个结果的例子。
"_id" : ObjectId("5a22c8e562c2e489c5df710a"),
"2016rank" : 22,
"Dealershipgroupname" : "West-Herr Automotive Group Inc.",
"Address" : "3552 Southwestern Blvd.",
"City/State/Zip" : "Orchard Park, NY 14127",
"Phone" : "(716) 926-7052",
"Companywebsite" : "www.westherr.com",
"Topexecutive" : "Scott Bieler",
"Topexecutivetitle" : "president",
"Totalnewretailunits" : "29,486",
"Totalusedunits" : "20,525",
"Totalfleetunits" : 302,
"Totalwholesaleunits" : "23,694",
"Total_units" : "74,007",
"Total_number_of _dealerships" : 25,
"Grouprevenuealldepartments*" : "$1,805,200,100",
"2015rank" : 20
这是我写的聚合组代码,但我希望它只使用State。
db.car.aggregate([
{"$group" : {_id:"$City/State/Zip", count:{$sum:1}}}
])
那么我如何从City / State / Zip中抽出只有状态并在我的聚合中使用它呢?
答案 0 :(得分:0)
你可以试试这个。
<html>
<script>
// Greetings
var words = ['Hello', 'Hey', 'What\'s up!?'];
function randomAlert() {
// Index for picking the greeting you want to show
var alertIndex;
// Generates a number between 0 and 1
var randomValue = Math.random();
// 50% chance of 'Yes'
if (randomValue < 0.5) {
alertIndex = 0;
}
// 50% chance of 'No'
else if(randomValue < 0.5) {
alertIndex = 1;
}
alert(words[alertIndex])
}
</script>
<button onClick="randomAlert()">Click me!</button>
</html>