split string expression in aggregation framework ?

1,512 views
Skip to first unread message

ddorian

unread,
Sep 4, 2014, 9:16:39 AM9/4/14
to mongod...@googlegroups.com
Basically, is there an expression to create some fields by splitting an existing field in the aggregation framework ? I didn't find anything from searching.

Will Berkeley

unread,
Sep 4, 2014, 11:00:56 AM9/4/14
to mongod...@googlegroups.com
What do you have in mind as a "split", specifically? The string expression operators for the $project stage include a $substr operator that extracts a substring of a given string.

-Will

Dorian Hoxha

unread,
Sep 4, 2014, 11:04:45 AM9/4/14
to mongod...@googlegroups.com
In python:

"0000:11111".split(":")[1]

Split the string by a character, returning an array of strings and choose one of them (by position).


On Thu, Sep 4, 2014 at 5:00 PM, Will Berkeley <william....@mongodb.com> wrote:
What do you have in mind as a "split", specifically? The string expression operators for the $project stage include a $substr operator that extracts a substring of a given string.

-Will

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/hzLTa9dhm4U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/f627a7c8-774d-47e4-a4de-520acb7a59c2%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Will Berkeley

unread,
Sep 4, 2014, 12:24:12 PM9/4/14
to mongod...@googlegroups.com
Indeed, I know what a split function does, but what is your use case for a split? There's clearly no split function available in the aggregation framework but that doesn't mean a suitable substitute or workaround can't be found, depending on exactly what you're trying to do.

-Will

Dorian Hoxha

unread,
Sep 4, 2014, 1:12:47 PM9/4/14
to mongod...@googlegroups.com
I have documents {"_id": "site_id:yymmdd:article_id", "views": 24"}

And i want to group top articles, and getting the 'article_id' by splitting the "_id" of the document.


On Thu, Sep 4, 2014 at 6:24 PM, Will Berkeley <william....@mongodb.com> wrote:
Indeed, I know what a split function does, but what is your use case for a split? There's clearly no split function available in the aggregation framework but that doesn't mean a suitable substitute or workaround can't be found, depending on exactly what you're trying to do.

-Will

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/hzLTa9dhm4U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.

Will Berkeley

unread,
Sep 4, 2014, 1:28:36 PM9/4/14
to mongod...@googlegroups.com
Two methods: first, you could split the _id before you insert and store it as an embedded document like

{ "_id": { "site_id" : site_id, "date" : yymmdd, "article_id" : article_id }, "views": 24 }

assuming you want to keep the combination of site_id, date, and article_id as the unique key. This seems best to me. It's upgrading the three conceptually distinct components of an _id value to three separate fields of the document.

Second, if the site_id has a fixed length, say 7 characters like the string "site_id", you can effectively split the current value of _id with the $substr operator:

> db.test.aggregate([{ "$project" : { "site_id" : { "$substr" : ["$_id", 0, 7 ] }, "date" : { "$substr" : [ "$_id", 8, 6 ] }, "article_id" : { "$substr" : ["$_id", 15, -1] } } }])

{ "_id" : "site_id:yymmdd:article_id", "site_id" : "site_id", "date" : "yymmdd", "article_id" : "article_id" }

-Will

Dorian Hoxha

unread,
Sep 4, 2014, 3:37:32 PM9/4/14
to mongod...@googlegroups.com
The site_id is not a fixed length, but I can zero-pad it.

Thanks


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/hzLTa9dhm4U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.

Asya Kamsky

unread,
Sep 4, 2014, 4:41:13 PM9/4/14
to mongodb-user
I have a "stupid aggregation trick" that shows how you can get a first
location of a particular character in a string (which you can then use
as input to $substr. It's not pretty though.

http://www.kamsky.org/stupid-tricks-with-mongodb/ugly-way-to-parse-a-string-with-aggregation-framework

Asya
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mongodb-user...@googlegroups.com.
> To post to this group, send email to mongod...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mongodb-user.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mongodb-user/CANsFX05YsD%2BQ-uYk_ymjp7GHA_t3kK7YrvA4r5BP4q0A%3D54aXw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages