mtz_federico
mtz_federico

Reputation: 107

converting mongodb values in query to a string in python

How can I convert the values in the multiple responses from mongodb in python to a string with the values separated by a comma?

I don't know if my question is understandable, basically, this is what I want to do.

I am using this code to get data from mongodb:

    myclient = pymongo.MongoClient("mongodb://localhost:27017/")
    mydb = myclient["mydatabase"]
    driversdb = mydb["orders"]

    ordersQuery = { "city": city }
    mydoc = ordersdb.find(ordersQuery)
    for x in mydoc:

The response is:

{ <more values>, 'orderid': 'IHZXMZQ3SX', <more values> }
{ <more values>, 'orderid': 'eu8j35tvoO', <more values> }
<more data>

how can I convert that to:

orderslist = "IHZXMZQ3SX, eu8j35tvoO, <more orderids>"

Upvotes: 1

Views: 344

Answers (1)

venkata krishnan
venkata krishnan

Reputation: 2046

it can be extracted in one line

orderids = [x['orderid'] for x in mydoc]

Upvotes: 1

Related Questions