mongodb - 查找第二个集合的数组具有来自第一个集合的数组的单词
问题描述
我正在尝试做一个聚合,我想将一个数组中的项目关联到另一个数组。
这个想法是,获取与术语相关的句子,其中句子包含术语项中的所有单词。输出将是一个包含所有文本字段的项目和一个包含所有术语属性的自定义字段。
这是我的第一个收藏:
db.terms.insertMany([
{ "_id" : 1, "items" : ["sun", "day"] },
{ "_id" : 2, "items" : ["moon", "night"] },
])
第二个:
db.texts.insertMany([
{ "_id" : 1, "sentence" : ["a beautiful sun makes a bright day", "not usefull here"] },
])
意图聚合:
db.texts.aggregate([
{$lookup: {
from: "terms",
let: { term_items: "$items" },
pipeline: [
{ $match: { $expr: { "$sentence": { $all: "$$term_items" } } } }
],
as: "term_obj"
}},
]);
当我执行此聚合时,我收到此错误:
org.graalvm.polyglot.PolyglotException:命令失败,出现错误 168(InvalidPipelineOperator):服务器 localhost:27019 上的“无法识别的表达式“$$term_items”。完整的响应是 {"ok": 0.0, "errmsg": "Unrecognized expression '$$term_items'", "code": 168, "codeName": "InvalidPipelineOperator"}
另一个意图:
db.texts.aggregate([
{$lookup: {
from: "terms",
let: { term_items: "$items" },
pipeline: [
{ $match: { $expr: { $in: ["$$term_items", "$sentence"] } } }
],
as: "term_obj"
}},
]);
错误:
org.graalvm.polyglot.PolyglotException:命令失败,错误 40081(Location40081):'$in 需要一个数组作为第二个参数,发现:在服务器 localhost:27019 上丢失。完整的响应是 {"ok": 0.0, "errmsg": "$in 需要一个数组作为第二个参数,找到:missing", "code": 40081, "codeName": "Location40081"}
我在这里想念什么?
解决方案
在现有查找中,您正在使用$items
它之前的值。$let
是您应该$sentence
从外部文档分配给变量的地方。
完成此查找的一种可能解决方案:
- $map 在
sentence
数组上 - 对于每个句子,对
items
数组进行 $reduce 并使用$in
- $reduce 在结果布尔数组上查看是否有一个句子匹配所有项目
- 使用 $match 和 $expr 测试结果
db.texts.aggregate([
{$lookup: {
from: "terms",
let: {sentences: "$sentence"},
pipeline: [
{$match: {
$expr: {
$reduce: {
initialValue: false,
input: {
$map: {
input: "$$sentences",
as: "sentence",
in: {$reduce: {
input: "$items",
initialValue: "true",
in: {$and: [
"$$value",
{$regexMatch: {
regex: "$$this",
input: "$$sentence"
}}
]}
}}
}
},
in: {$or: ["$$this", "$$value"]}
}
}
}}
],
as: "term_obj"
}}
])
推荐阅读
- python - 在 AWS lambda 函数中使用来自 S3 存储桶的 Pandas read_csv 时出错 - 第 5 行中预期有 1 个字段,看到 2
- java - 在验证中检查多个条件
- r - 如何将多个excel工作簿合并到数据框中使用R中新ID列中的特定单元格
- java - Micronaut:如何用@Context替换@EventListener与@Singleton结合?
- c++ - 如何在 vscode 中的 powershell 上运行命令?
- javascript - React,使用 useEffect 更新数据,一个数据是一个数组,但是当我使用 map 或 Object.keys(data).map 时,两者都不起作用。如何让它发挥作用?
- http - golang 正确的 http2 请求
- django - 如何在 Django 模型的 ManyToManyField 中添加 id(而不是对象)?
- c++ - 如何改进我的抽象可迭代构造以消除显式模板使用的需要?
- python - 如何在指定字符串的一部分之后抓取指定数量的字符?