Mongo从多字段列抽象字段

我希望能够计算每个州的经销商数量。我遇到的麻烦是我的城市/州/邮编全部在一列中。所以,我需要一种方法来从中抽象出唯一的状态。Mongo从多字段列抽象字段

这是我的一个结果的例子。

"_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

这是我写的聚合组代码,但我希望它只使用状态。

db.car.aggregate([ 

{"$group" : {_id:"$City/State/Zip", count:{$sum:1}}}

])

那么,我将如何从City/State/Zip抽象出状态,并在我的聚合中使用它?

回答:

你可以试试这个。

db.car.aggregate([ 

{ $project : { city_state : { $split: ["$City/State/Zip", " "] } } },

{ $unwind : "$city_state" },

{ $match : { city_state : /[A-Z]{2}/ } },

{ $group : {_id:{ "state" : "$city_state" }, count:{$sum:1}}}

])

以上是 Mongo从多字段列抽象字段 的全部内容, 来源链接: utcz.com/qa/258284.html

回到顶部