def merge_collections(a, b, target_id):
    '\n    merge two collections into a single merged collection\n\n    for keys that are in both collections, the value in b will be kept\n\n    Parameters\n    ----------\n    a  the inferior collection (will lose values of shared keys)\n    b  the superior collection (will keep values of shared keys)\n    target_id  str  the name of the key used in b to dereference ids in a\n\n    Returns\n    -------\n    the combined collection.  Note that it returns a collection only containing\n    merged items from a and b that are dereferenced in b, i.e., the merged\n    intercept.  If you want the union you can update the returned collection\n    with a.\n\n    Examples\n    --------\n    >>>  grants = merge_collections(self.gtx["proposals"], self.gtx["grants"], "proposal_id")\n\n    This would merge all entries in the proposals collection with entries in the\n    grants collection for which "_id" in proposals has the value of\n    "proposal_id" in grants.\n    '
    adict = {}
    for k in a:
        adict[k.get('_id')] = k
    bdict = {}
    for k in b:
        bdict[k.get('_id')] = k
    b_for_a = {}
    for k in adict:
        for (kk, v) in bdict.items():
            if (v.get(target_id, '') == k):
                b_for_a[k] = kk
    chained = {}
    for (k, v) in b_for_a.items():
        chained[k] = ChainDB(adict[k], bdict[v])
    return list(chained.values())
merge two collections into a single merged collection

for keys that are in both collections, the value in b will be kept

Parameters
----------
a  the inferior collection (will lose values of shared keys)
b  the superior collection (will keep values of shared keys)
target_id  str  the name of the key used in b to dereference ids in a

Returns
-------
the combined collection.  Note that it returns a collection only containing
merged items from a and b that are dereferenced in b, i.e., the merged
intercept.  If you want the union you can update the returned collection
with a.

Examples
--------
>>>  grants = merge_collections(self.gtx["proposals"], self.gtx["grants"], "proposal_id")

This would merge all entries in the proposals collection with entries in the
grants collection for which "_id" in proposals has the value of
"proposal_id" in grants.
def merge_collections(a, b, target_id):
    '\n    merge two collections into a single merged collection\n\n    for keys that are in both collections, the value in b will be kept\n\n    Parameters\n    ----------\n    a  the inferior collection (will lose values of shared keys)\n    b  the superior collection (will keep values of shared keys)\n    target_id  str  the name of the key used in b to dereference ids in a\n\n    Returns\n    -------\n    the combined collection.  Note that it returns a collection only containing\n    merged items from a and b that are dereferenced in b, i.e., the merged\n    intercept.  If you want the union you can update the returned collection\n    with a.\n\n    Examples\n    --------\n    >>>  grants = merge_collections(self.gtx["proposals"], self.gtx["grants"], "proposal_id")\n\n    This would merge all entries in the proposals collection with entries in the\n    grants collection for which "_id" in proposals has the value of\n    "proposal_id" in grants.\n    '
    adict = {}
    for k in a:
        adict[k.get('_id')] = k
    bdict = {}
    for k in b:
        bdict[k.get('_id')] = k
    b_for_a = {}
    for k in adict:
        for (kk, v) in bdict.items():
            if (v.get(target_id, ) == k):
                b_for_a[k] = kk
    chained = {}
    for (k, v) in b_for_a.items():
        chained[k] = ChainDB(adict[k], bdict[v])
    return list(chained.values())