【问题标题】:Find documents in MongoDB by UUID (Convert ObjectId to UUID)通过 UUID 在 MongoDB 中查找文档(将 ObjectId 转换为 UUID)
【发布时间】:2015-06-08 15:07:23
【问题描述】:

我必须通过 UUID 在 MongoDB 中查找文档,但文档是使用 ObjectId 存储的。如何将 UUID 转换为 ObjectId,反之亦然?我使用python和pymongo进行查询处理。

我发现只有一次提到了这个问题:

https://gist.github.com/enaeseth/5768348 -

有一个将 ObjectID 转换为 UUID 的 python 脚本。但它不能正常工作。例如,当处理'52933322e4b0ce16e9596447' 时,它返回'f940bd00-55c3-11e3-a447-00e4b0ce16e9' 而不是'f940bd03-55c3-11e3-a4b0-ce16e9596447'。 (我有相对 UUID 和 ObjectId 的样本要测试)。

您能帮我找到其他解决方案或修改此脚本吗?非常感谢。

【问题讨论】:

  • 为什么需要它成为uuid?您不能将uuid 存储在一个字段中并以类似的方式查询它吗?
  • 我无法修改数据,只能检索。在 mongoDB 中,数据与 ObjectId 一起存储,但我有必须使用的文档 UUID 列表。
  • 这没有任何意义。如果文档使用 ObjectId 作为 _id,您从哪里获得这些 UUID,它们怎么可能与 MongoDB 有任何关系?
  • 数据存储在两个数据库中 - 带有 ObjectIds 的 mongodb 和带有 UUID 的 mysql DB 中。我需要通过合并 MongoDB 和 Mysql DB 数据来检索信息。而且我不知道为什么要使用这种架构。

标签: python mongodb pymongo uuid objectid


【解决方案1】:

将 ObjectId 转换为 UUID

import datetime as dt
from bson import ObjectId
from uuid import UUID


class UTC(dt.tzinfo):
    ZERO = dt.timedelta(0)

    def utcoffset(self, dt):
        return self.ZERO

    def tzname(self, dt):
        return 'UTC'

    def dst(self, dt):
        return self.ZERO

UTC = UTC()
UUID_1_EPOCH = dt.datetime(1582, 10, 15, tzinfo=UTC)
UUID_TICKS_PER_SECOND = 10000000


def unix_time_to_uuid_time(dt):
    return int((dt - UUID_1_EPOCH).total_seconds() * UUID_TICKS_PER_SECOND)


def object_id_to_uuid(object_id):
    """
    Converts ObjectId to UUID

    :param object_id: some ObjectId
    :return: UUID
    """

    str_object_id = str(object_id)

    b = []
    for i in [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22]:
        b.append(int(str_object_id[i:i+2], 16))

    generation_time = ObjectId(str_object_id).generation_time.astimezone(UTC)
    time = unix_time_to_uuid_time(generation_time)
    time |= (b[4] >> 6) & 0x3

    most_sig_bits = str(hex(0x1000 | time >> 48 & 0x0FFF
                            | time >> 16 & 0xFFFF0000
                            | time << 32))[9:]

    least_sig_bits = str(hex(2 << 62
                             | (b[4] & 0x3F) << 56 | (b[5] & 0xFF) << 48
                             | (b[6] & 0xFF) << 40 | (b[7] & 0xFF) << 32
                             | (b[8] & 0xFF) << 24 | (b[9] & 0xFF) << 16
                             | (b[10] & 0xFF) << 8 | b[11] & 0xFF))[2:]

    return UUID('%s-%s-%s-%s-%s' % (most_sig_bits[:8], most_sig_bits[8:12], most_sig_bits[12:16],
                               least_sig_bits[0:4], least_sig_bits[4:]))

将 UUID 转换为 ObjectId

import datetime as dt
from bson import ObjectId
import pytz


def get_timestamp_from_uuid(uuid_object):

    generation_time = dt.datetime.utcfromtimestamp((uuid_object.time - 0x01b21dd213814000) * 100 / 1e9)
    generation_time = generation_time.replace(tzinfo=pytz.UTC)

    return int(generation_time.timestamp())


def get_timestamp_from_object_id(object_id):

    generation_time = object_id.generation_time
    return int(generation_time.timestamp())


def return_decimal_bytes(binary):

    binary = binary[2:]
    rest = len(binary) % 8

    if rest != 0:
        binary = '0'*(8 - rest) + binary
    bytes = {}

    for i in range(0, len(binary)//8):
        bytes[i] = binary[:8]
        binary = binary[8:]

    return bytes


def uuid_to_object_id(uuid_object):
    """
    Converts UUID to ObjectId

    :param uuid_object: UUID
    :return: ObjectId
    """

    uuid_to_bin = bin(int(uuid_object))
    parts_of_uuid = return_decimal_bytes(uuid_to_bin)

    timestamp_of_uuid = hex(get_timestamp_from_uuid(uuid_object))
    timestamp_of_uuid = timestamp_of_uuid.replace('0x', '')

    uuid_time = uuid_object.time

    fourth = int(parts_of_uuid[8], 2) & 0x3F | (uuid_time << 6)
    fourth = str(hex(fourth))[-2:]

    fifth = str(hex(int(parts_of_uuid[9], 2)))
    sixth = str(hex(int(parts_of_uuid[10], 2)))
    seventh = str(hex(int(parts_of_uuid[11], 2)))
    eighth = str(hex(int(parts_of_uuid[12], 2)))
    ninth = str(hex(int(parts_of_uuid[13], 2)))
    tenth = str(hex(int(parts_of_uuid[14], 2)))

    try:
        eleventh = str(hex(int(parts_of_uuid[15], 2)))
    except Exception as e:
        print(e)
        eleventh = str(hex(1))

    def rep(el):
        if len(el) <= 3:
            return el.replace('x', '')
        else:
            return el.replace('0x', '')

    res = rep(fifth) + rep(sixth) + rep(seventh) + rep(eighth) + rep(ninth) + rep(tenth) + rep(eleventh)

    return ObjectId(timestamp_of_uuid + fourth + res)

Refference

【讨论】:

    猜你喜欢
    • 2013-02-06
    • 2015-01-17
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    相关资源
    最近更新 更多