Table of Contents
MongoDB is a document-oriented database system that allows for the storage and retrieval of data in the form of documents. In order to split a string into an array of substrings in MongoDB, one can make use of the split() method. This method allows for the division of a string into smaller substrings based on a given delimiter. The resulting substrings are then stored in an array, providing easy access to individual elements. This functionality is useful for manipulating and organizing data in a database, making it a valuable tool for developers and data analysts working with MongoDB.
MongoDB: Split String into Array of Substrings
You can use the following syntax to split a string into an array of substrings in MongoDB:
db.myCollection.aggregate([
{ $project: { split_field: { $split: [ "$field1", " " ] } } },
{ $merge: "myCollection" }
])This particular example splits the string in “field1” based on spaces into a new field titled “split_field” and adds the new field to the collection titled myCollection.
The following example shows how to use this syntax in practice with a collection teams with the following documents:
db.teams.insertOne({name: "Dallas Mavs", points: 31})
db.teams.insertOne({name: "San Antonio Spurs", points: 22})
db.teams.insertOne({name: "Houston Rockets", points: 19})
db.teams.insertOne({name: "Boston Celtics", points: 26})
db.teams.insertOne({name: "Cleveland Cavs", points: 33})Example: Split String into Array of Substrings in MongoDB
We can use the following code to split the string in the “name” column into an array of strings and display the results in a new field titled “split_name” in the teams collection:
db.teams.aggregate([
{ $project: { split_name: { $split: [ "$name", " " ] } } },
{ $merge: "teams" }
])Here’s what the updated collection now looks like:
{ _id: ObjectId("62014a924cb04b772fd7a938"),
name: 'Dallas Mavs',
points: 31,
split_name: [ 'Dallas', 'Mavs' ] }
{ _id: ObjectId("62014a924cb04b772fd7a939"),
name: 'San Antonio Spurs',
points: 22,
split_name: [ 'San', 'Antonio', 'Spurs' ] }
{ _id: ObjectId("62014a924cb04b772fd7a93a"),
name: 'Houston Rockets',
points: 19,
split_name: [ 'Houston', 'Rockets' ] }
{ _id: ObjectId("62014a924cb04b772fd7a93b"),
name: 'Boston Celtics',
points: 26,
split_name: [ 'Boston', 'Celtics' ] }
{ _id: ObjectId("62014a924cb04b772fd7a93c"),
name: 'Cleveland Cavs',
points: 33,
split_name: [ 'Cleveland', 'Cavs' ] } Notice that every document has a new field titled “split_name” that contains an array of substrings from the “name” field.
For this particular example we chose to separate the original string using an empty space as a delimiter.
If the string happens to be separated by a different delimiter (like a dash, slash, colon, etc.) then simply use that delimiter in the $split function instead.
Note: You can find the complete documentation for the $split function .
Additional Resources
The following tutorials explain how to perform other common operations in MongoDB:
Cite this article
stats writer (2024). How can I split a string into an array of substrings in MongoDB?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-split-a-string-into-an-array-of-substrings-in-mongodb/
stats writer. "How can I split a string into an array of substrings in MongoDB?." PSYCHOLOGICAL SCALES, 30 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-split-a-string-into-an-array-of-substrings-in-mongodb/.
stats writer. "How can I split a string into an array of substrings in MongoDB?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-split-a-string-into-an-array-of-substrings-in-mongodb/.
stats writer (2024) 'How can I split a string into an array of substrings in MongoDB?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-split-a-string-into-an-array-of-substrings-in-mongodb/.
[1] stats writer, "How can I split a string into an array of substrings in MongoDB?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I split a string into an array of substrings in MongoDB?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
