在嵌套对象中搜索文本

huangapple go评论48阅读模式
英文:

mongoose search text in nested object

问题

对不起,您的请求似乎有点混乱。这是您的问题的中文翻译:

在下面的MongoDB文档中存在一个嵌套的文档,看起来像下面的图片。我想在此文档的body字段中执行搜索,但我没有成功。我该如何进行这个搜索?提前感谢您的帮助。

我尝试像这样搜索。我的路径正确吗?

const result = await this.campaignModel.find({
  "sequencesPageData": {
    "$elemMatch": {
      "mailSteps": {
        "$elemMatch": {
          "mailTemplate": x
        }
      }
    }
  }
});

我尝试了这样的搜索,但没有结果。

const result = await this.campaignModel.find({
  "sequencesPageData": {
    "$elemMatch": {
      "mailSteps": {
        "$elemMatch": {
          "mailTemplate": x
        }
      }
    }
  }
});

希望这对您有所帮助。

英文:

there is a nested mongodb document that looks like the image below. I want to do a search in the body field in this document, but I could not succeed. how can i do this search. thanks in advance

在嵌套对象中搜索文本

I tried to search like this. Is my path correct?

const result = await this.campaignModel.find({
       "sequencesPageData": {
         "$elemMatch": {
           "mailSteps": {
             "$elemMatch": {
               "mailTemplate": x
             }
           }
         }
       }
     });

I did a search like this but got no results.

const result = await this.campaignModel.find({
       "sequencesPageData": {
         "$elemMatch": {
           "mailSteps": {
             "$elemMatch": {
               "mailTemplate": x
             }
           }
         }
       }
     });

答案1

得分: 0

For object fields, simply access them using dot notation. For array fields, use $elemMatch to perform the match.

英文:

For object fields, simply access them using dot notation. For array fields, use $elemMatch to perform the match.

db.collection.find({
  "sequencesPageData.mailSteps": {
    "$elemMatch": {
      "mailTemplate": {
        "$elemMatch": {
          "subject": "asdf"
        }
      }
    }
  }
})

Mongo Playground

答案2

得分: 0

如果您只测试数组中的单个字段,则无需使用$elemMatch,可以使用点符号表示法:

db.collection.find({ "sequencesPageData.mailSteps.mailTemplate.body": "x" })

$elemMatch用于确保由相同的数组元素满足2个或更多测试,但由于此示例仅使用1个测试,因此可以正常工作。

英文:

If you are only testing a single field in the array, there is no need to use $elemMatch, you can use dot notation:

db.collection.find({ "sequencesPageData.mailSteps.mailTemplate.body": "x" })

$elemMatch is useful to ensure that 2 or more tests are satisfied by the same array element, but since this example uses only 1 test, it works without.

huangapple
  • 本文由 发表于 2023年2月14日 03:51:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定