将字符串拆分为 MongoDB 中的子字符串或字符数组 [英] Split string into an array of substrings or characters in MongoDB
问题描述
我需要像这样转换字段:
I need to convert fields like this:
{
"_id" : ObjectId("576fd6e87d33ed2f37a6d526"),
"phoneme" : "JH OY1 N Z"
}
到这样的子字符串数组中
into an arrays of substrings like this
{
"_id" : ObjectId("576fd6e87d33ed2f37a6d526"),
"phonemes" : [ "JH", "OY1", "N", "Z" ]
}
有时会变成这样的字符数组
and sometimes into an array of characters like this
{
"_id" : ObjectId("576fd6e87d33ed2f37a6d526"),
"phonemes" : ["J", "H", " ", "O", "Y", "1", " ", "N", " ", "Z"]
}
我找到了一些代码 here 将字符串转换为数组,但它对我的目的来说有点太简单了,因为只有一个数组元素要创建.
I found some code here which converts a string into an array, but it's a bit too simple for my purposes as there is only a single array element to be created.
db.members.find().snapshot().forEach( function (x) {
x.photos = [{"uri": "/images/" + x.photos}];
db.members.save(x);
});
我可以在 mongo shell 语句中使用整个 javascript 语言吗?
Is the entire javascript language available to me from within mongo shell statements?
推荐答案
假设我们集合中的文档如下所示:
Suppose that the documents in our collection look like this:
{ "phoneme" : "JH OY1 N Z" }
{ "phoneme" : "foobar" }
在 3.4+ 版本中,我们可以使用 $split
运算符将字段值划分为子字符串数组.
In version 3.4+, we can use $split
operator to divide the field value into an array of substrings.
要将字符串拆分为字符数组,我们需要应用 $substrCP
表达式使用 $map
运算符.
To split a string into an array of characters, we need to apply a $substrCP
expression to the array of all chars in the string index using the $map
operator.
要获取索引值数组是从 0 到字符串长度减一的所有整数,可以使用 $range
和 $strLenCP
运算符.
To get the array of index value is all integers from 0 to the string's length minus one which can generate using the $range
and the $strLenCP
operators.
我们使用 $addFields
将新字段添加到初始文档的管道阶段,但为了使其持久化,我们可以 创建视图或使用 $out
聚合管道算子.
We use the $addFields
pipeline stage to add the new fields to the initial document, but for this to be persistent, we can either create a view or overwrite our collection using the $out
aggregation pipeline operator.
[
{
"$addFields":{
"arrayOfPhonemeChar":{
"$map":{
"input":{
"$range":[
0,
{
"$strLenCP":"$phoneme"
}
]
},
"in":{
"$substrCP":[
"$phoneme",
"$$this",
1
]
}
}
},
"phonemeSubstrArray":{
"$split":[
"$phoneme",
" "
]
}
}
}
]
产生如下所示的东西:
{
"phoneme" : "JH OY1 N Z",
"arrayOfPhonemeChar" : ["J", "H", " ", "O", "Y", "1", " ", "N", " ", "Z"],
"phonemeSubstrArray" : ["JH", "OY1", "N", "Z"]
},
{
"phoneme" : "foobar",
"arrayOfPhonemeChar" : ["f", "o", "o", "b", "a", "r"],
"phonemeSubstrArray" : ["foobar"]
}
这篇关于将字符串拆分为 MongoDB 中的子字符串或字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!