How can I return an array of mongodb objects in pymongo (without a cursor)? Can MapReduce do this?

后端 未结 3 1619
情话喂你
情话喂你 2021-01-11 12:05

I have a db set up in mongo that I\'m accessing with pymongo.

I\'d like to be able to pull a small set of fields into a list of dictionaries. So, something like wha

3条回答
  •  旧巷少年郎
    2021-01-11 12:35

    You don't need to call mapReduce, you just turn the cursor into a list like so:

    >>> data = list(col.find({},{"a":1,"b":1,"_id":0}).limit(2))
    >>> data
    [{u'a': 1.0, u'b': 2.0}, {u'a': 2.0, u'b': 3.0}]
    

    where col is your db.collection object.

    But caution with large/huge result cause every thing is loaded into memory.

提交回复
热议问题