如何将多重匹配合并为一个聚合来比较 MongoDB 上的完整字符串?

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

我正在尝试在

$and
子句中创建
$match
子句。这个
$and
子句应该与我提供的字符串进行完整的比较。我想确保与
attributes.value
的比较应该与我传递的字符串完全相同。用下面的表达式:

db.getCollection('cards').aggregate([{
    "$match" : {
        "$and" : [
            { "attributes.key" : "setCode", "attributes.value" : "qc" },
            { "attributes.key" : "code", "attributes.value" : "potions" }
        ]
    }
}

如您所见,我得到了与

potion
potions
cauldron potion
等代码匹配的结果。我还得到了其他具有不同键的属性的结果,其中包含单词
potions

上面的查询返回的一些示例数据:

[
  {
    "_id": {"$oid": "66ab6eec2982eff57de4420f"},
    "attributes": {
      "name": "Gold Cauldron",
      "code": "gold-cauldron",
      "text": "To play this card, return 2 of your Potions Lessons from play to your hand.",
      "illustrator": "Bob Petillo",
      "orientation": "horizontal",
      "set": "Quidditch Cup",
      "setCode": "qc",
      "type": "Item",
      "typeCode": "item",
      "rarity": "Foil Premium",
      "rarityCode": "foil-premium",
      "lessonType": "Potions",
      "lessonTypeCode": "potions",
      "lessonCost": "9",
      "actionCost": "1",
      "cardNumber": "7",
      "providesLesson": "Potions",
      "subType": ["Cauldron"]
    },
    "language": "en"
  },
  {
    "_id": {"$oid": "66ab6eec2982eff57de44247"},
    "attributes": {
      "name": "Mopsus Potion",
      "code": "mopsus-potion",
      "text": "Do 3 damage to yout opponent or to a Creature of your choice.",
      "illustrator": "Greg Hildebrandt",
      "orientation": "vertical",
      "set": "Quidditch Cup",
      "setCode": "qc",
      "type": "Spell",
      "typeCode": "spell",
      "rarity": "Common",
      "rarityCode": "common",
      "lessonType": "Potions",
      "lessonTypeCode": "potions",
      "lessonCost": "8",
      "actionCost": "1",
      "cardNumber": "63",
      "subType": ""
    },
    "language": "en"
  },
  {
    "_id": {"$oid": "66ab6eec2982eff57de44256"},
    "attributes": {
      "name": "Potions",
      "code": "potions",
      "text": "Provides 1 Potions lesson.",
      "illustrator": "Shanth Enjeti, Melissa Ferreira",
      "orientation": "horizontal",
      "set": "Quidditch Cup",
      "setCode": "qc",
      "type": "Lesson",
      "typeCode": "lesson",
      "rarity": "Common",
      "rarityCode": "common",
      "lessonType": "Potions",
      "lessonTypeCode": "potions",
      "actionCost": "1",
      "cardNumber": "78",
      "providesLesson": "Potions",
      "subType": ""
    },
    "language": "en"
  }
]

所以我的

$match
子句不尊重具有值
attributes.key
setCode
code
,因为它正在与其他属性进行比较。

如何确保上述

$match
条件仅匹配给定的键和给定的值?

谢谢大家!

mongodb aggregate
1个回答
0
投票

按照@aneroid的建议,可以达到我想要的结果

$elemMatch
:

db.getCollection('cards').aggregate([
    {
        "$match": {
            "attributes": {
                $elemMatch: {
                    key: "setCode",
                    value: "qc"
                }
            }
        }
    }, {
    "$match": {
        "attributes": {
            $elemMatch: {
                key: "code",
                value: "potions"
            }
        }
    }
}]);

如果有人知道通过更简单的查询获得相同结果的更好方法,请告诉我!

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