Requirement: mongodb 2 collection Fields Difference Comparison to Realize oracle mimus-like Functions.
The documents are as follows:
collection A :
{
"_id" : {
"total_date" : {
"total_date" : "2017-09-13 00:00:00.0",
"stock_group" : "AC_01"
}
},
"cnt" : 72.0
}
collection B:
{
"_id" : {
"total_date" : {
"total_date" : "2017-09-13 00:00:00.0",
"stock_group" : "BB_01"
}
},
"cnt" : 72.0
}
Expected results:
Display: “Stock _ Group”: “AC _ 01” “Stock _ Group”: “BB _ 01”
Can consultation be realized?
Currently MongoDB has not provided an effective operator to support this requirement, but it can be achieved through code (js, python).
In [12]: def flattern(d): ...: def _flattern(src, dst, prefix=''): ...: for k, v in src.items(): ...: key = k if prefix == '' else '{}.{}'.format(prefix, k) ...: if isinstance(v, dict): ...: _flattern(v, dst, key) ...: else: ...: dst[key] = v ...: result = {} ...: _flattern(d, result) ...: return result In [13]: d1 Out[13]: {'_id': {'total_date': {'stock_group': 'AC_01', 'total_date': '2017-09-13 00:00:00.0'}}, 'cnt': 72.0} In [14]: d2 Out[14]: {'_id': {'total_date': {'stock_group': 'BB_01', 'total_date': '2017-09-13 00:00:00.0'}}, 'cnt': 72.0} In [16]: for k1, v1 in flattern(d1).items(): ...: for k2, v2 in flattern(d2).items(): ...: if k1 == k2 and v1 ! = v2: ...: print('k1 => v1: {} => {}'.format(k1, v1)) ...: print('k2 => v2: {} => {}'.format(k2, v2)) ...: ...: k1 => v1: _id.total_date.stock_group => AC_01 k2 => v2: _id.total_date.stock_group => BB_01