Get & Concatenate MongoDB Subdocuments by a Condition

Question

Trying to find the dataList rows that have the first element in the string array greater than 6154 and less than or equal to 6155. Is it possible to do it in MongoDB? For example I am expecting MongoDB to return the rows: “6154.5,37.9,1.529,1.429,1.429”,“6155,30.4,1.505,1.532,1.543”.

 

The collection has the following structure:

{
    “_id” : ObjectId(“54f6a766bf4436333edcd6a2”),
    “_class” : “com.abc.core.bo.obj.Objs”,
    “objList” : [
        {
             “name” : “ABB-09”,
             “uid” : “ABB-09”,
             “data” : {
                 “dataId” : NumberLong(0),
                 “dataList” : [
        “6150,32.9,1.475,,1.434”,
        “6150.5,43,,1.529,1.402”,
        “6151,31.8,1.506,1.447,1.453”,
        “6151.5,33.6,1.481,1.456,1.521”,
        “6152,30.9,1.465,1.472,1.547”,
        “6152.5,39.5,1.404,1.425,1.485”,
        “6153,43.2,1.406,1.446,1.481”,
        “6153.5,39.5,1.433,1.468,1.488”,
        “6154,32.7,1.459,1.477,1.427”,
        “6154.5,37.9,1.529,1.429,1.429”,
        “6155,30.4,1.505,1.532,1.543”,
        “6155.5,37.3,1.49,1.436,1.462”,
        “6156,35.3,1.538,1.45,1.488”,
        “6156.5,37.3,1.517,1.535,1.473”,
        “6157,32.7,1.401,1.405,1.497”,
        “6157.5,38.9,1.488,1.468,1.499”,
        “6158,35.4,1.526,1.422,1.452”,
        “6158.5,43.3,1.516,1.433,1.491”,
        “6159,34.6,1.519,1.442,1.478”,
        “6159.5,42.7,1.426,1.514,1.428”,
        “6160,32.7,1.451,1.5,1.516”
       ]
      }
     }]}

 

Answer

The algorithm is to filter the documents under dataList by the first number in the string arrays. You can accomplish it with MongoDB API, but the code will be complicated. If you want a more efficient coding process, try using Structured Process Language (SPL) to achieve it. Here’s the SPL script:

A

1

=mongo_open(“mongo://localhost:27017/local?user=test&password=test”)  

2

=mongo_shell(A1,”test35.find()”)  

3

=A2.objList.data.dataList

4

=A3.select(~.split@cp()(1)   > 6154 && ~.split@cp()(1) <= 6155)

5

>mongo_close(A1)

A3: Locate and get the rows to be computed and return them as a sequence.

undefined

A4: Convert each string member into a sequence, get the first element and find the eligible rows according to the specified condition.

undefined