简体   繁体   English

返回数组类型字段包含确切顺序的数组的文档

[英]Return documents of which field of type array contains array in exact order

I'm having documents in MongoDB structured like this: 我在MongoDB中的文档结构如下:

{ _id : 1, tokens : [ "one","two","three","four","five","six","seven" ] }
{ _id : 2, tokens : [ "two","three","four","one","one","five","eight" ] }
{ _id : 3, tokens : [ "six","three","four","five","one","five","nine" ] }

On average the documents contain token arrays with a length of 4500 items. 平均而言,文档包含长度为4500项的令牌数组。

I need to do some sort of pattern matching, where I have arrays of tokens in exact order to match, ie let's say I have to find the following in exactly matching order... 我需要进行某种模式匹配,其中我要按完全相同的顺序排列令牌数组,也就是说,我必须按完全匹配的顺序查找以下内容...

["three","four","five"]

...I want my query to provide me the following documents... ...我希望我的查询向我提供以下文件...

{ _id : 1, tokens : [ "one","two","three","four","five","six","seven" ] }
{ _id : 3, tokens : [ "six","three","four","five","one","five","nine" ] }

Ie both documents contain the exact order of the items I had in my array to search with. 也就是说,两个文档都包含我要在数组中搜索的项目的确切顺序。

Arrays I search with may have different lengths, ranging from 1 to 15 tokens. 我搜索的数组可能具有不同的长度,范围从1到15个标记。

I'm looking for the following: 我正在寻找以下内容:

  • Is this doable with MongoDB queries? 这对MongoDB查询可行吗? I've read, and re-read and re-re-read the pretty good docs, but couldn't find a solution eg using $all. 我已经阅读,并重新阅读并重新阅读了很好的文档,但是找不到解决方案,例如使用$ all。
  • Is there perhaps a better way to store tokens like this to get done what I need? 也许有更好的方法来存储这样的令牌来完成我需要的工作吗?

Thanks for any help. 谢谢你的帮助。

It's going to be slow, but you can do this with a $where operator; 这会很慢,但是您可以使用$where运算符来完成; pairing it with an $all operator to help with performance. 将其与$all运算符配对以提高性能。

db.test.find({
    tokens: {$all: ["three","four","five"]},
    $where: function() {
        var ix = -1;
        // Find each occurrence of 'three' in this doc's tokens array and return
        // true if it's followed by 'four' and 'five'.
        do {
            ix = this.tokens.indexOf('three', ix + 1);
            if (ix !== -1 && ix+2 < this.tokens.length && 
                this.tokens[ix+1] === 'four' && this.tokens[ix+2] === 'five') {
                return true;
            }
        } while (ix !== -1);
        return false;
    }
})

暂无
暂无

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

相关问题 如何在Mongodb(Mongoid)中使用包含另一个文档的数组字段创建模型作为嵌入式文档 - How to create model with an array field which contains another documents as an embedded documents in Mongodb (Mongoid) 返回包含数组字段的文档,该字段包含Elasticsearch 6.x中用户数组中的所有元素 - Return documents with an array field that contains ALL elements from a user array in Elasticsearch 6.x 按另一个数组对包含 OpenStructs 的数组进行排序 - Order an array which contains OpenStructs by another array MongoDB:查找具有精确数组值和大小的文档 - MongoDB: Find documents which has exact array values and size 如何查找其特定字段不是数组类型的文档? - how to find documents that their specific field is not in array type? 返回仅包含奇数整数的数组 - return an Array which contains only odd integers 猫鼬:查找所有包含在另一个数组中的数组字段的文档 - Mongoose: Find all documents with array field which is included in another array Mongodb-将数组中的元素拉到包含该元素的所有文档中 - Mongodb - Pull an element in an array to all documents which contains that element 包含完全相反的元素顺序的两个数组的交集 - Intersection of two array which contain exact reverse order of elements 按数组字段的元素索引顺序查找 mongodb 集合中的文档 - Finding documents in mongodb collection by order of elements index of array field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM