0

I connected the database with mongoClient from pymongo. I made a find() query but I can't read the result inside the cursor and I don't know why. Before I made this query from my local database and it worked, now I move all the code on a ubuntu server and the same code doesn't work anymore.

I tried this but it doesn't work on the server. On the local environment, it works

import json
import requests
from pymongo import MongoClient

client = MongoClient('mongodb://IP_ADDRESS/')
db=client.mydb
mycol = mydb["mycollection"]

array = []
for x in mycol.find():
    array.append(x)
print(array)

I also tried this but the response is a cursor and I don't know how to read this

import json
import requests
from pymongo import MongoClient

client = MongoClient('mongodb://IP_ADDRESS/')
db=client.mydb
mycol = mydb["mycollection"]

x = mycol.find()
print(x)

this is the output I obtained:

<pymongo.cursor.Cursor object at 0x7fa7bd016c10>

my expected value is something like this:

{ "_id" : ObjectId("5d47f749fa8ca433e0d58b5f"), "value1" : "result1", "value2" : "result2", "value3" : "result3"}
0

2 Answers 2

1

Please find the below source code example.

for post in posts.find():
    print(post)
Sign up to request clarification or add additional context in comments.

1 Comment

I already tried this solution. In the local environment, it works. But it doesn't work on the server for me!
-1

You will have to pass query and projections as per the MongoDB docs: https://www.mongodb.com/docs/manual/reference/method/db.collection.find/

try like this:

x = mycol.find(query, {"_id":1, "value1":1, "value2":1,...}) print(x)

1 basically means, which value you need to see in output. If you want to see the value of key 'value1', then put it as "value1":1. If you don't wish to see the value of key 'value1', then put it as "value1":0.

1 Comment

This have nothing to do with the projection. OP's scenario seems to concern with IP resolve issue, which causes the discrepancy between local and remote server.

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.