将 Mongo 中的一些字段从字符串转换为数组 [英] Converting some fields in Mongo from String to Array

查看:41
本文介绍了将 Mongo 中的一些字段从字符串转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文档集合,其中标签"字段从空格分隔的标签列表切换到单个标签的数组.我想将以前的空格分隔字段更新为像新传入数据一样的数组.

I have a collection of documents where a "tags" field was switched over from being a space separated list of tags to an array of individual tags. I want to update the previous space-separated fields to all be arrays like the new incoming data.

我也遇到了 $type 选择器的问题,因为它将类型操作应用于单个数组元素,这些元素是字符串.所以按类型过滤只会返回所有内容.

I'm also having problems with the $type selector because it is applying the type operation to individual array elements, which are strings. So filtering by type just returns everything.

如何将每个看起来像第一个示例的文档转换为第二个示例的格式?

How can I get every document that looks like the first example into the format for the second example?

{
    "_id" : ObjectId("12345"),
    "tags" : "red blue green white"
}
{
    "_id" : ObjectId("54321"),
    "tags" : [
        "red",
        "orange",
        "black"
    ]
}

推荐答案

我们不能使用 $type 运算符在这里过滤我们的文档,因为我们数组中元素的类型是字符串",并且如文档中所述:

We can't use the $type operator to filter our documents here because the type of the elements in our array is "string" and as mentioned in the documentation:

当应用于数组时,$type 匹配任何指定 BSON 类型的内部元素.例如,当匹配 $type : 'array' 时,如果该字段具有嵌套数组,则文档将匹配.它不会返回字段本身是数组的结果.

When applied to arrays, $type matches any inner element that is of the specified BSON type. For example, when matching for $type : 'array', the document will match if the field has a nested array. It will not return results where the field itself is an array.

但幸运的是,MongoDB 还提供了 $exists 运算符,可在此处与数值数组索引一起使用.

But fortunately MongoDB also provides the $exists operator which can be used here with a numeric array index.

现在我们如何更新这些文件?

Now how can we update those documents?

好吧,从 MongoDB 版本 <= 3.2 开始,我们唯一的选择是 mapReduce() 但首先让我们看看即将发布的 MongoDB 中的另一种选择.

Well, from MongoDB version <= 3.2, the only option we have is mapReduce() but first let look at the other alternative in the upcoming release of MongoDB.

从 MongoDB 3.4 开始,我们可以$project 我们的文档并使用 $split 操作符将我们的字符串拆分为一个子字符串数组.

Starting from MongoDB 3.4, we can $project our documents and use the $split operator to split our string into an array of substrings.

请注意,要仅拆分那些字符串的标签",我们需要一个合乎逻辑的 $condition 处理以仅拆分字符串值.这里的条件是 $eq当字段的 $type 等于 "string" 时,评估为 true.顺便说一下,$type 这里是 3.4 中的新内容.

Note that to split only those "tags" which are string, we need a logical $condition processing to split only the values that are string. The condition here is $eq which evaluate to true when the $type of the field is equal to "string". By the way $type here is new in 3.4.

最后我们可以使用 $out<覆盖旧的集合/code> 流水线阶段操作符.但是我们需要在$project阶段明确指定包含其他字段.

Finally we can overwrite the old collection using the $out pipeline stage operator. But we need to explicitly specify the inclusion of other field in the $project stage.

db.collection.aggregate(
     [
        { "$project": { 
            "tags": { 
                "$cond": [ 
                    { "$eq": [ 
                        { "$type": "$tags" }, 
                        "string"
                    ]}, 
                    { "$split": [ "$tags", " " ] }, 
                    "$tags" 
                ] 
            } 
        }},
        { "$out": "collection" }
    ]
)

使用 mapReduce,我们需要使用 Array.prototype.split() 在我们的 map 函数中发出子字符串数组.我们还需要使用查询"选项过滤我们的文档.从那里我们需要使用 bulkWrite() 3.2 中的新方法或现已弃用的方法 Bulk() 如果我们使用的是 2.6 或 3.0,如图 此处.

With mapReduce, we need to use the Array.prototype.split() to emit the array of substrings in our map function. We also need to filter our documents using the "query" option. From there we will need to iterate the "results" array and $set the new value for "tags" using bulk operations using the bulkWrite() method new in 3.2 or the now deprecated Bulk() if we are on 2.6 or 3.0 as shown here.

db.collection.mapReduce(
    function() { emit(this._id, this.tags.split(" ")); }, 
    function(key, value) {}, 
    { 
        "out": { "inline": 1 }, 
        "query": { 
            "tags.0": { "$exists": false }, 
            "tags": { "$type": 2 }
        }
    }
)['results']

这篇关于将 Mongo 中的一些字段从字符串转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆