简体   繁体   English

如何从数组中拉出元素,其中元素的字符串长度>较大?

[英]How to $pull elements from an array, $where elements' string length > a large number?

And old slash escaping bug left us with some messed up data, like so: 而旧的斜线转义错误为我们留下了一些混乱的数据,如下所示:

{
    suggestions: [
        "ok",
        "not ok /////////// ... 10s of KBs of this ... //////",
    ]
}

I would like to just pull those bad values out of the array. 我只想从数组中拉出那些不好的值。 My first idea was to $pull based on a regex that matches 4 "/" characters, but it appears that regexes to not work on large strings: 我的第一个想法是基于匹配4个“ /”字符的正则表达式来$pull ,但是看起来正则表达式不适用于大字符串:

db.notes.count({suggestions: /\/\/\/\//}) // returns 0
db.notes.count({suggestions: {$regex: "////"}}) // returns 0

My next idea was to use a $where query to find documents that have suggestion strings that are longer than 1000. That query works: 我的下一个想法是使用$where查询查找suggestion字符串长于1000的文档。该查询有效:

db.notes.count({
    suggestions: {$exists: true},
    $where: function() {
        return !!this.suggestions.filter(function (item) {
            return (item || "").length > 1000;
        }).length
    }
})
// returns a plausible number

But a $where query can't be used as the condition in a $pull update. 但是$where查询不能用作$pull更新中的条件。

db.notes.update({
    suggestions: {$exists: true},
}, {
    $pull: {
        suggestions: {
            $where: function() {
                return !!this.suggestions.filter(function (item) {
                    return (item || "").length > 1000;
                }).length
            }
        }
    }
})

throws 抛出

WriteResult({
    "nMatched" : 0,
    "nUpserted" : 0,
    "nModified" : 0,
    "writeError" : {
        "code" : 81,
        "errmsg" : "no context for parsing $where"
    }
})

I'm running out of ideas. 我的想法不多了。 Will I have to iterate over the entire collection, and $set: {suggestions: suggestions.filter(...)} for each document individually? 我是否需要遍历整个集合以及每个文档的$set: {suggestions: suggestions.filter(...)} Is there no better way to clean bad values out of an array of large strings in MongoDB? 没有更好的方法从MongoDB的大型字符串数组中清除不良值?

(I'm only adding the "javascript" tag to get SO to format the code correctly) (我只添加“ javascript”标签即可获得SO以正确设置代码格式)

The simple solution pointed out in the question comments should have worked. 问题注释中指出的简单解决方案应该有效。 It does work with a test case that is a recreation of the original problem. 它确实适用于测试案例,可以重现原始问题。 Regexes can match on large strings, there is no special restriction there. 正则表达式可以匹配大字符串,那里没有特殊限制。

db.notes.updateOne({suggestions: /\/\//}, { "$pull": {suggestions: /\/\//}})

Since this didn't work for me, I ended up going with what the question discussed: updating all documents individually by filtering the array elements based on string length: 由于这对我不起作用,所以我最后讨论了这个问题:通过基于字符串长度过滤数组元素来单独更新所有文档:

db.notes.find({
    suggestions: {$exists: true}
}).forEach(function(doc) {
    doc.suggestions = doc.suggestions.filter(function(item) {
        return (item || "").length <= 1000;
    }); db.notes.save(doc);
});

It ran slow, but that wasn't really a problem in this case. 它运行缓慢,但是在这种情况下,这并不是真正的问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM