Can mongodb implement such operation data?

  mongodb, question

Can mongodb implement such operation data? Please help the bosses say:

For example, there is such a tablecollection-1

{"_ id": objectid ("fajlfkajfa840faff"), "city": "Beijing", "region": "dongcheng"}
 {"_ id": objectid ("fajlfkajfa9r8q9ga"), "city": "Beijing", "region": "Xicheng"}
 {"_ id": objectid ("fajlfkaj948 gfangi"), "city": "Beijing", "region": "sunrise"}
 
 {"_ id": objectid ("fajlfkajfdajhfa7"), "city": "Shanghai", "region": "Huangpu"}
 {"_ id": objectid ("fajlfkajfflja 983f"), "city": "Shanghai", "region": "luwan"}
 {"_ id": objectid ("fajlfkajfa8378fey"), "city": "Shanghai", "region": "Xuhui"}
 
 {"_ id": objectid ("fajlfkajfa48ua0ja"), "city": "Tianjin", "region": "Hedong"}
 {"_ id": objectid ("fajlfkajf8a7yuhf"), "city": "Tianjin", "region": "Hexi"}
 {"_ id": objectid ("fajlfkajfalofa04"), "city": "Tianjin", "region": "peace"}
 
 Note: The actual data are out of order and not sorted by' city' as above.

Question:
1, can you give each the samecityAdd acity_idNumber, after completion like thiscollection-2

{"_ id": objectid ("fajlfkajfa840faff"), "city": "Beijing", "region": "dongcheng", "city_id":"1"}
 {"_ id": objectid ("fajlfkajfa9r8q9ga"), "city": "Beijing", "region": "Xicheng", "city_id":"1"}
 {"_ id": objectid ("fajlfkaj948 gfangi"), "city": "Beijing", "region": "sunrise", "city_id":"1"}
 
 {"_ id": objectid ("fajlfkajfdajhfa7"), "city": "Shanghai", "region": "Huangpu", "city_id":"2"}
 {"_ id": objectid ("fajlfkajfflja 983f"), "city": "Shanghai", "region": "luwan", "city_id":"2"}
 {"_ id": objectid ("fajlfkajfa8378fey"), "city": "Shanghai", "region": "Xuhui", "city_id":"2"}
 
 {"_ id": objectid ("fajlfkajfa48ua0ja"), "city": "Tianjin", "region": "Hedong", "city_id":"3"}
 {"_ id": objectid ("fajlfkajf8a7yuhf"), "city": "Tianjin", "region": "Hexi", "city_id":"3"}
 {"_ id": objectid ("fajlfkajfalofa04"), "city": "Tianjin", "region": "peace", "city_id":"3"}
 
 Note: cities do not need to use the specified' city_id'. after sorting by cities,' city_id' will grow naturally from 1.

2. Can I take out the first article from the same city and save it as a table?collection-3, like this:

{"_ id": objectid ("fajlfkajfa840faff"), "city": "Beijing", "region": "dongcheng", "city_id":"1"}
 {"_ id": objectid ("fajlfkajfdajhfa7"), "city": "Shanghai", "region": "Huangpu", "city_id":"2"}
 {"_ id": objectid ("fajlfkajfa48ua0ja"), "city": "Tianjin", "region": "Hedong", "city_id":"3"}
 
 Note: In fact, it may not be the above three data. Just take out the first one of the same cities in the' collection-2' table.

I don’t quite understand the first requirement. According to the current statement, each city corresponds to a unique one.city_idBut who is in front and who is behindcityIs it in the right order? In this way, there is no ready-made method. You need to find out how many cities you have, and distribute them circularly after sorting them out.city_id, together with requirement 2 can have:

db['collection-1'].aggregate([
 {$ group: {_ id: "$ city", doc: {$ first: "$ $ root"}}}},//get the first article by city grouping
 {$ replacerroot: {newroot: "$ doc"}},//the document originally located in doc replaces the root document
 {$out: "collection-2"} // output to collection-2
 ]);

$first,$$ROOTRefer to relevant documents for usage of. At this timecollection-2There are all the independent ones you needcity, and then assign them a uniquecity_idAnd updated tocollection-1Medium:

let i = 1;
 db['collection-2'].find().collation({locale: 'zh'}).sort({city: 1}).forEach(doc => {
 db['collection-1'].updateMany({city: doc.city}, {$set: {city_id: i++}});
 });

It is important to note that the.collation({locale: 'zh'}), can be sorted according to pinyin, if not specified, it is arranged according to the binary sequence of each Chinese character code, which has no meaning to us (you can try the sequence yourself). This is a new feature supported by MongoDB 3.4.
In addition, if the amount of data is large, please create an appropriate index yourself before proceeding.