Split a string into an array of substrings or characters in MongoDB

I need to convert such fields:

{ "_id" : ObjectId("576fd6e87d33ed2f37a6d526"), "phoneme" : "JH OY1 NZ" } 

into substring arrays like this

 { "_id" : ObjectId("576fd6e87d33ed2f37a6d526"), "phonemes" : [ "JH", "OY1", "N", "Z" ] } 

and sometimes to an array of such characters

 { "_id" : ObjectId("576fd6e87d33ed2f37a6d526"), "phonemes" : ["J", "H", " ", "O", "Y", "1", " ", "N", " ", "Z"] } 

I found code here that converts a string to an array, but this is too easy for my purposes, since there is only a separate array element to be created.

 db.members.find().snapshot().forEach( function (x) { x.photos = [{"uri": "/images/" + x.photos}]; db.members.save(x); }); 

Is all javascript available to me from mongo shell expressions?

+6
source share
4 answers

Much easier than I thought. Just use the JavaScript split function. boom!

 db.temp.find().snapshot().forEach( function (el) { el.phonemes = el.phoneme.split(' '); db.temp.save(el); }); 
+6
source

Suppose the documents in our collection look like this:

 { "phoneme" : "JH OY1 NZ" } { "phoneme" : "foobar" } 

In version 3.4+, we can use the $split operator to split the field value into an array of substrings.

To split a string into an array of characters, we need to apply the expression $substrCP to the array of all characters in string using the $map operator.

To get an array of index values, all integers from 0 to the length of the string minus one that can be generated using $range and the $strLenCP operators.

We use the $addFields pipeline step to add new fields to the source document, but for this to be permanent, we can either create a view or overwrite our collection using the $out "→ pipeline aggregation operator.

 [ { "$addFields":{ "arrayOfPhonemeChar":{ "$map":{ "input":{ "$range":[ 0, { "$strLenCP":"$phoneme" } ] }, "in":{ "$substrCP":[ "$phoneme", "$$this", 1 ] } } }, "phonemeSubstrArray":{ "$split":[ "$phoneme", " " ] } } } ] 

gives what looks like this:

 { "phoneme" : "JH OY1 NZ", "arrayOfPhonemeChar" : ["J", "H", " ", "O", "Y", "1", " ", "N", " ", "Z"], "phonemeSubstrArray" : ["JH", "OY1", "N", "Z"] }, { "phoneme" : "foobar", "arrayOfPhonemeChar" : ["f", "o", "o", "b", "a", "r"], "phonemeSubstrArray" : ["foobar"] } 
+5
source

How to split a string into an array?

In any intermediate modern JavaScript engine, this

 var myString = 'foo bar baz'; var myArray = myString.split(' '); 

which should work even on the shell.

Does MongoDB provide a complete set of JavaScript functions?

Internally, since MongoDB 2.4 V8 engine , which corresponds to ECMA-262 . Expect all the functionality defined in this standard, at least.

I have not tested it, but some objects that you know from the browser do not really make much sense in the mongo shell. Everything related to the DOM, that is. Therefore, before using them, I better check whether they exist immediately.

+2
source

This should work with Mongo 3.4+ ( see here for more info ). This is a bit more concise than user3100115.

 db.members.aggregate( [ { "$addFields": { "phonemes": { "$split": [ "$phoneme", " " ] } }}, { "$out": "members" } ] ) 
+1
source

Source: https://habr.com/ru/post/978267/


All Articles