在MongoDB中将字符串拆分为子字符串或字符的数组 [英] Split string into an array of substrings or characters in MongoDB
本文介绍了在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" ]
}
有时是这样的字符数组
{
"_id" : ObjectId("576fd6e87d33ed2f37a6d526"),
"phonemes" : ["J", "H", " ", "O", "Y", "1", " ", "N", " ", "Z"]
}
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?
推荐答案
比我想象的要容易得多.只需使用JavaScript拆分功能.繁荣!
Much easier than I thought. Just use JavaScript split function. boom!
db.temp.find().snapshot().forEach( function (el) {
el.phonemes = el.phoneme.split(' ');
db.temp.save(el);
});
这篇关于在MongoDB中将字符串拆分为子字符串或字符的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文