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 · 3961 次点击
    这是一个创建于 4047 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码:
    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.dumps([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
        10
    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   ·   我们的愿景   ·   实用小工具   ·   3129 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 12:38 · PVG 20:38 · LAX 05:38 · JFK 08:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.