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>"

1 Answer 1

1

it can be extracted in one line

orderids = [x['orderid'] for x in mydoc]
Sign up to request clarification or add additional context in comments.

9 Comments

It works but I get: "orders": [ "IHZXMZQ3SX", "eu8j35tvoO" ]
you wanted a list right ? and the list has all the order id's right ? or did i misread the question ?
It gets the job done, but I need to remove the extra characters
maybe you need to learn the string method join, as in ','.join(['a','b','c'])
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.