首页 > 解决方案 > 如何在聚合期间从另一个数组创建新数组?

问题描述

我有以下文件:

{
        "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" : ""
            }
        ],
    }

有没有办法使用“_id”和“allowRoaming”字段制作以下数组:

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

我最好的结果是,我尝试使用项目、addFields 等仍然无法获得我想要的结构。其余的查询工作得很好,只是缺少这部分

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

我希望这{$addFields:{dataplanRoaming:["$subscriptionDataplans.allowRoaming", "$subscriptionDataplans._id"]}}, 会给我想要的结果,它只是用 _id 和 allowRoaming 作为分隔字段制作数组?有没有办法使用聚合等创建我想要的结果?

标签: databasemongodbaggregation-framework

解决方案


  • $map迭代循环subscriptionDataplans并返回所需的 feidls
db.collection.aggregate([
  {
    $addFields: {
      dataplanRoaming: {
        $map: {
          input: "$subscriptionDataplans",
          in: {
            _id: "$$this._id",
            allowRoaming: "$$this.allowRoaming"
          }
        }
      }
    }
  }
])

操场


推荐阅读