1

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>"
mtz_federico
  • 107
  • 1
  • 9

1 Answers1

1

it can be extracted in one line

orderids = [x['orderid'] for x in mydoc]
venkata krishnan
  • 1,961
  • 1
  • 13
  • 20