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

MongoDB中使用变量作为字段值查询时出现语法错误求助

问题解决:MongoDB查询中的Python语法错误

你的代码出现语法错误的原因很明确:字典键$gte没有加引号。在Python中,字典的字符串类型键必须用引号包裹,否则解释器会把它当作未定义的变量,直接触发语法错误。

修正后的代码如下:

async def get_users(self):
     today = 5
     return self.col.find({'prexdate': {'$gte': int(today)}})

额外优化:因为today本身已经是整数类型,int(today)属于多余的类型转换,可以直接简化为:

async def get_users(self):
     today = 5
     return self.col.find({'prexdate': {'$gte': today}})

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 09:45:42