pymongo 使用记录
Translation pending: this page is scaffold content and keeps original Chinese content for reference.
近期项目开始使用 pymongo,遇到一对坑。
pymongo 使用过程中的坑
0x0 Question 1: 插入自动生成 _id
1 | db.collection.insert(array) |
均会使 array 生成 mongodb 中的自增 ObjectId ,且使用
1 | for row in array: |
无法进行删除!造成后续更新过程中的困扰。
Solution
由于 python 在传参过程中属于地址传递,所以会造成上述问题。可使用copy.deepcopy()生成副本后再进行插入操作。
1 | ... |
这样可以避免 array 中由于插入操作生成 _id 字段,导致后期更新等出现问题。
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.
Comments



![[徒步]宁波九龙爱心线](https://i.cuger.cn/b/d3c7f637-f0a4-4cfb-8e2b-23631aaba006.jpg)


![[出差]内蒙草原风光](https://i.cuger.cn/b/0039bc8f-15e8-4039-a286-9fb1d1af9e33.jpg)