【发布时间】:2022-01-17 11:24:35
【问题描述】:
我有一个 mongo 图像元数据集合,包含以下字段:camera_name(str)、photographer_name(str)、resolution(str)、image_size(int in MB, rounded) 和 timestamp(10 digit UNIX timestamp)
我只想运行 2 个查询:
- 给定 camera_name,返回时间戳
- 给定 camera_name、photographer_name、resolution、image_size 和 timestamp,我想检索记录,按输入的时间戳降序排列。
我创建了 2 个索引:
{ "camera_name": 1, "timestamp": -1 }{ "camera_name": 1, "photographer_name": 1, "resolution": 1, "image_size": 1, "timestamp": -1}
第一个索引有效,但是当我对第二个索引运行查询时,没有返回任何记录。我确信集合中存在记录,并且我希望在运行第二个查询时至少获得 10 条记录,但它返回一个空列表。
索引的配置方式有问题吗?谢谢
这里是示例数据:
{"camera_name": "Nikon", "photographer_name": "Aaron", "resolution": "1920x1080", "image_size": "3", "timestamp": 1397232415}
{"camera_name": "Nikon", "photographer_name": "Paul", "resolution": "1920x1080", "image_size": "4", "timestamp": 1717286853}
{"camera_name": "Nikon", "photographer_name": "Beth", "resolution": "720x480", "image_size": "1", "timestamp": 1503582086}
{"camera_name": "Nikon", "photographer_name": "Aaron", "resolution": "1920x1080", "image_size": "4", "timestamp": 1500628458}
{"camera_name": "Nikon", "photographer_name": "Paul", "resolution": "1920x1080", "image_size": "6", "timestamp": 1407580951}
{"camera_name": "Canon", "photographer_name": "Beth", "resolution": "1920x1080", "image_size": "5", "timestamp": 1166049453}
{"camera_name": "Canon", "photographer_name": "Paul", "resolution": "720x480", "image_size": "2", "timestamp": 1086317569}
{"camera_name": "Canon", "photographer_name": "Beth", "resolution": "720x480", "image_size": "1", "timestamp": 1400638926}
{"camera_name": "Canon", "photographer_name": "Aaron", "resolution": "720x480", "image_size": "1", "timestamp": 1345248762}
{"camera_name": "Canon", "photographer_name": "Paul", "resolution": "1920x1080", "image_size": "5", "timestamp": 1462360853}
{"camera_name": "Fuji", "photographer_name": "Beth", "resolution": "720x480", "image_size": "2", "timestamp": 1815298047}
{"camera_name": "Fuji", "photographer_name": "Shane", "resolution": "720x480", "image_size": "3", "timestamp": 1666493455}
{"camera_name": "Fuji", "photographer_name": "Beth", "resolution": "1920x1080", "image_size": "5", "timestamp": 1846677247}
{"camera_name": "Fuji", "photographer_name": "Beth", "resolution": "1920x1080", "image_size": "5", "timestamp": 1630996389}
{"camera_name": "Fuji", "photographer_name": "Shane", "resolution": "720x480", "image_size": "2", "timestamp": 1816829362}
我执行的查询:
- camera_name=Nikon 和时间戳
- camera_name='Fuji' ,photographer_name='Beth', resolution='1920x1080', image_size='5' and timestamp
【问题讨论】:
-
索引不会阻止结果。索引提高了查询的速度。如果您没有得到任何结果,则要么数据不符合预期,要么查询不符合预期。此条件与索引规范无关。
-
由于您没有提供示例数据,也没有提供查询,我无法帮助您解决结果问题。
-
@barrypicker 我已经编辑了问题并添加了示例数据和查询及其预期输出
-
感谢您提供示例数据。这有点帮助。您尚未提供查询。您已经提供了查询的目的。有时实施是问题所在。我同意您的结果与您的意图不符。请提供实际查询。
标签: mongodb pymongo mongodb-indexes compound-index