繁体   English   中英

在 MongoDB / mongoose 中是否有一个大的 $or 查询的替代方法?

[英]Is there an alternative to a large $or query in MongoDB / mongoose?

想象一下,我在 MongoDB 中有大量收藏,例如

{
  filename: 'a.jpg',
  filepath: '/images/83920/',
  ...
},
{
  filename: 'b.jpg',
  filepath: '/images/52345/',
  ...
},
{
  filename: 'c.jpg',
  filepath: '/images/14152/',
  ...
},
...
{
  filename: 'xyz.jpg',
  filepath: '/images/15325/',
  ...
}

现在我想一次检索多个文档,其中每个文档都匹配特定的条件。

现在我正在这样做:

db.getCollection('images')
    .find({
        $or: [
            {filename: 'b.jpg', filepath: '/images/52345/'},
            ...
            {filename: 'xyz.jpg', filepath: '/images/15325/'},
        ]
     });

我的问题是:在 MongoDB / mongoose 中是否有替代大型 $or 查询的方法?

就像是

db.getCollection('images')
    .findMultiple([
            {filename: 'b.jpg', filepath: '/images/52345/'},
            ...
            {filename: 'xyz.jpg', filepath: '/images/15325/'},
    ]);

真的很有帮助。

或者 $or 链接是预期的方式吗?

您可以添加派生字段以进行查询:

{
  filename: 'a.jpg',
  filepath: '/images/83920/',
  hash: md5(filename, filepath),
  ...
},

然后查询$in(hash)

这将允许您索引 hash 字段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM