英文:
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"
}
}
}
}
})
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论