0

I have following document:

{
        "subscriptionIds" : [ 
            ObjectId("60c312c6dbb5a49fbbf560ea")
        ],
        "gps" : {
            "type" : "Point",
            "coordinates" : [ 
                23.942706, 
                54.932539
            ]
        },
        "online" : false,
        "suspended" : false,
        "hwModel" : "",
        "fw" : "",
        "lastSeen" : ISODate("2021-06-16T04:43:36.682Z"),
        "lastSimRequest" : ISODate("2021-06-16T04:34:59.749Z"),
        "lastLocation" : "LT",
        "lastLocationType" : "gps",
        "createdAt" : ISODate("2021-05-20T10:37:16.025Z"),
        "updatedAt" : ISODate("2021-06-11T07:37:56.981Z"),
        "notes" : "",
        "psk_seed" : "QTAebOeNP4nIs-JJSNNlkAQ78N_VaxOq98-_lQPCyZQ=",
        "lastOnline" : ISODate("2021-06-15T08:01:59.886Z"),
        "lastOffline" : ISODate("2021-06-16T04:43:36.682Z"),
        "onlineReason" : "deviceOnlineStatusFromAC",
        "offlineReason" : "deviceOfflineStatusTimeout",
        "allocationSettings" : "dataplan",
        "subscriptionDataplans" : [ 
            {
                "_id" : ObjectId("5fae82fc1224cc8d62b5bf17"),
                "organizationId" : ObjectId("5dd63d1c1d042f3018e8374e"),
                "organizationName" : "",
                "name" : "Naujas plan Telia 75GB",
                "enabled" : true,
                "contractsId" : [ 
                    ObjectId("5e32847ab8013befcc14bb1b"), 
                    ObjectId("5e32847ab8013befcc14bb1b")
                ],
                "simQuota" : 0,
                "periodQuota" : NumberLong(0),
                "allocationRules" : null,
                "createdAt" : ISODate("2020-11-13T12:58:36.650Z"),
                "updatedAt" : ISODate("2021-06-14T08:08:28.728Z"),
                "notes" : "",
                "allowRoaming" : false,
                "enablePriorityOrdering" : false,
                "priorityOrdering" : ""
            }, 
            {
                "_id" : ObjectId("5fcf25662b1c7d9bab8c1f7d"),
                "organizationId" : ObjectId("5dd63d1c1d042f3018e8374e"),
                "organizationName" : "",
                "name" : "London test",
                "enabled" : true,
                "contractsId" : [ 
                    ObjectId("5e5dfea1efcf754767408eae")
                ],
                "simQuota" : 0,
                "periodQuota" : NumberLong(0),
                "createdAt" : ISODate("2020-12-08T07:04:06.255Z"),
                "updatedAt" : ISODate("2021-06-15T09:28:07.472Z"),
                "notes" : "",
                "allowRoaming" : true,
                "enablePriorityOrdering" : false,
                "priorityOrdering" : ""
            }
        ],
    }

Is there a way to make following array using "_id" and "allowRoaming" fields:

"dataplanRoaming": [
{
                "_id" : ObjectId("5fae82fc1224cc8d62b5bf17"),       
                "allowRoaming" : false,
            }, 
            {
                "_id" : ObjectId("5fcf25662b1c7d9bab8c1f7d"),
                "allowRoaming" : true,
            }
]

My best result was, I tried using project, addFields etc still can't get structure which I want. Rest of query works just fine just missing this part

"dataplanRoaming" : [ 
        [ 
            false, 
            true
        ], 
        [ 
            ObjectId("5fae82fc1224cc8d62b5bf17"), 
            ObjectId("5fcf25662b1c7d9bab8c1f7d")
        ]
    ],

I hoped that {$addFields:{dataplanRoaming:["$subscriptionDataplans.allowRoaming", "$subscriptionDataplans._id"]}}, would give me wanted result it just made array with _id and allowRoaming as separates fields? Is there a way to create my wanted result using aggregation etc?

1 Answer 1

1
  • $map to iterate loop of subscriptionDataplans and return needed feidls
db.collection.aggregate([
  {
    $addFields: {
      dataplanRoaming: {
        $map: {
          input: "$subscriptionDataplans",
          in: {
            _id: "$$this._id",
            allowRoaming: "$$this.allowRoaming"
          }
        }
      }
    }
  }
])

Playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.