如何在pymongo中查询使用ISODATE?

发布于 2025-02-04 21:29:44 字数 978 浏览 3 评论 0原文

长话短说,我正在尝试做 this 过去两天在Pymongo。请帮忙。

for x in restaurants.find( 
            {"grades":
             {"$elemMatch":
              {'date': dateutil.parser.parse("2014-08-11T00:00:00Z"),
               'grade':'A',
               'score':11
              }}},
{"name":1,
 "grades.grade":1,
 "grades.score":1,
 "grades.date":1
}):
print(x)

编辑:在这里's's the Database。它的结构:

我正在尝试编写一个查询,以查找餐厅ID,名称和成绩的成绩,这些餐厅的成绩达到了“ A”,并在iSodate上获得11分。” 2014-08-11T00: 00:00z“在许多调查日期中。

我开始认为我输入的日期或数据库有问题。另外,正如您在第四行代码中看到的,我正在使用时间对象。请尝试查询自己。它不起作用。

Long story short, I'm trying to do this query in PyMongo for the last two days. Please help.

for x in restaurants.find( 
            {"grades":
             {"$elemMatch":
              {'date': dateutil.parser.parse("2014-08-11T00:00:00Z"),
               'grade':'A',
               'score':11
              }}},
{"name":1,
 "grades.grade":1,
 "grades.score":1,
 "grades.date":1
}):
print(x)

Edit: Here's the database. It's Structure:
enter image description here

I'm trying to write a query to find the restaurant Id, name, and grades for those restaurants which achieved a grade of "A" and scored 11 on an ISODate "2014-08-11T00:00:00Z" among many of survey dates.

I'm starting to think there's something wrong with either the date I'm entering or the database. Also, as you can see in the fourth line of code I AM using a time object. Please try querying yourself. Its not working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朦胧时间 2025-02-11 21:29:44

pymongo不使用“ iSodate”,它是mongo类型,您应该只使用datetime创建一个时间对象,例如:

from datetime import datetime
import pytz

parsed_date = datetime.strptime("2014-08-11T00:00:00Z", '%Y-%m-%dT%H:%M:%SZ')
timezone = pytz.timezone("UTC")
with_timezone = timezone.localize(parsed_date)

for x in restaurants.find(
        {
            "grades":
                {
                    "$elemMatch": {
                        'date': with_timezone,
                        'grade': 'A',
                        'score': 11
                    }
                }
        },
        {
            "name": 1,
            "grades.grade": 1,
            "grades.score": 1,
            "grades.date": 1
        }):
    print(x)

pymongo doesn't use "ISODate", it's a Mongo type, you should just use datetime to create a time object, like so:

from datetime import datetime
import pytz

parsed_date = datetime.strptime("2014-08-11T00:00:00Z", '%Y-%m-%dT%H:%M:%SZ')
timezone = pytz.timezone("UTC")
with_timezone = timezone.localize(parsed_date)

for x in restaurants.find(
        {
            "grades":
                {
                    "$elemMatch": {
                        'date': with_timezone,
                        'grade': 'A',
                        'score': 11
                    }
                }
        },
        {
            "name": 1,
            "grades.grade": 1,
            "grades.score": 1,
            "grades.date": 1
        }):
    print(x)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文