Brief introduction
One of the most common types of business in Express Logistics systems is the inquiry and recording of orders. Orders are characterized by the fact that the order data needs to be updated at any time as the delivery process progresses. The data structure needs to be flexible, which is very much in line with the document model, and MONGODB supports the GIS function, which is very suitable for MongoDB to support the logistics business. And MongoDB has the ability to sharding, while the logistics industry orders relatively independent, the operation of the order is very few, and on a single order, update additional operations will be more, such as logistics transfer processing. So the logistics business model is very well matched to MongoDB. Here is a virtual demo, for reference, features:
- The array structure of the MongoDB document
- TTL index, automatic expiration of historical data
- Compound index, multi-criteria Query Index
- Partial Indexes, conditional index, index only valid data, reduce the footprint of the index
- MongoDB GIS function, support Geojson standard, can access third-party system to process GEO information.
Data structure definition
{"_ID":<String>,//Order ID "status":<String>,//Order status, shipping,deliveried, etc. "Order_image_url":<String>,//Order picture Information "Create_date":<Isodate>,//Order creation date "from": {//Shipping Information "City":<String>, "Address": <string>, "name": <string>, "Phone": < String>, "location": <geojson>}, "delivery": {//Receipt information "City": <String>, "Address":
<
String>, "name": <string>, "Phone": <string>, "location": < geojson>}, "Details": [//logistic details, array structure {"Action": "reviced",//Logistic action "operator": "Express little Brother 1th",//Operator "date": < ; isodate>} ...]}
For example:
{ "_id ":"E123456789", "Status ":"Delivering", "Create_date ":Isodate ("2016-06-21t09:00:00+08:00"), "Order_image_url ":"Http://oss/xxxxx.jpg", "From ":{ "City ":"Hangzhou", "Address ":No. No. 969 Wen Yi Xi Lu, "Name ":"Xiao Wang", "Phone ":"18657112345", "Location ":{ "Type ":"Point", "Coordinates ":[120,30]}}, "Delivery ":{ "City ":"Beijing", "Address ":"Chaoyang District", "Name ":"The Chaoyang Crowd", "Phone ":"18601011011", "Location ":{ "Type ":"Point", "Coordinates ":[116,39]}}, "Details ":[ { "Action ":"Reviced", "Operator ":"Express Little Brother 1th", "Date ":Isodate ("2016-06-21t09:00:00+08:00")}, {"Action ":"Shipping", "Station ":"Hangzhou-airport", "Date ":Isodate ("2016-06-22t01:00:00+08:00")}, {"Action ":"Shipping", "Station ": " Beijing-airport ","date ": isodate (" 2016-06-22t07:00:00+08:00 ")}, {"action ": "Shipping", "Station": "Chaoyang-station", "date": isodate ("2016-06-22t15:00:00+08:00")}, { "action": "Delivering", "operator": "express little Brother 2nd", "date": isodate ("2016-06-23t10:0 0:00+08:00 ")}]}
A few points to note:
- Using the GEO feature, the Geojson standard is used, but the coordinate standard problem should be paid attention to in the coordinate system;
- Time processing, the logistics industry will involve internationalization, so in strict accordance with the ISO standard, define time, CN
+80:00
;
- The Order details make good use of the document's array characteristics;
- Express orders are unique and can be used as the primary key for MongoDB;
Insert the document after the collection:
> Db.order.find (). Pretty () {"_id": "E123456789", "status": "Delivering", "Create_date": Isodate ("2016-06-21 t01:00:00z ")," Order_image_url ":" Http://oss/xxxxx.jpg "," from ": {" City ":" Hangzhou "," Address ": No. No. 969 Wen Yi Xi lu, "name": "Xiao Wang", "Phone": "18657112345", "location": {"type": "Point", "Coordinates": [[+]}}, "delivery": {"City" : "Beijing", "Address": "Chaoyang", "name": "Chaoyang Crowd", "Phone": "18601011011", "location": { "Type": "Point", "coordinates": [Deta Ils ": [{" Action ":" reviced "," Operator ":" Express little Brother 1th "," date ": Isodate (" 2016-06- 21t01:00:00z ")}, {" Action ":" Shipping "," station ":" Hangzhou-airport ", "Date": Isodate ("2016-06-21t17:00:00z ")}, {" Action ":" Shipping "," station ":" Beijing-airport ", "Date": Isodate ("2016-06-21t23:00:00z")}, {"Action": "Shipping", "station": "Cha Oyang-station "," date ": Isodate (" 2016-06-22t07:00:00z ")}, {" Action ":" Delivering ", "Operator": "Express Little Brother 2nd", "date": Isodate ("2016-06-23t02:00:00z")}]}
Data manipulation
Logistics Express Order Modification is mainly the query and information two kinds, mainly introduce the two kinds:
Order Information Inquiry, the most common operation, the user's order inquiry:
DB. OrderFind ({_id:"E123456789"});
Sometimes need to do information statistics, according to the status of query:
Db.order.find ({"Status": "Delivering", "delivery.city": "Beijing", "delivery.address": "Chaoyang District"});
Logistics status updates, you need to update the corresponding orders, MongoDB directly $push
past can be:
DB. Order_id:{$push: { details: {"action":isodate ("2016-06-23t13:00:00+ 8:00am ") }})
Index creation
_id
indexes, which exist by default, do not need to be recreated, and can be used with the sharding structure when the amount of data is large Hash(_id)
.
TTL index, field create_date
, automatically cleans up data after 180 days:
Db.order.createIndex ({"Create_date": 1}, {"Expireafterseconds": 15552000})
Position and status index, in order to be able to quickly process "a place not processed orders" query, this is a multi-criteria query, so is a composite index, the status
field is placed in front, because most of the query will depend on the State field
Db.order.createIndex ({"Status": 1, "delivery.city": 1, "delivery.address": 1})
In this demo, there is another way to speed up the query is to create a partial indexes index that contains only the specified state. status
For example, must be delivering
added to the index, effectively control the size of the index, speed up the query.
Db.order.createIndex ({"delivery.address": 1},{partialfilterexpression:{' status ': {$eq:" Delivering "} }})
MongoDB GIS
MongoDB follows the Geojson specification, the description of the object is described by a Type field describing the Geojson type, and the coordinates field describes the spatial information.
{type: "<geojson type>", Coordinates: <coordinates>}
The coordinates is an [longitude, latitude]
array type. Another notable concern is that the WGS84 standard is used by MongoDB Geo. WGS84 is also an international standard, China used the famous Mars coordinate GCJ-02, there is a set of Baidu coordinates BD09, three of the coordinate conversion can refer to the appendix related links.
Appendix
GeoJSON
- https://docs.mongodb.com/manual/reference/geojson/
- Http://geojson.org/geojson-spec.html
- https://docs.mongodb.com/manual/reference/glossary/#term-WGS84
MongoDB Geo Index
- Https://docs.mongodb.com/manual/core/2dsphere/
- https://docs.mongodb.com/manual/reference/glossary/#term-legacy-coordinate-pairs
GeoHack
- Https://tools.wmflabs.org/geohack/geohack.php?pagename=hangzhou¶ms=30\_15\_n\_120\_10\_e\_type:city ( 9018000) _region:cn-33_
- Https://tools.wmflabs.org/geohack/geohack.php?pagename=beijing¶ms=39\_55\_n\_116\_23\_e\_type:city ( 21700000) _region:cn-11_
- Https://en.wikipedia.org/wiki/world\_geodetic\_system
Geo Datum
- Https://github.com/wandergis/coordtransform
MongoDB helps quickly build a logistics order system