当前位置:网站首页>使用pymongo,将MongoDB生成的ObjectId类型数据与字符串之间的相互转化
使用pymongo,将MongoDB生成的ObjectId类型数据与字符串之间的相互转化
2022-08-08 15:34:00 【呆萌的代Ma】
对于生成的id,直接用str()
即可转为字符串,而对于字符串,使用bson.objectid.ObjectId
将字符串转为ObjectId
类型
示例代码
from bson.objectid import ObjectId
str_id = "62ef8ac55e820c5d091e7380"
object_id = ObjectId(str_id) # 字符串转ObjectID
print(object_id, type(object_id))
string_id = str(object_id) # ObjectID转字符串
print(string_id, type(string_id))
得到结果:
62ef8ac55e820c5d091e7380 <class 'bson.objectid.ObjectId'>
62ef8ac55e820c5d091e7380 <class 'str'>
边栏推荐
猜你喜欢
随机推荐
JS-BOM-for, if (string to case)
CS231n:6 训练神经网络(二)
EasyExcel导入校验必填项不能为空
分布式服务治理
leetcode/删除链表的倒数第n个结点
761. 特殊的二进制序列 : 经典构造题
sqllabs 1~6通关详解
web-sql注入
Install Update(Patches) on ESXi
5G NR RRC连接控制
Kubernetes-Basics-Common Commands
What is low-code development?Is everyone really optimistic about low-code development?
Superset 1.2.0 installation
Share these new Blender plugins that designers must not miss in 2022
携手数字创新 共筑国产生态 7月份AntDB与5款产品完成互认证
【Unity入门计划】制作RubyAdventure02-处理瓦片地图&碰撞
Common regularization methods in deep learning (Regularization) and detailed explanation of WeightDecay parameters in optimizers
Redis RDB分析系统
兆骑科创赛事服务平台对接,海内外高层次人才引进
【愚公系列】华为云云数据库MySQL的体验流程|【华为云至简致远】