pymongo shows skip must be an instance of int - error

Viewed 2807

I am a newbie to mongodb. I want to retrieve the datas of a certain fields on a specified date from mongodb using python. My Mongodb Collection looks like this

{ 
    "_id" : ObjectId("5d9d7eec7c6265a42e352d6d"), 
    "browser" : "Chrome", 
    "countryCode" : "IN", 
    "Page" : "http://192.168.1.34/third.html", 
    "date" : "2019-10-09T10:32:08.438660"
}
{ 
    "_id" : ObjectId("5d9d7eec7c6265a42e352d6e"), 
    "browser" : "Chrome", 
    "countryCode" : "IN", 
    "Page" : "http://192.168.1.14/fourth.html", 
    "date" : "2019-10-12T10:32:08.438662"


   }

and so on

I retrieved the data from mongodb by using the following query in mongodb

db.collection_name.find({"date": {'$gte': "2019-10-09T10:32:08.438660", '$lte': "2019-10-10T10:32:08.438661"}},{}, {Page:[], _id:0})

I want to get that data using pymongo in python. Here's the Code I tried,

from pymongo import MongoClient
import pymongo
from bson.raw_bson import RawBSONDocument

myclient = pymongo.MongoClient(
    "mongodb://localhost:27017/", document_class=RawBSONDocument)
mydb = myclient['smackcoders']
mycol = mydb['logs']
from_date = "2019-10-09T10:32:08.438663"
to_date = "2019-10-12T10:32:08.438671"
for doc in mycol.find({"date": {'$gte': from_date, '$lte': to_date}}, {}, {'Page': [], '_id': 0}):
    print(doc)

It shows error:

Traceback (most recent call last):
  File "temp3.py", line 20, in <module>
    for doc in mycol.find({"date": {'$gte': from_date, '$lte': to_date}}, {}, {'url': [], '_id': 0}):
  File "/home/paulsteven/.local/lib/python3.7/site-packages/pymongo/collection.py", line 1460, in find
    return Cursor(self, *args, **kwargs)
  File "/home/paulsteven/.local/lib/python3.7/site-packages/pymongo/cursor.py", line 145, in __init__
    raise TypeError("skip must be an instance of int")
TypeError: skip must be an instance of int

Output Required:

["http://192.168.1.34/third.html","http://192.168.1.14/fourth.html",.....and goes on for a specified date]

I don't Know how to make it work. Query works in mongodb but in python, it fails. Help me with some solutions.

1 Answers

You've got 3 parameters in your find function; you probably only need 2; a query and a projection. The third parameter is skip which is why it's failing with that error.

Mongo shell only takes 2 parameters so it is likely ignoring the third which is why it looks like it is working.

Related