如何在 Mongo 中使用多个隐式 $and 运算符和多个 $in 运算符

问题描述 投票:0回答:1

我想用 $regex 搜索检查特定字段中是否有 3 个单词,如果存在则返回集合内的所有文档。

{

  $and: [

    { "preferredTitle.hasValue.value": { $regex: "journal", $options: "i" } },

    { "preferredTitle.hasValue.value": { $regex: "physics", $options: "i" } },

    { "preferredTitle.hasValue.value": { $regex: "Nuclear", $options: "i" } }

  ]

}

如果找不到匹配项,请使用相同的 $regex 在另一个字段中搜索相同的单词并返回集合中的所有文档。

{

  $and: [

    { "preferredTitle.hasValue.value": { $regex: "journal", $options: "i" } },

    { "preferredTitle.hasValue.value": { $regex: "physics", $options: "i" } },

    { "preferredTitle.hasValue.value": { $regex: "Nuclear", $options: "i" } }

  ]

}

当尝试使用多个 $and 运算符时,我可以获得预期的答案。 当尝试使用单个 $or 运算符与多个隐式 $and 运算符组合 $and 运算符时,无法执行查询。

{
 
    { $or: [
        { "preferredTitle.hasValue.value": { $regex: "journal", $options: "i" } },
        { "preferredTitle.hasValue.value": { $regex: "physics", $options: "i" } },
        { "preferredTitle.hasValue.value": { $regex: "Nuclear", $options: "i" } }
      ]
    },
    { "abbreviatedTitle.hasValue.value": { $regex: "Nuclear", $options: "i"}},
    { "abbreviatedTitle.hasValue.value": { $regex: "phy", $options: "i" } },
    { "abbreviatedTitle.hasValue.value": { $regex: "Nuclear", $options: "i" } }
  
}

有人可以帮忙吗?

我的文档结构将是:

{
  "_id": "Entity1",
  "createdDate": "sssss",
  "modifiedDate": "aaaaa",
  "sourceType": {
    "hasValue": {
      "value": "journ"
    },
    "origin": "zzzzzzz",
    "score": 6.5
  },
  "description": [],
  "preferredTitle": [
    {
      "hasValue": {
        "language": "und",
        "value": "Journal of Physics G: Nuclear and Particle Physics"
      },
      "origin": "ssddeedddd",
      "score": 6.5
    }
  ],
  "alternateTitle": [
    {
      "hasValue": {
        "language": "und",
        "value": "Journal of physics. G, Nuclear and particle physics : an Institute of Physics journal"
      },
      "origin": "sdsdsdsdsd",
      "score": 6.5
    }
  ],
  "abbreviatedTitle": [
    {
      "hasValue": {
        "language": "eng",
        "value": "journal.of.nuclear.phy."
      }
    }
  ],
  "translatedTitle": [],
  "transliteratedTitle": [],
  "identifier": [
    {
      "hasValue": {
        "value": "000000",
        "type": "pISSN"
      },
      "origin": "dfedcfgddeds",
      "score": 6.5
    },
    {
      "hasValue": {
        "value": "111111",
        "type": "eISSN"
      },
      "origin": "sssddsrefdfdf",
      "score": 6.5
    },
    {
      "hasValue": {
        "value": "22222",
        "type": "aaaaaaa"
      },
      "origin": "cdeefff",
      "score": 6.5
    },

  ],
  "alternateIdentifier": [],
  "homePage": {
    "hasValue": {
      "value": "http://fggfgfgfgfgfgf"
    },
    "origin": "csdsesdsdsd",
    "score": 6.5
  },
  "hasSubject": [
    {
      "type": "edm:Concept",
      "identifier": {
        "type": "idtype:ASJC",
        "value": "333333"
      },
      "origin": "fdfrefefef",
      "score": 6.5
    }
  ],
  "hasSourceQualityTag": [],
  "hasLicenseModel": [],
  "productDecision": [],
  "_class": "dsss"
}
regex mongodb search mongodb-query find
1个回答
0
投票

您的查询无效,因为它不是有效的对象。将条件包装到逻辑运算符(

$or
|
$and
,取决于您的搜索条件)应该可以解决。

db.collection.find({
  $or: [
    {
      $or: [
        {
          "preferredTitle.hasValue.value": {
            $regex: "journal",
            $options: "i"
          }
        },
        {
          "preferredTitle.hasValue.value": {
            $regex: "physics",
            $options: "i"
          }
        },
        {
          "preferredTitle.hasValue.value": {
            $regex: "Nuclear",
            $options: "i"
          }
        }
      ]
    },
    {
      "abbreviatedTitle.hasValue.value": {
        $regex: "Nuclear",
        $options: "i"
      }
    },
    {
      "abbreviatedTitle.hasValue.value": {
        $regex: "phy",
        $options: "i"
      }
    },
    {
      "abbreviatedTitle.hasValue.value": {
        $regex: "Nuclear",
        $options: "i"
      }
    }
  ]
})

演示@Mongo Playground

© www.soinside.com 2019 - 2024. All rights reserved.