You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pymongo报skip必须为int错误,MongoDB日期查询Python实现求助

Hey there! Let's fix this issue step by step — I'll break down what's going wrong and how to get your desired Page list.

问题原因分析

The TypeError: skip must be an instance of int happens because PyMongo's find() method has a different parameter order than the MongoDB Shell, plus you're using a document class that complicates field access. Here's the breakdown:

  • In PyMongo, the find() method's signature places the skip parameter as the third argument. Your code passed the projection dictionary ({'Page': [], '_id': 0}) into this third slot, which expects an integer — hence the error.
  • You're using RawBSONDocument as your document class, which returns raw BSON byte objects instead of easily accessible dictionaries. This would block you from directly grabbing the Page field even if the skip error was fixed.

修正后的解决方案

Step 1: Fix the find() parameter order

Move your projection to the second argument position, or use explicit keyword arguments (even clearer!) to avoid order confusion. PyMongo accepts the same projection syntax as the shell, though 1 (include field) and 0 (exclude field) is the most common way to write it.

Step 2: Use the default document class (unless you need raw BSON)

Remove document_class=RawBSONDocument from your client setup — the default dictionary type lets you access fields directly without extra decoding.

Full Working Code

from pymongo import MongoClient

# Connect to MongoDB with default document class (dictionaries)
myclient = MongoClient("mongodb://localhost:27017/")
mydb = myclient['smackcoders']
mycol = mydb['logs']

from_date = "2019-10-09T10:32:08.438663"
to_date = "2019-10-12T10:32:08.438671"

# Get the Page list using a list comprehension (clean and concise)
page_list = [doc['Page'] for doc in mycol.find(
    filter={"date": {'$gte': from_date, '$lte': to_date}},
    projection={'Page': 1, '_id': 0}  # Include Page, exclude _id
)]

# Print the desired list format
print(page_list)

If you must use RawBSONDocument

If you need to work with raw BSON for specific reasons, you'll need to decode the raw bytes into a dictionary first:

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

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

page_list = []
for raw_doc in mycol.find(
    {"date": {'$gte': from_date, '$lte': to_date}},
    {'Page': 1, '_id': 0}
):
    # Decode raw BSON to a dictionary to access fields
    doc = bson.decode(raw_doc.raw)
    page_list.append(doc['Page'])

print(page_list)

Expected Output

After running the corrected code, you'll get exactly the list you want:

["http://192.168.1.34/third.html","http://192.168.1.14/fourth.html"]

内容的提问来源于stack exchange,提问作者Rural Smack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:49:19