python与mongo的find()问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
hilenlai
V2EX    Python

python与mongo的find()问题

  •  
  •   hilenlai 2013-03-31 19:23:49 +08:00 4507 次点击
    这是一个创建于 4652 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码:
    import os
    import sys
    import pymongo
    import json
    from pymongo import Connection
    con = Connection()
    db = con.test
    collection = db.data
    result = collection.find()
    for value in result:
    print value
    print result

    ------------
    for 循环得到的结果是:
    {u'age': 25.0, u'_id': ObjectId('5157f624d86141fb9fbd9d08'), u'name': u'zhangsan', u'sex': 1.0}

    {u'age': 25.0, u'_id': ObjectId('5157f625d86141fb9fbd9d09'), u'name': u'zhangsan', u'sex': 1.0}

    {u'age': 25.0, u'_id': ObjectId('5157f625d86141fb9fbd9d0a'), u'name': u'zhangsan', u'sex': 1.0}

    ------------
    result的结果是:
    <pymongo.cursor.Cursor object at 0xb6f0de6c>



    请问如何讲result的结果 直接打印出来 json 序列化呢?
    像这种格式:
    { "people": [{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
    { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},
    { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
    ]}
    11 条回复    1970-01-01 08:00:00 +08:00
    dreampuf
        1
    dreampuf  
       2013-03-31 19:56:19 +08:00
    print json.dump(list(result))
    013231
        2
    013231  
       2013-03-31 20:11:55 +08:00
    json.dumps([dict((key, item[key]) for key in item if key != '_id') for item in doc.find()])
    013231
        3
    013231  
       2013-03-31 20:13:44 +08:00
    上面的答案要修改一下.
    json.dmps([dict((key, item[key]) for key in item if key != '_id') for item in result])
    ritksm
        4
    ritksm  
       2013-03-31 20:26:23 +08:00   1
    亲们。。。要去掉_id直接在find里加参数不就得了。。。find({}, {"_id":0})
    ritksm
        5
    ritksm  
       2013-03-31 20:26:59 +08:00
    建议lz参考document。。。。http://api.mongodb.org/python/2.5/
    hilenlai
        6
    hilenlai  
    OP
       2013-03-31 20:33:50 +08:00
    @dreampuf 这个结果不对撒~

    @013231 这个可行!!得到一串
    [{"age": 25.0, "name": "zhangsan", "sex": 1.0}, {"age": 25.0, "name": "zhangsan", "sex": 1.0}, {"age": 25.0, "name": "zhangsan", "sex": 1.0}, {"age": 25.0, "name": "zhangsan", "sex": 1.0}, {"age": 25.0, "name": "zhangsan", "sex": 1.0}, {"age": 25.0, "name": "zhangsan", "sex": 1.0}, {"age": 25.0, "name": "zhangsan", "sex": 1.0}, {"age": 25.0, "name": "zhangsan", "sex": 1.0}]


    @ritksm 其实不是去掉id.. mongo查出来的不是bson嘛。 然后我想转化成json :)
    binux
        7
    binux  
       2013-03-31 20:40:50 +08:00
    @hilenlai 你显然没明白bson的b是什么

    from bson import json_util as json
    json.dump(list(result))
    hilenlai
        8
    hilenlai  
    OP
       2013-03-31 20:44:45 +08:00
    @binux 的确是没有特别明白。。刚刚接触python才几天 :)我试试方法~~
    ritksm
        9
    ritksm  
       2013-03-31 20:53:17 +08:00
    @hilenlai bson是mongodb用来存储的格式。。。。pymongo查出来已经是dict了。。。。想知道是什么类型请用type(X)

    http://d.pr/i/2cSz

    BTW:Connection已经在新版本的pymongo中被弃用了。。。用MongoClient吧。。。
    hilenlai
        0
    hilenlai  
    OP
       2013-03-31 21:31:46 +08:00
    @ritksm 多谢指教。

    MongoClient 是python3.0的用法吗?
    我还在用2.7呢
    ooscrewyou
        11
    ooscrewyou  
       2013-04-09 22:12:35 +08:00
    @hilenlai 和Python版本 Connection已deprecated了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     951 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 22:38 PVG 06:38 LAX 14:38 JFK 17:38
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86