这是场景:
目前我能想到的最好的方法是使用聚合。这些阶段看起来像这样:
聚合 -> 匹配第一个集合中的 2 个索引值 -> 排序 -> 使用与两个集合中的关系属性都匹配的管道进行查找,并根据第二个集合中的索引值的潜在搜索值进行匹配 ->与 OR 匹配,使用正则表达式查看第一个集合中的 2 个搜索字段,或者查找中的项目是否包含任何结果 -> 限制 -> 具有值的项目
令人担忧的是,搜索将在查找过程中对第一个集合中的所有文档与第二个集合中的所有文档进行联接。请记住,搜索的所有内容都是索引,但查找是这里主要关注的问题。建议以正确的方式做到这一点?更好的方法?
代码示例:
db.collection1.aggregate( [
{
$match: { // initial filters based on indexed values
field1: "somevalue",
field2: "somevalue"
},
},
{
$sort: {
firstSortField: -1, _id: -1 // sort results by needed order
}
},
{
$lookup: // join with another collection to search on a specific value
{
from: collection2,
localField: …Run Code Online (Sandbox Code Playgroud)