X Tutup
Skip to content

Commit 105b550

Browse files
committed
add json
1 parent 51de263 commit 105b550

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

232.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ db指向了数据库mydb,books是这个数据库里面的一个集合(类似
284284
{u'_id': ObjectId('554f30be65db941152e6df8d'), u'name': u'Bush', u'title': u'java'}
285285
{u'lang': u'python', u'_id': ObjectId('554f0e3cf579bc0767db9edf'), u'author': u'qiwsir', u'title': u'from beginner to master'}
286286
{u'lang': u'english', u'title': u'physics', u'_id': ObjectId('554f28f465db941152e6df8b'), u'author': u'Newton'}
287+
288+
读者如果看到这里,请务必注意一个事情,那就是mongodb中的每个文档,本质上都是“键值对”的类字典结构。这种结构,一经python读出来,就可以用字典中的各种方法来操作。与此类似的还有一个名为json的东西,可以阅读本教程第贰季进阶的第陆章模块中的[《标准库(8)](./227.md)。但是,如果用python读过来之后,无法直接用json模块中的json.dumps()方法操作文档。其中一种解决方法就是将文档中的`'_id'`键值对删除(例如:`del doc['_id']`),然后使用json.dumps()即可。读者也可是使用json_util模块,因为它是“Tools for using Python’s json module with BSON documents”,请阅读[http://api.mongodb.org/python/current/api/bson/json_util.html](http://api.mongodb.org/python/current/api/bson/json_util.html)中的模块使用说明。
287289

288290
**更新**
289291

0 commit comments

Comments
 (0)
X Tutup