当前位置:网站首页>使用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'>
边栏推荐
- 增效降本开源节流,2022年技术趋势前瞻(异步编程/容器技术)
- JS - BOM - - can be achieved through calculation or default values
- SAP系统为什么要迁移上云?
- Mx_yolov3环境配置+模型测试训练
- [Unity entry plan] Unity instance - how to protect data members through encapsulation in C#
- 从洞察到决策,一文解读标签画像体系建设方法论丨DTVision分析洞察篇
- 兆骑科创创业赛事活动举办平台,投融资对接,线上直播路演
- Is it safe to open an account in China Galaxy Securities?
- Streamsets Data Collector 3.12
- 面试题 17.05. 字母与数字
猜你喜欢
Superset 1.2.0 installation
sqllabs 1~6通关详解
Notes on the development of kindergarten enrollment registration system based on WeChat applet
Mysql的分布式事务原理理解
Mx_yolov3环境配置+模型测试训练
Iptables防火墙iprange模块扩展匹配规则
企业开发小程序有什么优势?为什么要开发小程序?
C#/VB.NET convert PDF to PDF/X-1a:2001
【Unity入门计划】用双血条方法控制伤害区域减血速度
IBM3650M4的ESXI主机报警“其他主机硬件对象的状态”
随机推荐
Create a 2D array
CS231n:6 训练神经网络(二)
Interview questions 17.05. Letters and numbers
[Unity entry plan] Use the double blood bar method to control the blood loss speed of the damage area
leetcode/delete the nth node from the bottom of the linked list
【Unity入门计划】用双血条方法控制伤害区域减血速度
依赖传递和依赖调解
表实时同步,没有etl 可以用这个吗,从mysql到mysql
1052. The Angry Bookstore Boss
从洞察到决策,一文解读标签画像体系建设方法论丨DTVision分析洞察篇
bzoj3262 Flowers bloom on Mo
瑞吉外卖学习笔记3
有了国产 DevOps 工具 ,还怕数字化转型成本高?
Common regularization methods in deep learning (Regularization) and detailed explanation of WeightDecay parameters in optimizers
跟我一起来学弹性云服务器ECS【华为云至简致远】
分享这些2022设计师们决不能错过的Blender新插件
优雅地实时检测和更新 Web 应用
Mysql的分布式事务原理理解
技术分享 | 接口自动化测试之JSON Schema模式该如何使用?
bzoj2816 [ZJOI2012]网络