MongoDB获取数组中字段拼成字符串输出

数据格式:

{
    "_id": ObjectId("5cc83985ef921e27e17e1b6e"),
    "domainId": NumberLong("10"),
    "shop": DBRef("shop_shop", ObjectId("5cc83985ef921e27e17e1b6a")),
    "simpleShop": {
        "shopId": ObjectId("5cc83985ef921e27e17e1b6a"),
        "shopCode": [
            "10-01-10-02-01-006"
        ],
        "shopType": "PUBLIC_SHOP",
        "shopNature": "RENT",
        "shopLocations": [
            {
                "marketId": "5c8f7db0f45a923e561051be",
                "market": "测试路1号",
                "buildingId": "5cc8133997bcec029206ae86",
                "building": "AD天桥",
                "floor": NumberInt("2"),
                "road": "AD天桥",
                "number": "002"
            }
        ],
        "area": NumberDecimal("25.5"),
        "coveredArea": NumberDecimal("0"),
        "businessScope": [
            "a-b-c"
        ],
        "brands": [
            "xxx"
        ],
        "block": "aaa",
        "level": NumberDecimal("0"),
        "tenants": [
            {
                "_id": ObjectId("5cc8133a008ec91198f192c5"),
                "customerType": "PERSONAL",
                "name": "周奇",
                "mobile": "13588888888"
            }
        ]
    },
    "businessUsers": [
        {
            "_id": ObjectId("5cc8133a008ec91198f192c5"),
            "customerType": "PERSONAL",
            "name": "周奇",
            "mobile": "13588888888"
        }
    ]
 }

想要输出的数据格式:

{
    "_id": ObjectId("5cc83985ef921e27e17e1b6e"),
    "shopLocations": [
        "测试路1号AD天桥2AD天桥002"
    ],
    "tenants": [
        {
            "name": "周奇",
            "mobile": "13588888888"
        }
    ],
    "businessUsers": [
        {
            "name": "周奇",
            "mobile": "13588888888"
        }
    ]
}

代码:

db.shop_shop_business.aggregate({
    $project: {
        _id: 1,
        shopLocations: {
            $map: {
                input: "$simpleShop.shopLocations",
                as: "s",
                in: {
                    $concat: ["$$s.market", "$$s.building", {
                        $toString: "$$s.floor"
                    }, "$$s.road", "$$s.number"]
                }
            }
        },
        tenants: {
            $map: {
                input: "$simpleShop.tenants",
                as: "s",
                in: {
                    name: "$$s.name",
                    mobile: "$$s.mobile"
                }
            }
        },
        businessUsers: {
            $map: {
                input: "$businessUsers",
                as: "s",
                in: {
                    name: "$$s.name",
                    mobile: "$$s.mobile"
                }
            }
        }
    }
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值