简体   繁体   English

查询匹配连续数组元素的文档

[英]Query for documents where match contiguous array elements

I have a MongoDB collection with documents in the following format:我有一个 MongoDB 集合,其中包含以下格式的文档:

    { "_id" : 1, "tokens": [ "I", "have", "a", "dream" ] },
    { "_id" : 2, "tokens": [ "dream", "a", "little", "dream" ] },
    { "_id" : 3, "tokens": [ "dream", "a", "dream" ] },
    { "_id" : 4, "tokens": [ "a" , "little", "dream" ] },
    ...

I need to get all doucuments which "tokens" include contiguous array elements: "a", "dream" .我需要获取所有“令牌”包含连续数组元素的文件: “a”、“dream” So, the following are matched doucuments:因此,以下是匹配的文件:

    { "_id" : 1, "tokens": [ "I", "have", "a", "dream" ] },
    { "_id" : 3, "tokens": [ "dream", "a", "dream" ] },

Is there a way to get the right results?有没有办法得到正确的结果?

A trick that is to have a regexp.一个技巧就是有一个正则表达式。

  • $match to get the all documents which has $all array input $match获取具有$all数组输入的所有文档
  • $addFields to have a duplicate the tokens and input array $addFields具有重复的标记和输入数组
  • $reduce helps to concat all string joining - $reduce有助于连接所有字符串连接-
  • $regexMatch to match both strings $regexMatch匹配两个字符串
  • $match to eliminate unwanted data $match消除不需要的数据
  • $project to get necessary fields only $project仅获取必要的字段

The code is代码是

[{
    $match: {
        tokens: { $all: ["a", "dream"] }
    }
}, {
    $addFields: {
        duplicate: "$tokens",
        inputData: ["a", "dream"]
    }
}, {
    $addFields: {
        duplicate: {
            $reduce: {
                input: "$duplicate",
                initialValue: "",
                in: { $concat: ["$$value", "-", "$$this"] }
            }
        },
        inputData: {
            $reduce: {
                input: "$inputData",
                initialValue: "",
                in: { $concat: ["$$value", "-", "$$this"] }
            }
        }
    }
}, {
    $addFields: {
        match: {
            $regexMatch: { input: "$duplicate", regex: '$inputData' }
        }
    }
}, {
    $match: {
        match: true
    }
}, {
    $project: {  _id: 1,  tokens: 1 }
}]

Working Mongo playground工作蒙戈游乐场

Note: Do check multiple scenarios although its working for this scenario注意:请检查多个场景,尽管它适用于这种场景

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mongodb返回文档和匹配查询的数组元素 - Mongodb return documents AND the array elements that match query MongoDB匹配包含数组字段的文档,其中包含与查询匹配的所有元素 - MongoDB match documents that contain an array field with ALL elements that match the query 查询以找到所有数组属性元素都在数组中的文档 - Query to find documents where all array property elements are in array 如何按匹配查询的数组中元素的数量排序文档? - How do I order documents by the number of elements in an array that match a query? Mongo 查询以查找任何字段元素与任何查询参数元素匹配的文档 - Mongo query to find documents where any of the field's elements match any of the query param elements mongodb查询以获取与一个数组中的元素匹配并且不应该与另一个数组中的元素匹配的文档 - mongodb query to get documents which match an element inside an array and should not match elements inside another array 查询数组元素而不是文档 - Query array elements instead of documents 所有元素均符合条件的MongoDB查询数组 - MongoDB query array where all elements match criteria Mongo - 数组查询,仅查找所有元素匹配的位置 - Mongo - Array query, only find where all elements match Mongodb:查找具有数组的文档,其中所有元素都存在于查询数组中,但是文档数组可以更小 - Mongodb: find documents with array where all elements exist in query array, but document array can be smaller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM