简体   繁体   中英

Merge MongoDB multiple documents output to single documents

Can anyone help me to merge array?

Using bellow query

db.products_new.aggregate([{"$match":{"$or":[{"name":{"$regex":"^Cosimo\\b"}},{"brand.brand_name":{"$regex":"Cosimo"}},{"categories":{"$elemMatch":{"$regex":"Cosimo"}}},{"model_number":{"$regex":"^Cosimo\\b"}},{"country_of_mfg":{"$regex":"^Cosimo\\b"}},{"keywords":{"$in":["Cosimo"]}},{"model_number":"Cosimo"},{"name":{"$regex":"\\bcosimo\\b"}}],"$and":[{"active":"1"}]}},{"$group":{"_id":{"brand_id":"$brand_id"},"a":{"$push":{"id":"$id","name":"$name","brand_id":"$brand_id","primary_image_url":"$primary_image_url","brand_name":"$brand.brand_name","slug":"$slug"}}}},{"$unwind":{"path":"$a","includeArrayIndex":"ranking"}},{"$project":{"_id":0,"brand_id":"$_id.brand_id","id":"$a.id","name":"$a.name","primary_image_url":"$a.primary_image_url","slug":"$a.slug","brand_name":"$a.brand_name","ranking":"$ranking"}},{"$sort":{"brand_id":1}},{"$facet":{"limit-2-per-brand_id":[{"$bucket":{"groupBy":"$ranking","boundaries":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99],"default":"Other","output":{"products_push":{"$push":{"_id":"$_id","group":1,"id":"$id","name":"$name","brand_id":"$brand_id","primary_image_url":"$primary_image_url","slug":"$slug","brand_name":"$brand_name"}}}}}]}},{"$unwind":"$limit-2-per-brand_id"},{"$project":{"products":{"$setUnion":[["$limit-2-per-brand_id.products_push"]]}}},{"$unwind":"$products"}])

Current Output

{ "products" : [ { "group" : 1, "id" : "28359", "name" : "ACE", "brand_id" : "1", "primary_image_url" : "https://dt4f7ywfipgvt.cloudfront.net/products_images/28359/optimized/ace-1.jpg", "slug" : "ace", "brand_name" : "Cosimo" } ] }
{ "products" : [ { "group" : 1, "id" : "28360", "name" : "ADA", "brand_id" : "1", "primary_image_url" : "https://dt4f7ywfipgvt.cloudfront.net/products_images/28360/optimized/ada-1.jpg", "slug" : "ada", "brand_name" : "Cosimo" } ] }
{ "products" : [ { "group" : 1, "id" : "28361", "name" : "BOWEN", "brand_id" : "1", "primary_image_url" : "https://dt4f7ywfipgvt.cloudfront.net/products_images/28361/optimized/bowen-1.jpg", "slug" : "bowen", "brand_name" : "Cosimo" } ] }
{ "products" : [ { "group" : 1, "id" : "28362", "name" : "BRUNO", "brand_id" : "1", "primary_image_url" : "https://dt4f7ywfipgvt.cloudfront.net/products_images/28362/optimized/bruno-1.jpg", "slug" : "bruno-Cosimo-sym", "brand_name" : "Cosimo" } ] }

Expected Output

"products" : [
    { "group" : 1, "id" : "28359", "name" : "ACE", "brand_id" : "1", "primary_image_url" : "https://dt4f7ywfipgvt.cloudfront.net/products_images/28359/optimized/ace-1.jpg", "slug" : "ace", "brand_name" : "Cosimo" },
    { "group" : 1, "id" : "28360", "name" : "ADA", "brand_id" : "1", "primary_image_url" : "https://dt4f7ywfipgvt.cloudfront.net/products_images/28360/optimized/ada-1.jpg", "slug" : "ada", "brand_name" : "Cosimo" },
    { "group" : 1, "id" : "28361", "name" : "BOWEN", "brand_id" : "1", "primary_image_url" : "https://dt4f7ywfipgvt.cloudfront.net/products_images/28361/optimized/bowen-1.jpg", "slug" : "bowen", "brand_name" : "Cosimo" },
    { "group" : 1, "id" : "28362", "name" : "BRUNO", "brand_id" : "1", "primary_image_url" : "https://dt4f7ywfipgvt.cloudfront.net/products_images/28362/optimized/bruno-1.jpg", "slug" : "bruno-Cosimo-sym", "brand_name" : "Cosimo" }

This is fixed using below aggregate options and giving me an expected output.

array(
        '$unwind' => '$limit-2-per-brand_id'
    ),
    array(
        '$group' => array(
            '_id'=> null,
            "results" => array(
                '$push' => '$limit-2-per-brand_id.products_push'
            )
        )
    ),
    array(
        '$project' => array(
            'results'=> 1,
            '_id'=> 0
        )
    ),
    array(
        '$unwind' => '$results'
    ),
    array(
        '$unwind' => '$results'
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM