我应该如何构建我的MongoDB组件索引



我有一个mongo图像元数据集合,由以下字段组成:camera_name(str(、photographer_name(str

我只想运行2个查询:

  1. 给定camera_name,返回时间戳为<=的记录1639457261(UNIX时间戳示例(。记录必须按降序排序
  2. 给定camera_name、photographer_name、分辨率、image_size和时间戳,我想检索记录,按输入的时间戳的降序排序

我创建了两个索引:

  1. { "camera_name": 1, "timestamp": -1 }
  2. { "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}

我执行的查询:

  1. camera_name=尼康和时间戳<1503582086应返回4条记录
  2. camera_name='Fuji',photographer_name='Beth',分辨率='1920x1080',image_size='5',时间戳<190000000应该返回2条记录,但我得到了0条记录

索引不会"过滤器";结果,它们允许您通过扫描索引树而不是扫描原始文档来更快地访问数据。

这意味着如果第二个查询";"不返回任何内容";,它与您构建的任何索引都无关,但您使用的实际查询与数据库中的任何文档都不匹配。

我还要提到的是,您的第二个索引可能会更小(取决于规模和数据分布等某些假设(,这可以帮助更新/插入性能,同时还可以减少存储大小。然而,从原始数据来看,我认为这些并不是你的紧急考虑因素。

相关内容

  • 没有找到相关文章

最新更新