用新创建的对象ID替换复杂数据结构中的对象ID

问题描述 投票:1回答:2

我有一个可以深层嵌套的数据结构,如下所示:

{
 'field1' : 'id1',
 'field2':{'f1':'id1', 'f2':'id2', 'f3':'id3'},
 'field3':['id1','id2', 'id3' ,' id4'],
 'field4':[{'f1': 'id3', 'f2': 'id4'}, ...]
 .....
}

嵌套可以是任何深度,可以是任何数据结构的排列和组合。

这里id1,id2,id3是使用bson库生成的ObjectId的字符串等价物,并且通过从mongoDB查询获得记录。我想替换这些id的所有出现,即; id1,id2 ...用新创建的。

替换必须是这样的,id1必须被所有地方的新id替换为新创建的id,并且其他ID保持相同。

为了清楚上述:如果id5是新生成的id,那么id5必须出现在id1出现的所有地方,依此类推。

这是我做上述的解决方案:

import re
from bson import ObjectId
from collections import defaultdict
import datetime  


class MutableString(object):
'''
class that represents a mutable string
'''
def __init__(self, data):
    self.data = list(data)
def __repr__(self):
    return "".join(self.data)
def __setitem__(self, index, value):
    self.data[index] = value
def __getitem__(self, index):
    if type(index) == slice:
        return "".join(self.data[index])
    return self.data[index]
def __delitem__(self, index):
    del self.data[index]
def __add__(self, other):
    self.data.extend(list(other))
def __len__(self):
    return len(self.data)


def get_object_id_position_mapping(string):
    '''
    obtains the mapping of start and end positions of object ids in the record from DB
    :param string: string representation of record from DB
    :return: mapping of start and end positions of object ids in record from DB (dict)
    '''
    object_id_pattern = r'[0-9a-f]{24}'
    mapping = defaultdict(list)
    for match in re.finditer(object_id_pattern, string):
        start = match.start()
        end = match.end()
        mapping[string[start:end]].append((start,end))
    return mapping


def replace_with_new_object_ids(mapping, string):
    '''
    replaces the old object ids in record with new ones
    :param mapping: mapping of start and end positions of object ids in record from DB (dict)
    :param string: string representation of record from DB
    :return:
    '''
    mutable_string = MutableString(string)
    for indexes in mapping.values():
        new_object_id = str(ObjectId())
        for index in indexes:
            start,end = index
            mutable_string[start:end] = new_object_id
    return eval(str(mutable_string))


def create_new(record):
    '''
    create a new record with replaced object ids
    :param record: record from DB
    :return: new record (dict)
    '''
    string = str(record)
    mapping = get_object_id_position_mapping(string)
    new_record = replace_with_new_object_ids(mapping, string)
    return new_record 

简而言之,我将字典转换为字符串,然后替换了ID并完成了工作。

但我觉得这绝对不是最好的方法,因为如果我没有合适的导入(在这种情况下是日期时间),eval()会失败,而且我可能没有对象类型的信息(例如datetime等) 。)在DB中预先。

我甚至尝试了这里描述的nested_lookup方法https://github.com/russellballestrini/nested-lookup/blob/master/nested_lookup/nested_lookup.py

但是不能让它以我想要的方式工作。有一个更好的方法吗?

注意:效率不是我的担忧。我想要的是自动化用新的替换这些id的过程,以节省手动操作的时间。

编辑1:我将调用create_new(),并将从MongoDB获取的记录作为其参数

编辑2:结构可以有其他对象,如datetime作为值Eg:

 {
 'field1' : 'id1',
 'field2':{'f1':datetime.datetime(2017, 11, 1, 0, 0), 'f2':'id2', 'f3':'id3'},
 'field3':['id1','id2', 'id3' ,' id4'],
 'field4':[{'f1': 'id3', 'f2': datetime.datetime(2017,11, 1, 0 , 0)}, ...]
 .....
}

其他对象必须不受影响,只能更换ID

python mongodb replace objectid
2个回答
2
投票

您可以使用递归函数深入查看嵌套在输入数据结构中的字符串。

def replace_ids(obj, new_ids=None):
  if new_ids is None:
    new_ids = {}
  if isinstance(obj, dict):
    return {key: replace_ids(value, new_ids) for key, value in obj.items()}
  if isinstance(obj, list):
    return [replace_ids(item, new_ids) for item in obj]
  if isinstance(obj, str):
    if obj not in new_ids:
      new_ids[obj] = generate_new_id()
    return new_ids[obj]
  return obj

generate_new_id是一个应该确定如何生成新ID的函数。


0
投票

在michaelrccurtis的帮助下,我可以做到以下几点:

from bson import ObjectId
import datetime


def replace_ids(obj, new_ids=None):
  if new_ids is None:
    new_ids = {}
  if isinstance(obj, dict):
    return {key: replace_ids(value, new_ids) for key, value in obj.items()}
  if isinstance(obj, list):
    return [replace_ids(item, new_ids) for item in obj]
  if isinstance(obj, str):
    if obj not in new_ids:
      new_ids[obj] = generate_new_id(obj)
    return new_ids[obj]
  if isinstance(obj, ObjectId):
    return ObjectId()
  return obj


def generate_new_id(obj):
  if is_valid_objectid(obj):
      return str(ObjectId())
  return obj


def is_valid_objectid(objid):
  if not objid:
      return False
  obj = ObjectId()
  return obj.is_valid(objid)


a = {'_id':ObjectId('5a37844dcf2391c87fb4f845'),
     'a':'5a37844dcf2391c87fb4f844',
     'b':[{'a':'5a37844dcf2391c87fb4f844', 'b':'ABCDEFGH'},{'a':'5a37844dcf2391c87fb4f846', 'b':'abc123456789111111'}],
     'c':['5a37844dcf2391c87fb4f846','5a37844dcf2391c87fb4f844','5a37844dcf2391c87fb4f847'],
     'd':datetime.datetime(2017,11,1,0,0)
    }

b = replace_ids(a)
print(b)

输出:

{ '_id': ObjectId('5a380a08147e37122d1ee7de'), 
  'a': '5a380a08147e37122d1ee7e2', 
  'c': ['5a380a08147e37122d1ee7e0', '5a380a08147e37122d1ee7e2', 
       '5a380a08147e37122d1ee7e4'], 
  'b': [{'b': 'ABCDEFGH', 'a': '5a380a08147e37122d1ee7e2'}, {'b': 
        'abc123456789111111', 'a': '5a380a08147e37122d1ee7e0'}], 
  'd': datetime.datetime(2017, 11, 1, 0, 0)
}

注意:答案可能因机器上的ID生成而有所不同。

向michaelrccurtis大声喊出一个惊人的答案

© www.soinside.com 2019 - 2024. All rights reserved.