gpt4 book ai didi

database - 使用 MongoDB 查找文档,该数组包含一个字符串,该字符串是特定单词的子字符串

转载 作者:可可西里 更新时间:2023-11-01 10:08:48 25 4
gpt4 key购买 nike

假设,我有这个集合:

{"str": ["pho", "goa"], ...},
{"str": ["sma", "aba"], ...},
{"str": ["gag"], ...}
...

我想选择所有文档,其中字段(str 这里)包含一个字符串,它是某个单词的子字符串。 智能手机 例如。所以查询的结果应该是:

{"str": ["pho", "goa"], ...},
{"str": ["sma", "aba"], ...}

我该怎么做?

这个问题与 #54279248 有关,您不必在数组中搜索。

最佳答案

您可以使用以下聚合:

db.collection.aggregate([
{
$match: {
$expr: {
$anyElementTrue: {
$map: {
input: "$str",
as: "s",
in: { $ne: [ -1, { $indexOfBytes: [ "smartphone", "$$s" ] } ] }
}
}
}
}
}
])

这个想法是你可以利用 $indexOfBytes要检查 str 中的字符串是否包含在 "smartphone" 中,然后您只需要使用 $anyElementTrue检查 str

中的任何项目是否满足条件

Mongo Playground here

关于database - 使用 MongoDB 查找文档,该数组包含一个字符串,该字符串是特定单词的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54307938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com